Spring Bean初始化的几种常规方式

云栖号资讯:【点击查看更多行业资讯】
在这里您可以找到不同行业的第一手的上云资讯,还在等什么,快来!


  • 通过构造方法实例化
  • 通过静态工厂实例化
  • 通过实例工厂实例化
  • 通过FactoryBean实例化

RumenzA实体类

package com.rumenz;
publ6 w 0ic class Ruk H {menzA {
private St6 V c $ z Xring id;
private String name;
publici (  static RumenzA createRumenzA(){
RumenzA rumenzA=new RumenzA();
rumenzA.setId("123");
ruJ ; v u j J n omenzA.setNam8 U Q #e("入门小站 O J _");
retur: ! [n rumenzA;
}
public  RL l k u qumenzA() {
System.out.println("RumenzA 无参构造方法");
}
public RumenzA(String id) {
this.id = id;
System.out.println("ID构造方法");
}
// set get省略
}

构造方法

beans.xml
<?xml version=q ` F a 3 Q / & D8 , j"1.0" encoding="UTF-8"?>
<beans xmlns="htL % c f ptp://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-insr g ptance"
xs: 5 J ; 9 H - ~ Zi:schema` r ( ] ALocation=V . v y ` 1 2"http://www.springframework.org: Q * 1 k ? a )/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="rumenz" class="com.rumenz.RumenzA" /&g) @ L C f w v U `t;
<b( g t 2 Yean id="rumenz1" class="com.rumenz.RumenzA">
<constructor-arg name="id" value="1"/>
<k } d x Y z W i;/be0 = Xan>
</beans>

DemoApplication.jal f H va

package com.rumenz;
import o~ y # grg.springframework.context.support.ClassPathXmlApplicationContext;
public class DemoAppli-  7cation {
public static  vo& w & { J vid mai5 f D ! J + $ 2n(String[] args) {
ClassPathXmlAppb q vlicationContext  ca=new ClassPathXmlApplicationContext("beans.xml");
RumenzA rumenzA=(RumenzA)ca.getBean("rumenz");
}
}

输出

xxx.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz'
RumenzA 无参构造方法
xxx.DefaultListableBeanFactory - Cre3 j H m X ?ating shared instance of singleton bean 'rumenz1'
ID构造方法

静态工厂

beanK h ( @ j N V Q `s.xml
<?xml version="1.0" encoh s $ B Q R b Ading="UTF-8"?>
<beans xmlns="http://* | 2 T Z C 7 )www.springframework.org/schema/beans"
xmlns:xsi="htt5 2 % ?p://www.w3.org/2001/XMLSchema-instance; % ` 1"
xsi:schemaLocatiT e V  _ ,on="http://www.springframework.o# ? ` Grg/schema/beans
https% 2 w N s j://www.springframework.org/schema/beans/spring-beans.xsd"&gz G i f g p o $t;
<bean id="rFactory" class="com.rumenz.Rz A $ x eumenzFactory" factory-mG 8 l - @ y Z r pethod="rumenzFactory"/>
<bean id="rFactory1" class="com.rumenz.RumenzFactory" factory-method="rumenzFactory">
<constructor-aF R ^ { *rg name="id" value="111"/>
</bean>
</b4 z :  e ] ieans>

RumenzFactory工厂类

package com.rumenz;
public class RumenzFactory {
//静p @ ^ E |态方法
public static RumenzA rumenzFactory()I u 5 J 4 R{
returB ; Tn new RumenzA();
}
public static RumenzA r^ 9 5 Q B { ` 8 JumenzF/ w # y Y D 6 } tactory(String id){
return new Rumenz7 o * j L : ] .A(id);
}
}

DemoApplication.java

package com.rumenz;
import org.springH x xframework.cc B k q a .ontext.support.ClassPathXmlApplicationContext;
public class DemoApplication {
public static  void main(String[] args) {
ClassPathXmlApplicationContext  ca=new ClassPathXmlApplicationContext("beans.xml");
}
}

输出

xxx.DefaultListableR x ] V R * ! +BeanFactory - Creating shared instance of singleton bean 'rFactory'
RumenzA 无参构造方法
xxx.DefaultListableBeanFactory - Creating shared instance oH C 4 z gf singleton beanT ^ v ; / 3 l y  'rFactory1'g } 4 f l 8 o 8 x
ID构造方法

实例工厂实例化

<?xml version="1.0" ee i # , & Fncoding="UTF-8"?>
<beans xmln H 1 ^ `  a @s="h` W g ] = t - % &t4 5 Z , .tp://www.springframework.org/schema/beansz H S"
xmlns:xsiB # D R +=O D 0 g / = c h !"http://www.w3.org/2001/XMLSchema-instance"
xsi:sch( K h ] lemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schev e ! ^ c F A Oma/beans/spring-beans.xJ i P r ] r dsd">
<bean  id="rFactory" class="com 8 t B | D r $ Hm.rumenzj U s Y g ; .RumenzFactory" />
<bean id="rumenz" factory-bean="rFao j dctory" factory-method="rumenzFactory"></bean>
<bean id="rumenz1" faN Q 9 D w 0 . l sctory-bean="rFactory" factory-method="rumenzFactory">
<constructor-arg name="id" value="666"></constructor-arg>
</bean>
</beo @ y o r ^ s Ians>

RumenzFactory.jaL 7 l z eva

pI G Nackage com.rumenz;
public class Rumed P y _nzFa[ $ W w H K 8 T fctory {
//不能用静态方法
public  RumenzA rumenzFaP I D & kctory(){
return ne| ; F U h y _w RumenzA();
}
public  RumenzA rumenzFacb ~ g Z e ! Z dtory(String id){
return new RumenzA(id);
}
}

DemoApplication.java

package com.rumenz;
import org.springf) ! 8 1 H O Uramework.context.support.ClassPathXmlApplicationCoF ] r U b x $ntext;
public class DemoApplicationB  6 X y {
public static  void main(String[] args) {
ClassPathXmlApplicationContv = $ ) { ^ =ext o p  ca=new ClassPathXmlApplicatio- d i p ] ?nQ . Context("beans.xml");
//RumenzA rumenzA=(RumenzA)ca.getBean("rFactory1");
}
}

输出

xxxz - k h B.DefaultListableBeanFactory - Creating shared instance of singleton bean 'rumenz'
RumenzA 无参构造方法
xxx.DefaultListableBeanFactory - Creating shared inZ j h L m b Wstance of singleton bean 'rumenz1'
ID构造方法

通过( a CFo O O i k t E $actoryBean实例化

beans.xml
<?xml version="1.0" encodii l ~ = ,ng="UTF-8"?>
<beaG v T 8 -ns xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.? x S u o 9 / L aw3.org/2001/XMLSchema-instance"
xsi:schemaLocat*  cion="http://T l : ; % , x ? vww5 ~ 3w.springframp 9 V [ A @ fework.org/schema/beans
https://www.sprin~ X % # 1gframework.org/schema/beans/spring-beans.xsd"&V 0 h  ~ = ? Mgo u H [ = 6 J zt;
<bean id="rumenz-by-factoryBean"  class="co@ a  9 #m.rumenz.Rum2 h * 1 :enzAFactoryBean"/>
&$ G , Dlt;/bZ [ y eans>

Ru` | + ` UmenzAF= g I x S p -actoryBean.java

package com.rumenz;
import org.springframework.beans.factory.FactoryBean;
public class RumenzAFactoryBean implements FactoryBean {
@Override
publiL M fc Object getObject() throws Exception {
return R7 R G Q . PumenzA.createRumenzA();
}
@Override
public Class<?> getObjectType() {
return RumenzA.class;
}
}

DemoApplication.jP ; P W F B R ; |ava

package com.rumenz;
import org.springframework.context.support.ClassPatz * ^hXmlAppli8 I y B d = I Qcationf x J l V Q ! T 5Context;
publ: E m t * Eic class DemoApplication {
public static  void main(String[] arO r h E ` { T Fgs) {
ClassPathXmlApplicatG W Q K }ionContext  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 无参构造方法

【云栖号在线课堂】每天都有产品技术专家分享!
课程地址:https://yqh.aliyun.com/N 8 T . glive

立即加入社群,与专家0 q J w面对面,及时了解课程最新动态!
【云栖号在线课堂 社群】https://c.tb.cn/F3.Z8gvnK

原文发布时间:2020-06-28
本文作者:入门小站
本文来自:“掘金”,了解相关信息可以关注“掘金”