Spring实例化bean
1、构造方法实例化bean
<bean id=".." class="...">
<constructor-arg ref="..." />
<constructor-arg ref="..." />
</bean>
2、使用静态工厂实例化
<bean id="clientService"
class="examples.ClientService"
factory-method="createInstance" />
类中的静态方法如下:
public class ClientService {
private static ClientService clientService = new ClientService();
private ClientService() {}
public static ClientService createInstance() {
return clientService;
}
}
3、使用动态工厂实例化
<!-- the factory bean, which contains a method called createInstance() -->
<bean id="serviceLocator" class="examples.DefaultServiceLocator">
<!-- inject any dependencies required by this locator bean -->
</bean>
<!-- the bean to be created via the factory bean -->
<bean id="clientService"
factory-bean="serviceLocator"
factory-method="createClientServiceInstance"/>
类的实例化方法如下:
public class DefaultServiceLocator {
private static ClientService clientService = new ClientServiceImpl();
public ClientService createClientServiceInstance() {
return clientService;
}
}
区别:
静态工厂实例化使用的factory-method指定bean初始化所需要的的方法,一定需要的是静态方法,而实例工厂初始化bean需要两个标签,一个是factory-bean,另一个是factory-method。factory需要在配置文件中初始化一个bean,然后将实例工厂初始化的bean指向这个bean,而factory-method是指向一个实例方法,也就是说,在实例工厂实例化中是没有class标签的。
依赖注入
1、基于构造方法的依赖注入
<bean id=".." class="...">
<constructor-arg type=".." index=".." name=".." value=".."/>
</bean>
其中constructor-arg中拥有三个可选的参数
第一,构造函数参数的类型匹配:type=".."
第二,构造函数参数索引:index="0" 索引值从0开始
第三、构造函数参数名称:name="参数名"
2、基于setter的依赖注入
<bean id="exampleBean" class="examples.ExampleBean">
<!-- setter injection using the nested ref element -->
<property name="beanOne">
<ref bean="anotherExampleBean"/>
</property>
<!-- setter injection using the neater ref attribute -->
<property name="beanTwo" ref="yetAnotherBean"/>
<property name="integerProperty" value="1"/>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
定义个POJO类来设置和获取对应的值
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
}
public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo;
}
public void setIntegerProperty(int i) {
this.i = i;
}
}
集合类
集合类主要分以下四种的分析:1、props 2、list 3、set 4、map
props集合是为Java持久属性集注入值,也就是向 java.util.Properties 对象中注入值。
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.org</prop>
<prop key="support">support@example.org</prop>
<prop key="development">development@example.org</prop>
</props>
</property>
其他三种集合类的注入
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry key="an entry" value="just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
自动装配问题
1、byName(根据名称自动装配): 必须将目标 Bean 的名称和属性名设置的完全相同。
2、byType(根据类型自动装配): 若 IOC 容器中有多个与目标 Bean 类型一致的 Bean. 在这种情况下, Spring 将无法判定哪个 Bean 最合适该属性, 所以不能执行自动装配。报错。利用注解时,会用到byType。
3、constructor(通过构造器自动装配): 当 Bean 中存在多个构造器时, 此种自动装配方式将会很复杂. 不推荐使用。
格式:<bean id="..." class="..." autowire="byName" />
当定义其他几个bean的id跟自动装载bean的属性名一致的bean将会自动装载到当前的bean
网友评论