Spring IOC容器 の HelloWorld
1.新建对象的项目和模块,maven项目
- 新建一个项目,移除原有的模块,新建一个空白的java模块!
2.通过maven导入spring对应的jar包
右键,add framework support 添加Maven支持,通过Maven导入spring对应的jar包:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.crud</groupId> <artifactId>ssm-crud</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>4.12</junit.version> <spring.version>4.3.10.RELEASE</spring.version> <mybatis.version>3.4.5</mybatis.version> <mybatis.spring.version>1.3.1</mybatis.spring.version> <druid.version>1.1.6</druid.version> <commons-dbcp.version>1.4</commons-dbcp.version> <jstl.version>1.2</jstl.version> <log4j.version>1.2.17</log4j.version> <fastjson.version>1.2.45</fastjson.version> <slf4j.version>1.7.25</slf4j.version> <jackson.version>2.8.9</jackson.version> <commons-fileupload.version>1.3.3</commons-fileupload.version> <commons-io.version>2.5</commons-io.version> <commons-codec.version>1.10</commons-codec.version> <aspectjweaver.version>1.8.10</aspectjweaver.version> </properties> <dependencies> <!-- Junit测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- spring包 core、web、oxm、tx、jdbc、webmvc、aop、context、test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mybatis 代码生成器 --> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.6</version> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis.spring.version}</version> </dependency> <!-- 导入Mysql数据库链接jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> </dependency> <!-- 阿里巴巴 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>${commons-dbcp.version}</version> </dependency> <!-- JSTL标签类 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <!-- 日志文件管理包 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!-- <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.3</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.3</version> </dependency>--> <!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jsp-api --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jsp-api</artifactId> <version>7.0.86</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1</version> </dependency> <!-- 格式化对象,方便输出日志 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.0.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <!-- json和bean之间相互转换 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <!-- 上传组件包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>${commons-codec.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectjweaver.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <!--能加载 java 文件夹中的资源文件 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> </resource> <resource> <directory>src/main/resources</directory> </resource> </resources> <plugins> <!-- Jetty 插件 --> <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.4.8.v20171121</version> <configuration> <scanIntervalSeconds>1</scanIntervalSeconds> <reload>automatic</reload> <httpConnector> <port>8080</port> </httpConnector> <stopKey>jettyStopKey1</stopKey> <stopPort>8520</stopPort> <webAppSourceDirectory>${basedir}/web</webAppSourceDirectory> </configuration> </plugin> <!-- Tomcat 7 插件--> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8080</port> <contextReloadable>true</contextReloadable> <warSourceDirectory>${basedir}/web</warSourceDirectory> </configuration> </plugin> <!-- 打war 的插件 --> <plugin> <artifactId>maven-war-plugin</artifactId> <configuration> <warSourceDirectory>${basedir}/web</warSourceDirectory> </configuration> </plugin> <!-- 编译java 文件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
将以上代码的dependcy部分复制到pom.xml中的相应位置,静静等待包的下载完毕
2.复制java1.8的插件进入pom.xml配置resource,这样可以让我们的xml文件在指定的路径可以被读取,被粘贴的位置在build和plugins之间(build>resources=plugins)
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.*</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> </resource> </resources>
3.新建实体对象类和测试类
- 创建javapackage,新建一个实体对象Student以及测试类
Student.java
HelloWorldTest.javapublic class Student { String id; String name; String stuNo; } //无参构造,全参构造,以及全参的get set 省略
@Test public void test1(){ System.out.println("hello"); } //运行成功,可以进行下一步
4.创建IOC容器
- 新建spring配置文件(spring-xxx.xml 或 applicationContext-xx.xml)
spring-beans.xml
只复制这一些代码进去<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 使用bean元素定义一个由IOC容器创建的对象 --> <!-- class属性指定用于创建bean的全类名 --> <!-- id属性指定用于引用bean实例的标识 --> <bean id="student" class="com.helloworld.bean.Student"> <!-- 使用property子元素为bean的属性赋值 --> <property name="studentId" value="1001"/> <property name="stuName" value="Tom2015"/> <property name="age" value="20"/> </bean> </beans>
- Test.java
@Test public void test1(){ BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-beans.xml"); System.out.println(beanFactory); }
- 运行结果
上面的警告是因为日志我们还没有配置,下方打印出了我们的beanFactory对象log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. org.springframework.context.support.ClassPathXmlApplicationContext@66048bfd: startup date [Thu Nov 14 17:08:33 CST 2019]; root of context hierarchy
- Test.java
@Test public void test1(){ // BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-beans.xml"); // System.out.println(beanFactory); Student student = new Student(); student.setId("qwer"); student.setStuNo("stu001"); student.setName("hkx"); System.out.println(student); }
- 运行结果
Student{id='qwer', name='hkx', stuNo='stu001'}
- 使用BeanFactory
- Test.java
此时要将spring-beans.xml进行完善,告诉BeanFactory模板是什么public void test1(){ BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-beans.xml"); Student student = (Student) beanFactory.getBean("s1"); //此时使用BeanFactory对实体类Student进行生产,可是模板是什么呢? System.out.println(student);}
- 修改spring-beans.xml文件,在beans标签中添加以下代码
<!--name属性可以容纳诸如"/abc"这样的名称,但是id不可以,当id中的"s1"无法被找到时将寻找name中的值。--> <!--最好是能用id就不用name--> <!--id对应的是test类中getBean的参数--> <!-- <bean id="s1" name="s1"></bean>--> <bean id="s1" class="top.leafii.springdemo.demo1helloworld.domain.Student"> </bean>
- 再次修改xml文件,增加一个Student类的对象student2
- spring-beans.xml
<bean id="s2" class="top.leafii.springdemo.demo1helloworld.domain.Student"> <property name="id" value="AA"></property> <property name="name"> <value>yys</value> </property> <!--以上两种方式无差异--> <!--什么时候用以上的那种子标签呢?如果value里有很多很多很多文字就可以用这个方式了 增强代码的可读性--> <property name="stuNo" value="stu007"/> <property name="student" ref="s1"/> <!--注意在类中引用对象要用ref而不是value--> </bean>
- Test.java
BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-beans.xml"); Student student1 = (Student) beanFactory.getBean("s1"); Student student2 = (Student) beanFactory.getBean("s2"); //此时使用BeanFactory对实体类Student进行生产,可是模板是什么呢? System.out.println("student1:"+student1); System.out.println("student2:"+student2);
- 运行结果(记得重写Student类的toString方法)
student1:Student{id='qwer', name='hkx', stuNo='stu001', student=null} student2:Student{id='AA', name='yys', stuNo='stu007', student=Student{id='qwer', name='hkx', stuNo='stu001', student=null}}
5.注入
- 此时我们要想一下使用spring-beans.xml构造对象时,使用的是哪种方法,是按照参数的顺序直接进行构造方法,还是先new一个无参对象,再使用属性的set方法呢?
- 两种注入的方法:
Student student1 = new Student("idAAA","hkx","stu001");//构造注入 Student student = new Student(); student.setId("qwer"); student.setStuNo("stu001"); student.setName("hkx"); //setter注入
- 通过下图这样的测试,我们得知,使用spring-beans默认所使用的注入方法为setter注入
- 但是可以通过指定的方式设置使用构造注入进行注入
使用constructor-arg标签之后,不关注属性名,只关注该属性值在构造函数中的位置 - 此时我们放弃过时的BeanFactory,使用新的框架
- Test.java
@Test public void test1(){ ApplicationContext context = new ClassPathXmlApplicationContext("spring-beans.xml"); // BeanFactory beanFactory = new ClassPathXmlApplicationContext("spring-beans.xml"); // Student student2 = (Student) beanFactory.getBean("s2"); Student student3 = (Student) context.getBean("s3"); System.out.println(student3); }
总结
- 新建对象的项目和模块,maven项目
- 通过maven导入spring对应的jar包
- 新建实体对象 类 和 测试类
- 创建IOC容器
4.1 新建spring配置文件(spring-xxx.xml 或 applicationContext-xx.xml)
4.2 写spring-xxx.xml
4.3 在实体类中添加“引用类型”的属性<bean id="s1" class="top.leafii.springdemo.demo1helloworld.domain.Student"> <property name="id" value="qwer"/> <property name="name"> <value>hkx</value> </property> <!--以上两种方式无差异--> <!--什么时候用以上的那种子标签呢?如果value里有很多很多很多文字就可以用这个方式了 增强代码的可读性--> <property name="stuNo" value="stu001"/> </bean>
<property name="student" ref="s1"/>
4.4 配置构造注入的方式
<bean id="s3" class="top.leafii.springdemo.demo1helloworld.domain.Student">
<constructor-arg value="idCCC" index="0"/>
<constructor-arg value="yysssy" index="1"/>
<constructor-arg value="stu003" index="2"/>
</bean>
需要掌握以下几点:
- 简单类型和引用类型的配置方式:value和ref
- 构造注入的方式
- IOC容器的创建
ApplicationContext context = new ClassPathXmlApplicationContext("spring-beans.xml");