maven创建
mvn archetype:generate
# 选择7,创建快速启动项目
然后idea打开即可
idea创建
点击创建
使用IOC
pom.xml添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.20</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
将App和AppTest删除
新建Student
package com.wujilaiang.entity;
import lombok.Data;
@Data
public class Student {
private long id;
private String name;
private int age;
}
传统方法
新建IocTest.java
package com.wujilaiang;
import com.wujilaiang.entity.Student;
import org.junit.Test;
public class IocTest {
@Test
public void traditionFunc(){
Student student = new Student();
student.setId(1L);
student.setName("张三");
student.setAge(22);
System.out.println(student);
}
}
ioc创建
resources文件夹下新建spring.xml
<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.xsd">
<bean id="student" class="com.wujilaiang.entity.Student">
<property name="id" value="1"></property>
<property name="name" value="小明"></property>
<property name="age" value="22"></property>
</bean>
</beans>
编写测试方法测试
package com.wujilaiang;
import com.wujilaiang.entity.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class IocTest {
@Test
public void traditionFunc(){
Student student = new Student();
student.setId(1L);
student.setName("张三");
student.setAge(22);
System.out.println(student);
}
@Test
public void IcoTest(){
//加载配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Student student = (Student) applicationContext.getBean("student");
System.out.println(student);
}
}
ref使用
新建Address.java
package com.wujilaiang.entity;
import lombok.Data;
@Data
public class Address {
private Integer id;
private String name;
}
修改Student.java
package com.wujilaiang.entity;
import lombok.Data;
@Data
public class Student {
private long id;
private String name;
private int age;
private Address address;
}
修改spring.xml
<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.xsd">
<bean id="student" class="com.wujilaiang.entity.Student">
<property name="id" value="1"></property>
<property name="name" value="小明"></property>
<property name="age" value="22"></property>
<property name="address" ref="address"></property>
</bean>
<bean id="address" class="com.wujilaiang.entity.Address">
<property name="name" value="山东"></property>
<property name="id" value="1"></property>
</bean>
</beans>
运行IcoTest
通过属性注入的时候需要有无参构造函数
通过构造函数注入
修改Studetn.java
package com.wujilaiang.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Student {
private long id;
private String name;
private int age;
private Address address;
}
修改spring.xml
<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.xsd">
<!--<bean id="student" class="com.wujilaiang.entity.Student">
<property name="id" value="1"></property>
<property name="name" value="小明"></property>
<property name="age" value="22"></property>
<property name="address" ref="address"></property>
</bean>-->
<bean id="student" class="com.wujilaiang.entity.Student">
<constructor-arg name="id" value="1"></constructor-arg>
<constructor-arg name="name" value="小明"></constructor-arg>
<constructor-arg name="age" value="22"></constructor-arg>
<constructor-arg name="address" ref="address"></constructor-arg>
</bean>
<bean id="address" class="com.wujilaiang.entity.Address">
<property name="name" value="山东"></property>
<property name="id" value="1"></property>
</bean>
</beans>
然后测试
可以不写名称按照顺序配置
<bean id="student" class="com.wujilaiang.entity.Student">
<constructor-arg value="1"></constructor-arg>
<constructor-arg value="小明"></constructor-arg>
<constructor-arg value="22"></constructor-arg>
<constructor-arg ref="address"></constructor-arg>
</bean>
或者通过参数索引指定
<bean id="student" class="com.wujilaiang.entity.Student">
<constructor-arg index="0" value="1"></constructor-arg>
<constructor-arg index="2" value="22"></constructor-arg>
<constructor-arg index="1" value="小明"></constructor-arg>
<constructor-arg index="3" ref="address"></constructor-arg>
</bean>
通过运行时类获取 bean
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
Student student = (Student) applicationContext.getBean(Student.class);
System.out.println(student);
这种方式存在一个问题,配置文件中一个数据类型的对象只能有一个实例,否则会抛出异常,因为没有唯一的 bean。
配置文件
-
通过配置
bean
标签来完成对象的管理。id
:对象名。class
:对象的模版类(所有交给 IoC 容器来管理的类必须有无参构造函数,因为 Spring 底层是通过反射机制来创建对象,调用的是无参构造)
-
对象的成员变量通过
property
标签完成赋值。name
:成员变量名。value
:成员变量值(基本数据类型,String 可以直接赋值,如果是其他引用类型,不能通过 value 赋值)ref
:将 IoC 中的另外一个 bean 赋给当前的成员变量(DI)