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>
下面几种注入方式先看下示例:
package examples;public class ExampleBean {// No. of years to the calculate the Ultimate Answerprivate int years;// The Answer to Life, the Universe, and Everythingprivate 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;}
}
转载于:https://www.cnblogs.com/zhwj184/archive/2013/01/08/3027438.html
spring 实例化bean的几种方法相关推荐
- Spring 实例化bean的三种方式
第一种方法:直接配置Bean Xml代码 <bena id="所需要实例化的一个实例名称" class="包名.类名"/> 例如: 配置文件中的 ...
- Spring装配bean的三种方法:自动化装配,java代码装配,XML装配及它们的混合使用
一.自动化装配 首先,把可能被装配的类声明为组件类,告知spring要为这个类创建bean如: import org.springframework.stereotype.Component;@ Co ...
- Spring2.5.6学习笔记-实例化bean的几种方法
1. 用构造器来实例化 <bean id="exampleBean" class="examples.ExampleBean"/> <bean ...
- Java普通类获取Spring框架Bean 的五种方法
方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContex(" ...
- Spring Ioc源码分析系列--容器实例化Bean的四种方法
- java 获取bean_spring如何获取bean的6种方法,你知道几个?
spring获取bean的6种方法 Bean工厂(com.springframework.beans.factory.BeanFactory)是Spring框架最核心的接口,它提供了高级IoC的配置机 ...
- Spring框架学习教程,详解Spring注入bean的几种方式
首先,要学习Spring中的Bean的注入方式,就要先了解什么是依赖注入. 依赖注入是指:让调用类对某一接口的实现类的实现类的依赖关系由第三方注入,以此来消除调用类对某一接口实现类的依赖. Sprin ...
- spring 获取 WebApplicationContext的几种方法
spring 获取 WebApplicationContext的几种方法 使用ContextLoader WebApplicationContext webApplicationContext = C ...
- Spring创建Bean的3种方式
1. Spring创建Bean的三种方式 1. 调用构造器(因为常用就不讲) 2. 调用静态工厂方法 3. 调用实例工厂方法 2. 使用静态工厂方法 1.此时<bean.../>元素要指定 ...
最新文章
- php取后三字符串,php字符串截取问题
- 使用STC8H1K的高级PWM的正交编码器计数方式
- 关于display相关的一些内容(总体)
- Java中使用Jedis连接Redis对Hash进行操作的常用命令
- 敏捷制造:并不是你想像的矛盾体
- Scala中的match(模式匹配)
- 编译驱动的Makefile解析
- hello March
- my04_Mysql复制数据一致性校验
- oracle 10g for solaris准备工作和实例的删除
- Network | parity bit
- 《算法竞赛入门经典》——刘汝佳
- 【GANs学习笔记】(十三)BIGGAN
- 2022前端未来发展趋势
- SPI FLASH测试
- 泥瓦匠之 Java 的成长感悟
- 【GEO数据库】数据
- git--工作区、暂存区、本地仓库、远程仓库
- python中最小公倍数函数_python求最大公约数和最小公倍数的简单方法
- python pdf 加水印_Python中通过PyPDF2实现PDF添加水印