spring框架创建helloworld

来源:互联网 发布:js arguments 回调函数 编辑:程序博客网 时间:2024/06/06 09:49

1.spring是一个容器,托管他的对象的生命周期
2.spring模块,中间这一块AOP Aspects instrumentation messaging,是一些思想。
这里写图片描述
官网
https://spring.io/
这里写图片描述

新建spring工程
1.通过maven来创建,新建工程
这里写图片描述
2.下一步,选择这个
这里写图片描述
3.输入项目名和包名
这里写图片描述
4.添加运行时的jar包,tomcat的包。
这里写图片描述
5.在main目录下新建java目录,
6.修改pom.xml中加入依赖

org.springframework
spring-context
4.3.8.RELEASE

新建package。包名com.sixstar.spring.charpter01
7.新建class,名叫HelloWorld,
public class HelloWorld {

private String userName;public HelloWorld() {    // TODO Auto-generated constructor stub    System.out.println("初始化helloworld");}public String getUserName() {    return userName;}public void setUserName(String username) {    System.out.println("设置属性值!!!");    this.userName = username;}public void sayHello(){    System.out.println(userName+"say hellow");}

}

8.新建Class ,Test。提供main方法
public class Test {

public static void main(String[] args) {    HelloWorld helloWorld = new HelloWorld();    /**     * 1.写一个JAVAbean 2.写一个bean配置文件     */    // 创建IOC容器    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");// 加载bean文件    // 从spring容器中获取需要的Bean实例    HelloWorld h2 = (HelloWorld) ctx.getBean("helloWorld");}

}

9.在resources目录下,新建beans.xml文件。

0 0