17、Spring初始化Bean的三种方式
来源:互联网 发布:sql server 2005 网盘 编辑:程序博客网 时间:2024/04/29 20:11
Spring Bean实例的创建方式以及依赖配置
创建Bean通常有三种方法:
1、调用构造器创建;
2、静态工厂创建;
3、实力工厂创建。
A、构造器创建:最常用使用设置注入的方式,要求该类提供无参构造器。这种情况下,class元素是必须的,除非采用继承,class属性值就是Bean的实现类。
Java代码如下:
public interface Person{
public void useAxe();
}
public class Chinese implements Person{
private Axe axe;
public Chinese(){
System.out.println("通过构造器初始化Bean");
}
public void setAxe(Axe axe){
System.out.println("Spring执行依赖注入");
this.axe = axe;
}
public void useAxe(){
System.out.println(axe.chop());
}
}
public interface Axe{
public String chop();
}
public SteelAxe implements Axe{
public SteelAxe(){
System.out.println("Spring初始化依赖Bean....");
}
public String chop(){
System.out.println("Spring依赖Bean初始化完成");
}
}
Spring-base.xml文件配置如下:
<bean id = "steelAxe" class="StellAxe"></bean>
<bean id = "chinese" class="Chinese">
<property name="axe" ref="steelAxe"/>
</bean>
测试代码:
public class Test{
public static void main(String[] args){
ApplicationContext cxt = ClassPathXmlApplicationContext("spring-base.xml");
Chinese cc = cxt.getBean("chinese");
cc.useAxe();
}
}
测试结果:
通过构造器初始化Bean
Spring初始化依赖Bean....
Spring依赖Bean初始化完成
B、静态工厂创建:使用静态工厂创建实例bean,class指向静态工厂类,添加factory-methd属性,用来创建所需的实例Bean.
Java代码如下:
public interface Product{
void buy();
}
public class Book implements Product{
private String msg;
public void buy(){
System.out.println("你好,我是:"+msg);
}
}
public class Food implements Product{
private String msg;
public void buy(){
System.out.println("你好,我是:"+msg);
}
}
public class StaticFactory{
public static Product getBean(String arg){
if(arg.equalsIgnoreCase("book")){
return new Book();
}else{
return new Food();
}
}
}
Spring-base.xml文件配置如下:
<bean id = "book" class="StaticFactory" factory-method="getBean">
<constructor-arg value="book"/>
<property name = "msg" value="book"/>
</bean>
测试代码:
public class Test{
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-base.xml");
Book book = ctx.getBean("book", Book.class);
book.buy();
}
}
C、使用实例工厂:实例工厂不需要class属性,直接用factory-bean属性指向工厂实例,其余配置和静态工厂无异。
public interface Person{
void sayHello();
}
public class Chinese implements Person{
public void sayHello(){
System.out.println("你好,我是中国人!");
}
}
public class American implements Person{
public void sayHello(){
System.out.println("你好,我是美国ren!");
}
}
public class InstanceFactory{
public static Person getInstance(String arg){
if (arg.equelsIgnoreCase("chinese")){
return new Chinese();
}
}
}
Spring-base.xml文件配置如下:
<bean id = "instanceFactory" class = "InstanceFactory"/>
<bean id = "chinese" factory-bean="instanceFactory" factory-method="getInstance">
<constructor-arg value="chinese"/>
</bean>
测试代码:
public class Test{
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-base.xml");
Chinese chinese = ctx.getBean("chinese", Chinese.class);
chinese.sayHello();
}
}
创建Bean通常有三种方法:
1、调用构造器创建;
2、静态工厂创建;
3、实力工厂创建。
A、构造器创建:最常用使用设置注入的方式,要求该类提供无参构造器。这种情况下,class元素是必须的,除非采用继承,class属性值就是Bean的实现类。
Java代码如下:
public interface Person{
public void useAxe();
}
public class Chinese implements Person{
private Axe axe;
public Chinese(){
System.out.println("通过构造器初始化Bean");
}
public void setAxe(Axe axe){
System.out.println("Spring执行依赖注入");
this.axe = axe;
}
public void useAxe(){
System.out.println(axe.chop());
}
}
public interface Axe{
public String chop();
}
public SteelAxe implements Axe{
public SteelAxe(){
System.out.println("Spring初始化依赖Bean....");
}
public String chop(){
System.out.println("Spring依赖Bean初始化完成");
}
}
Spring-base.xml文件配置如下:
<bean id = "steelAxe" class="StellAxe"></bean>
<bean id = "chinese" class="Chinese">
<property name="axe" ref="steelAxe"/>
</bean>
测试代码:
public class Test{
public static void main(String[] args){
ApplicationContext cxt = ClassPathXmlApplicationContext("spring-base.xml");
Chinese cc = cxt.getBean("chinese");
cc.useAxe();
}
}
测试结果:
通过构造器初始化Bean
Spring初始化依赖Bean....
Spring依赖Bean初始化完成
B、静态工厂创建:使用静态工厂创建实例bean,class指向静态工厂类,添加factory-methd属性,用来创建所需的实例Bean.
Java代码如下:
public interface Product{
void buy();
}
public class Book implements Product{
private String msg;
public void buy(){
System.out.println("你好,我是:"+msg);
}
}
public class Food implements Product{
private String msg;
public void buy(){
System.out.println("你好,我是:"+msg);
}
}
public class StaticFactory{
public static Product getBean(String arg){
if(arg.equalsIgnoreCase("book")){
return new Book();
}else{
return new Food();
}
}
}
Spring-base.xml文件配置如下:
<bean id = "book" class="StaticFactory" factory-method="getBean">
<constructor-arg value="book"/>
<property name = "msg" value="book"/>
</bean>
测试代码:
public class Test{
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-base.xml");
Book book = ctx.getBean("book", Book.class);
book.buy();
}
}
C、使用实例工厂:实例工厂不需要class属性,直接用factory-bean属性指向工厂实例,其余配置和静态工厂无异。
public interface Person{
void sayHello();
}
public class Chinese implements Person{
public void sayHello(){
System.out.println("你好,我是中国人!");
}
}
public class American implements Person{
public void sayHello(){
System.out.println("你好,我是美国ren!");
}
}
public class InstanceFactory{
public static Person getInstance(String arg){
if (arg.equelsIgnoreCase("chinese")){
return new Chinese();
}
}
}
Spring-base.xml文件配置如下:
<bean id = "instanceFactory" class = "InstanceFactory"/>
<bean id = "chinese" factory-bean="instanceFactory" factory-method="getInstance">
<constructor-arg value="chinese"/>
</bean>
测试代码:
public class Test{
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-base.xml");
Chinese chinese = ctx.getBean("chinese", Chinese.class);
chinese.sayHello();
}
}
0 0
- Spring Bean 初始化的三种方式
- 17、Spring初始化Bean的三种方式
- Spring管理的bean初始化方法的三种方式,以及@PostConstruct不起作用的原因
- Spring bean 的初始化和销毁的三种方式和执行顺序
- Spring bean的初始化和销毁三种方式及优先级
- spring实例bean的三种方式
- spring 创建bean的三种方式.
- spring创建bean的三种方式
- spring的三种bean创建方式
- Spring bean的三种创建方式
- Spring bean的三种注入方式
- spring创建bean的三种方式
- Spring装配bean的三种方式
- Spring获取bean的三种方式
- spring装配bean的三种方式
- Spring创建Bean的三种方式
- Spring创建Bean的三种方式
- Spring创建bean 的三种方式
- 测试博文测试博文
- [每日一记]php访问mysql
- Internal Server Error 500 错误
- 【动作】Cocosd-x3.x 动作类归纳
- 测试博文1
- 17、Spring初始化Bean的三种方式
- 颜色调节中一种逆分析方法--直方图分析+直方图规划
- 测试博文2
- UBUNTU ifconfig只有lo
- java中的进度条的显示
- Android加载Html的方法
- 从零开始制作H5应用(2)——V2.0版,多页单张图片滑动,透明过渡及交互指示
- Git常用命令总结
- 五月再见,六月你好