IoC实例化bean的方式
- 使用构造器
- 静态工厂方法实现实例化
- 实例工厂方法实现实例化
UserBean
package cn.sdut.bean;
public class UserBean {
private int id;
private String username;
private int age;
public UserBean() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
使用构造器
beans.xml
<bean id="user" class="cn.sdut.bean.UserBean"></bean>
Test测试类
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//获得UserBean对象
UserBean user = (UserBean) context.getBean("user");
//模拟从数据库中取出数据
user.setUsername("hydra");
System.out.println(user.getUsername());
UserBean user1 = (UserBean) context.getBean("user");
//scope默认为singleton
System.out.println(user1.getUsername());
scope不设置,默认为单例模式,两个对象user和user1为同一个对象。
静态工厂方法实例化
StaticBeanFactory
package cn.sdut.bean;
/**
* UserBean对象的静态工厂模式
* @author DELL
*
*/
public class StaticBeanFactory {
public static UserBean createUser() {
return new UserBean();
}
}
beans.xml
factory-method是工厂方法,填写的是StaticBeanFactory里的方法
<bean id="staticuser" class="cn.sdut.bean.StaticBeanFactory" factorymethod="createUser">
</bean>
测试类 Test
BeanFactory在使用时,才实例化bean,如果此时配置文件有错,则会产生装配错误
//延时创建,使用时创建
BeanFactory factory = new ClassPathXmlApplicationContext("beans.xml");
UserBean user2 = (UserBean) factory.getBean("staticuser");
user2.setUsername("Hydra");
System.out.println(user2.getUsername());
实例工厂模式
UserBeanFactory
package cn.sdut.bean;
/**
* 实例工厂
* @author DELL
*
*/
public class UserBeanFactory {
public UserBean createUser() {
return new UserBean();
}
}
beans.xml
<bean id="factory" class="cn.sdut.bean.UserBeanFactory"></bean>
<bean id="insuser" factory-bean="factory" factory-method="createUser"></bean>
实例化工厂模式需要配置两个bean,类似于
UserBeanFactory factory = new UserBeanFactory();
factory.createrUser();
测试类Test
//实例工厂模式
UserBean user3 = (UserBean) context.getBean("insuser");
user3.setAge(20);
System.out.println(user3.getAge());
bean注入的三种类型
- 构造函数注入
- 属性值是通过构造函数的参数注入
- 属性注入
- 属性值是通过属性的set方法注入
- 接口注入
- 与动态代理冲突,暂不考虑
构造函数注入
beans.xml
<bean id="zhangsan" class="cn.sdut.bean.UserBean">
<constructor-arg index="0" type="int" value="5"></constructor-arg>
<constructor-arg index="1" type="String" value="zhangsan"></constructor-arg>
<constructor-arg index="2" type="int" value="21"></constructor-arg>
<!-- <constructor-arg index="3" ref="group"></constructor-arg> -->
<constructor-arg index="3"><ref bean="group"></ref></constructor-arg>
</bean>
构造方法必须与beans.xml一一对应。
<constructor-arg>标签通过index、type、value进行构造函数注入。
index表示构造方法的参数索引值,从0开始,type为数据类型,(自定义时可以用ref属性或者ref标签引入),value为要注入的默认值。
属性注入
<bean id="ituser" class="cn.sdut.bean.UserBean" depends-on="group,dormitory">
<property name="group" ref="group"></property>
<property name="dormitory" ref="dormitory"></property>
</bean>
<bean id="group" class="cn.sdut.bean.Group">
<property name="id">
<value>1</value>
</property>
<property name="name">
<value>清华大学</value>
</property>
<property name="addr">
<value>北京</value>
</property>
</bean>
属性注入时,是通过set方法注入的,beans.xml通过porperty属性来配置,name为变量属性名,value为要注入的值。
集合属性注入
- list
- set
- map
- props
Group中新增两个属性,List集合的
private List<String> classList;
private List<UserBean> userList;
public List<UserBean> getUserList() {
return userList;
}
public void setUserList(List<UserBean> userList) {
this.userList = userList;
}
public List<String> getClassList() {
return classList;
}
public void setClassList(List<String> classList) {
this.classList = classList;
}
beans.xml
<bean id="neu" class="cn.sdut.bean.Group">
<property name="id" value="66"></property>
<property name="name" value="bd"></property>
<property name="addr"> <value>it is a addr</value></property>
<property name="classList">
<list>
<value>java6班</value>
<value>java7班</value>
<value>java8班</value>
</list>
</property>
<property name="userList">
<list>
<ref bean="ituser" />
</list>
</property>
</bean>
和其他的类似,基本数据类型用value,其他的用ref来注入,但是集合属性的注入需要在value标签外套上所属的集合标签
如上面的list,此外还有set,map,props。set和list用法相同,map和props使用的是键值对的方式来注入
<property name="map">
<map>
<entry key="key"><ref bean="user"/></entry>
</map>
</property>
<property name="props">
<props key="key1">value1</props>
<props key="key2">value2</props>
</property>