spring 实例化bean的几种方法
2013-01-08 17:04
435 查看
1.普通的通过构造函数初始化,没有指定构造函数参数的就是用默认的无参的构造方法
构造函数的几种方式:
1.普通沟通函数注入方式,按照构造函数参数的顺序和个数来注入bean
下面几种注入方式先看下示例:
2.按照构造函数的参数类型匹配注入
3.按照参数索引顺序注入
4. spring3以上还可以通过参数名称进行注入
5.spring3以上通过annotation注入
6.spring3.1以上还可以使用简化的c namespace来进行构造函数注入
c:_index方式注入
2.通过静态的工厂方法生成bean,这种方式在配置文件中没有指定返回的bean的类型
3.通过实例化的工厂方法生成bean
当然这个实例化的工厂类也可以生成多个bean
<bean id="exampleBean" class="examples.ExampleBean"/> <bean name="anotherExample" class="examples.ExampleBeanTwo"/>
构造函数的几种方式:
1.普通沟通函数注入方式,按照构造函数参数的顺序和个数来注入bean
package x.y; public class Foo { public Foo(Bar bar, Baz baz) { // ... } }
<beans> <bean id="foo" class="x.y.Foo"> <constructor-arg ref="bar"/> <constructor-arg ref="baz"/> </bean> <bean id="bar" class="x.y.Bar"/> <bean id="baz" class="x.y.Baz"/> </beans>
下面几种注入方式先看下示例:
package examples; public class ExampleBean { // No. of years to the calculate the Ultimate Answer private int years; // The Answer to Life, the Universe, and Everything private String ultimateAnswer; public ExampleBean(int years, String ultimateAnswer) { this.years = years; this.ultimateAnswer = ultimateAnswer; } }
2.按照构造函数的参数类型匹配注入
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg type="int" value="7500000"/> <constructor-arg type="java.lang.String" value="42"/> </bean>
3.按照参数索引顺序注入
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg index="0" value="7500000"/> <constructor-arg index="1" value="42"/> </bean>
4. spring3以上还可以通过参数名称进行注入
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg name="years" value="7500000"/> <constructor-arg name="ultimateanswer" value="42"/> </bean>
5.spring3以上通过annotation注入
@ConstructorProperties
package examples; public class ExampleBean { // Fields omitted @ConstructorProperties({"years", "ultimateAnswer"}) public ExampleBean(int years, String ultimateAnswer) { this.years = years; this.ultimateAnswer = ultimateAnswer; } }
6.spring3.1以上还可以使用简化的c namespace来进行构造函数注入
<bean id="foo" class="x.y.Foo" c:bar-ref="bar" c:baz-ref="baz" c:email="foo@bar.com">
c:_index方式注入
<-- 'c-namespace' index declaration --> <bean id="foo" class="x.y.Foo" c:_0-ref="bar" c:_1-ref="baz">
2.通过静态的工厂方法生成bean,这种方式在配置文件中没有指定返回的bean的类型
<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.通过实例化的工厂方法生成bean
<!-- 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(); private DefaultServiceLocator() {} public ClientService createClientServiceInstance() { return clientService; } }
当然这个实例化的工厂类也可以生成多个bean
<bean id="serviceLocator" class="examples.DefaultServiceLocator"> <!-- inject any dependencies required by this locator bean --> </bean> <bean id="clientService" factory-bean="serviceLocator" factory-method="createClientServiceInstance"/> <bean id="accountService" factory-bean="serviceLocator" factory-method="createAccountServiceInstance"/>
public class DefaultServiceLocator { private static ClientService clientService = new ClientServiceImpl(); private static AccountService accountService = new AccountServiceImpl(); private DefaultServiceLocator() {} public ClientService createClientServiceInstance() { return clientService; } public AccountService createAccountServiceInstance() { return accountService; } }
相关文章推荐
- spring 实例化bean的几种方法
- java_spring_实例化bean的3种方法
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring工厂方法创建(实例化)bean实例代码
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring三种实例化Bean的方法
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring三种实例化Bean的方法
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- spring 实例化定义在xml配置文件中的bean的三种方法?
- Spring、实例化Bean的三种方法
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring、实例化Bean的三种方法
- Spring 学习笔记1--三种实例化Bean方法、注解
- Spring、实例化Bean的三种方法
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序
- Spring-- Ioc 容器Bean实例化的几种场景
- Spring中实例化bean的三种方法
- Spring的几种工厂方法创建bean的总结
- Spring容器中的Bean几种初始化方法和销毁方法的先后顺序