spring 实例化bean的几种方法
生活随笔
收集整理的這篇文章主要介紹了
spring 实例化bean的几种方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.普通的通過構造函數初始化,沒有指定構造函數參數的就是用默認的無參的構造方法
<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>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;} }
轉載于:https://www.cnblogs.com/zhwj184/archive/2013/01/08/3027438.html
總結
以上是生活随笔為你收集整理的spring 实例化bean的几种方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: R语言下载安装
- 下一篇: 主机无法连接虚拟机中的redis服务