public class HelloSpring {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "HelloSpring{" +
"id=" + id +
", name=‘" + name + ‘\‘‘ +
‘}‘;
}
}
Spring容器创建、组装并管理Bean对象;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="hello" class="indi.jaywee.pojo.HelloSpring">
<property name="id" value="7"/>
<property name="name" value="jaywee"/>
</bean>
</beans>
可以传入多个参数
ApplicationContext context = new ClassPathXmlApplicationContext("xxx.xml","xxx.xml");
要获取哪个bean对象,参数就写哪个对象的 id
context.getBean("beanId");
@Test
public void testToString() {
// 实例化容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloSpring hello = (HelloSpring) context.getBean("hello");
System.out.println(hello);
}
测试结果:
如果用户有不同需求,只需修改XML文件中元数据的配置即可,不再需要修改程序代码。
改进IOC理论推导
中的需求实现。
配置Spring元数据
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDaoImpl" class="indi.jaywee.dao.UserDaoImpl"/>
<bean id="userDaoMySqlImpl" class="indi.jaywee.dao.UserDaoMySqlImpl"/>
<!--
引用数据类型:使用ref作为属性值,引用容器中的其他Bean。
-->
<bean id="userServiceImpl" class="indi.jaywee.service.UserServiceImpl">
<property name="userDao" ref="userDaoImpl"/>
</bean>
</beans>
测试
@Test
public void testSpring(){
// 实例化容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
UserService userService = (UserServiceImpl)context.getBean("userServiceImpl");
userService.getUserInfo();
}
原理:通过反射实现。
IOC配置元数据时,创建 Bean对象的方式如下:
要求:必须要有无参构造方法和setter方法。
<bean id="hello" class="indi.jaywee.pojo.HelloSpring">
<property name="id" value="7"/>
<property name="name" value="jaywee"/>
</bean>
要求:要有相应的有参构造方法,允许重载。
通过有参构造方法为变量赋值,可以通过以下 3种方式:
根据构造方法中声明的参数顺序,下标从0开始。
<bean id="hello" class="indi.jaywee.pojo.HelloSpring">
<constructor-arg index="0" value="7"/>
<constructor-arg index="1" value="jaywee"/>
</bean>
不推荐使用!因为当参数列表中有多个相同类型的参数时无法使用。
<bean id="hello" class="indi.jaywee.pojo.HelloSpring">
<constructor-arg type="int" value="7"/>
<constructor-arg type="java.lang.String" value="jaywee"/>
</bean>
推荐使用!
<bean id="hello" class="indi.jaywee.pojo.HelloSpring">
<constructor-arg name="id" value="7"/>
<constructor-arg name="name" value="jaywee"/>
</bean>
在实例化容器时,元数据中配置的所有Bean对象都已创建完成了。
如果是用无参构造方法,Bean对象都已创建完成,属性尚未赋值;
如果是用有参构造方法,Bean对象都已创建完成,属性已被赋值。即创建并组装完成。
进行如下测试
实体类一旦被实例化,就会执行构造方法中的代码。
toString()
中有设置好的值,说明Bean已完成装配。public class HelloSpring {
private int id;
private String name;
public HelloSpring() {
System.out.println("无参构造方法:Hello Spring被创建了!");
System.out.println(this.toString());
}
public HelloSpring(int id, String name) {
System.out.println("有参构造方法:Hello Spring被创建了!");
this.id = id;
this.name = name;
System.out.println(this.toString());
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "HelloSpring{" +
"id=" + id +
", name=‘" + name + ‘\‘‘ +
‘}‘;
}
}
@Test
public void testToString() {
// 实例化容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
}
修改元数据的配置,测试在不同构造方法的情况下,Bean对象的创建和装配情况:
有参构造方法
配置元数据
<bean id="hello" class="indi.jaywee.pojo.HelloSpring">
<constructor-arg name="id" value="7"/>
<constructor-arg name="name" value="jaywee"/>
</bean>
测试结果
结论:在实例化容器时,有参构造方法的Bean对象已创建并组装完成。
无参构造方法
配置元数据
<bean id="hello" class="indi.jaywee.pojo.HelloSpring">
<property name="id" value="7"/>
<property name="name" value="jaywee"/>
</bean>
测试结果:
结论:在实例化容器时,无参构造方法的Bean对象已创建完成,但还未组装。
进一步测试:
@Test
public void testToString() {
// 实例化容器
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloSpring hello = (HelloSpring) context.getBean("hello");
System.out.println("从容器中获取Bean");
System.out.println(hello);
}
测试结果:
结论:在从容器中获取Bean时,无参构造方法才完成组装。
不管是用哪种构造方法,在实例化容器时,元数据中配置的所有Bean对象都已完成创建。但是不同构造方法的组装情况有所不同。
原文:https://www.cnblogs.com/secretmrj/p/15082677.html