java bean 初始化_Spring Bean初始化的几种常规方式

本文详细介绍了Spring框架中实例化对象的四种常见方法:通过构造方法、静态工厂、实例工厂以及FactoryBean。通过具体代码示例展示了每种方式的使用和输出结果,帮助读者理解Spring中不同对象创建的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过构造方法实例化

通过静态工厂实例化

通过实例工厂实例化

通过FactoryBean实例化

RumenzA实体类

package com.rumenz;

public class RumenzA {

private String id;

private String name;

public static RumenzA createRumenzA(){

RumenzA rumenzA=new RumenzA();

rumenzA.setId("123");

rumenzA.setName("入门小站");

return rumenzA;

}

public RumenzA() {

System.out.println("RumenzA 无参构造方法");

}

public RumenzA(String id) {

this.id = id;

System.out.println("ID构造方法");

}

// set get省略

}

构造方法

beans.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

https://www.springframework.org/schema/beans/spring-beans.xsd">

DemoApplication.java

package com.rumenz;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoApplication {

public static void main(String[] args) {

ClassPathXmlApplicationContext ca=new ClassPathXmlApplicationContext("beans.xml");

RumenzA rumenzA=(RumenzA)ca.getBean("rumenz");

}

}

输出

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz'

RumenzA 无参构造方法

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz1'

ID构造方法

静态工厂

beans.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

https://www.springframework.org/schema/beans/spring-beans.xsd">

RumenzFactory工厂类

package com.rumenz;

public class RumenzFactory {

//静态方法

public static RumenzA rumenzFactory(){

return new RumenzA();

}

public static RumenzA rumenzFactory(String id){

return new RumenzA(id);

}

}

DemoApplication.java

package com.rumenz;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoApplication {

public static void main(String[] args) {

ClassPathXmlApplicationContext ca=new ClassPathXmlApplicationContext("beans.xml");

}

}

输出

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rFactory'

RumenzA 无参构造方法

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rFactory1'

ID构造方法

实例工厂实例化

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

https://www.springframework.org/schema/beans/spring-beans.xsd">

RumenzFactory.java

package com.rumenz;

public class RumenzFactory {

//不能用静态方法

public RumenzA rumenzFactory(){

return new RumenzA();

}

public RumenzA rumenzFactory(String id){

return new RumenzA(id);

}

}

DemoApplication.java

package com.rumenz;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoApplication {

public static void main(String[] args) {

ClassPathXmlApplicationContext ca=new ClassPathXmlApplicationContext("beans.xml");

//RumenzA rumenzA=(RumenzA)ca.getBean("rFactory1");

}

}

输出

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz'

RumenzA 无参构造方法

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz1'

ID构造方法

通过FactoryBean实例化

beans.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/beans

https://www.springframework.org/schema/beans/spring-beans.xsd">

RumenzAFactoryBean.java

package com.rumenz;

import org.springframework.beans.factory.FactoryBean;

public class RumenzAFactoryBean implements FactoryBean {

@Override

public Object getObject() throws Exception {

return RumenzA.createRumenzA();

}

@Override

public Class> getObjectType() {

return RumenzA.class;

}

}

DemoApplication.java

package com.rumenz;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoApplication {

public static void main(String[] args) {

ClassPathXmlApplicationContext ca=new ClassPathXmlApplicationContext("beans.xml");

RumenzA rumenzA=(RumenzA)ca.getBean("rumenz-by-factoryBean");

}

}

输出/异步加载bean

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz-by-factoryBean'

RumenzA 无参构造方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值