荣耀彩票代理

  • 热门专题

Spring中你不知道的注入方式

作者:  发布日期:2016-07-22 21:52:29
  • ZAISpringPEIZHIWENJIANZHONGSHIYONGXMLWENJIANJINXINGPEIZHI,SHIJISHANGSHIRANGSpringZHIXINGLEXIANGYINGDEDAIMA,LIRU:

    使用元素,实际上是让Spring执行无参或有参构造器使用元素,实际上是让Spring执行一次setter方法

    DANJavaCHENGXUHAIKENENGYOUQITALEIXINGDEYUJU:DIAOYONGgetterFANGFA、DIAOYONGPUTONGFANGFA、FANGWENLEIHUODUIXIANGDEFieldDENG,ERSpringYEWEIZHEIZHONGYUJUTIGONGLEDUIYINGDEPEIZHIYUFA:

    调用getter方法:使用PropertyPathFactoryBean调用类或对象的Filed值:使用FiledRetrievingFactoryBean调用普通方法:使用MethodInvokingFactoryBean

    注入其他Bean的属性值

    PropertyPathFactoryBeanYONGLAIHUODEMUBIAOBeanDESHUXINGZHI(SHIJISHANGJIUSHIDIAOYONGgetterFANGFAFANHUIDEZHI),HUODEDEZHIKEYIZHURUJIQITADEBean,YEKEYIZHIJIEDINGYIXINDEBean。KANRUXIADEPEIZHIWENJIAN:

    <bean id='person' class='com.abc.Person'>
        <property name='age' value='30' />
        <property name='son'>
            
            <bean class='com.abc.service.Son'>
                <property name='age' value='11' />
            bean>
        property>
    bean>
    
    <bean id='son2' class='com.abc.service.Son'>
        
        <property name='age'>
            
            <bean id='person.son.age' 
                class='org.springframework.beans.factory.config.PropertyPathFactoryBean' />
        property>
    bean>

    荣耀彩票代理QIZHONGPersonLEIHESonLEIDESHUXINGKEYICONGPEIZHIWENJIANZHONGKANCHU,ZHEIBUZAIJICHU。ZHUCHENGXURUXIA:

    public class Test {
        public static void main(String args[]) {
            ApplicationContext ac = 
                new ClassPathXmlApplicationContext('applicationContext.xml');
            System.out.println('age=' + ac.getBean('son2', Son.class).getAge());
        }
    }

    SHUCHUJIEGUO:

    age=11

    BeanSHILIDESHUXINGZHI,BUJINKEYIZHURULINGYIGEBean,HAIKEJIANGBeanSHILIDESHUXINGZHIZHIJIEDINGYICHENGBeanSHILI,ZHEIYESHITONGGUOPropertyPathFactoryBeanWANCHENGDE。DUISHANGMIANDEPEIZHIWENJIANZENGJIAZHEIYANGYIDUAN:

    <bean id='son1' 
        class='org.springframework.beans.factory.config.PropertyPathFactoryBean'>
        
        <property name='targetBeanName' value='person' />
        
        <property name='propertyPath' value='son' />
    bean>

    ZHIXINGSHANGMIANDETestLEI,BAson2HUANCHENGson1,JIEGUOYIYANG。

    注入其他Bean的Field值

    TONGGUOFieldRetrievingFactoryBeanLEI,KEYIJIANGQITABeanDEFieldZHIZHURUJIQITABean,HUOZHEZHIJIEDINGYIXINDEBean。XIAMIANSHIPEIZHIPIANDUAN:

    <bean id='son' class='com.abc.service.Son'>
        <property name='age'>
            <bean id='java.sql.connection.TRANSACTION_SERIALIZABLE'
                class='org.springframework.beans.factory.config.FieldRetrievingFactoryBean' />
        property>
    bean>

    CESHIZHUCHENGXUYUSHANGWENDINGYIDELEISI,ZHEILIBUZAITIGONG,ZHIXINGJIEGUORUXIA:

    age=8

    荣耀彩票代理ZAIZHEIGEPEIZHIZHONG,sonDUIXIANGDEageDEZHI,DENGYUjava.sql.Connection.TRANSACTION_SERIALIZABLEDE ZHI。ZAISHANGMIANDEDINGYIZHONG,DINGYIFieldRetrievingFactoryBeanGONGCHANGBeanSHI,ZHIDINGDEidBINGBUSHIGAIBeanSHILIDEWEIYIBIAOSHI,ERSHIZHIDING FieldDEBIAODASHI(JIJIANGYAOBEIQUCHULAIDEZHI)。

    荣耀彩票代理ZHUYI:FieldJIKEYISHIJINGTAIDE,YEKEYISHIFEIJINGTAIDE。SHANGMIANDEPEIZHIPIANDUANZHIDINGDEFieldBIAODASHISHIJINGTAIFieldZHI,YINCIKEYITONGGUOLEIMINGZHIJIEFANGWEN。RU GUOFieldZHISHIFEIJINGTAIDE,ZEYINGGAITONGGUORONGQIZHONGYIJINGCUNZAIDEBeanLAIFANGWEN——JIFieldBIAODASHIDEDIYIGEDUANYUYINGGAISHIRONGQIZHONGYIJINGCUNZAIDEBean。

    FieldZHIYEKEYIDINGYICHENGBeanSHILI,LIRU,ZAIPEIZHIWENJIANZHONGZENGJIAXIAMIANYIDUAN:

    <bean id='age' 
        class='org.springframework.beans.factory.config.FieldRetrievingFactoryBean'>
        
        <property name='targetClass' value='java.sql.Connection' />
        
        <property name='targetField' value='TRANSACTION_SERIALIZABLE' />
    bean>

    荣耀彩票代理ZAIZHUCHENGXUZHONGZENGJIARUXIASHUCHU:

    System.out.println('age=' + ac.getBean('age'));

    ZHIXINGJIEGUOHESHANGWENYIYANG。

    荣耀彩票代理SHIYONGFieldRetrievingFactoryBeanHUOQUFieldZHISHI,BIXUZHIDINGRUXIALIANGGESHUXING:

    targetClass或targetObject:分别用于指定Field值所在的目标累或目标对象。如果需要获得的Field是静态的,则使用targetClass指定目标累;如果Field是非静态的,则使用targetObject指定目标对象targetField:指定目标类或目标对象的Field名

    RUGUOFieldSHIGEJINGTAIField,ZEYOUYIZHONGGENGJIAJIANJIEDEXIEFA:

    <bean id='age' 
        class='org.springframework.beans.factory.config.FieldRetrievingFactoryBean'>
        
        <property name='staticField' value='java.sql.Connection.TRANSACTION_SERIALIZABLE' />
    bean>

    注入其他Bean的方法返回值

    TONGGUOMethodInvokingFactoryBeanGONGCHANGBean,KEJIANGMUBIAOFANGFADEFANHUIZHIZHURUWEIBeanDESHUXINGZHI。ZHEIGEGONGCHANGBeanYONGLAIHUO QUZHIDINGFANGFADEFANHUIZHI,GAIFANGFAJIKEYISHIJINGTAIFANGFA,YEKEYISHISHILIFANGFA;ZHEIGEZHIJIKEYIBEIZHURUDAOZHIDINGBeanSHILIDEZHIDINGSHUXING,YEKEYIZHIJIEDINGYICHENGBeanSHILI。KANLI ZI:

    <bean id='valueGenerator' class='com.abc.util.ValueGenerator' />
    <bean id='son1' class='com.abc.service.Son'>
        <property name='age'>
            
            <bean 
                class='org.springframework.beans.factory.config.MethodInvokingFactoryBean'>
                <property name='targetObject' ref='valueGenerator' />
                <property name='targetMethod' value='getValue' />
            bean>
        property>
    bean>

    XIAMIANSHIValueGenerator:

    public class ValueGenerator {
        public int getValue() { return 2; }
        public static int getStaticValue () { return 3;}
    }

    荣耀彩票代理CESHICHENGXUYIJIUDAYINson1ZHONGageDEZHI,DAIMALVE,JIEGUORUXIA:

    age=2

    荣耀彩票代理RUGUOYAODIAOYONGJINGTAIFANGFA,ZEBAPEIZHIXIUGAIWEI:

    <bean id='son1' class='com.abc.service.Son'>
        <property name='age'>
            
            <bean 
                class='org.springframework.beans.factory.config.MethodInvokingFactoryBean'>
                <property name='targetClass' value='com.abc.util.ValueGenerator' />
                <property name='targetMethod' value='getStaticValue' />
            bean>
        property>
    bean>

    CESHIJIEGUOWEI:

    age=3

    YOUYUJavaSHIZHICHIZHONGZAIDE,ZHIJIDINGFANGFAMING,HAIBUZUYINENGGOUQUEDINGDIAOYONGNAGEFANGFA,TONGGUOSHANGMIANDEPEIZHINENGDIAOYONGCHENGGONGSHIYINWEIValueGeneratorZHONGDELIANGGEFANGFADOUMEIYOUCANSHU。RUGUOFANGFAZHONGYOUCANSHU,GAIRUHEPEIZHINI?ZAIPEIZHIWENJIANZHONGJIARUYIXIANEIRONG:

    <bean id='sysProps' 
        class='org.springframework.beans.factory.config.MethodInvokingFactoryBean'>
        <property name='targetClass' value='java.lang.System' />
        <property name='targetMethod' value='getProperties' />
    <bean>
    <bean id='javaVersion' class='org.springframework.beans.factory.config.MethodInvokingFactoryBean'>
        
        <property name='targetObject' value='sysProps' />
        <property name='targetMethod' value='getProperty' />
        
        <property name='arguments'>
            
            <list>
                <value>java.versionvalue>
            list>
        property>
    <bean>

    荣耀彩票代理SHANGLIZHONGXIANGDANGYUYONG”java.version”ZUOWEICANSHUDIAOYONGLEjava.lang.SystemDEgetPropertyFANGFA,FANHUIZHIJIANGCHUANGJIANYIGEMINGWEIjavaVersionDEBean。JIXIANGDANGYU:

    javaVersion = java.lang.System.getProperty('java.version');

    荣耀彩票代理HEQIANWENZHONGDEFieldYIYANG,RUGUOYAODIAOYONGDEFANGFAWEIJINGTAIFANGFA,YEYOUYIZHONGGENGJIAJIANJIEDEFANGFA:

    <bean id='myBean'
        class='org.springframework.beans.factory.config.MethodInvokingFactoryBean'>
        
        <property name='staticMethod' value='com.abc.util.ValueGenerator.getStaticValue' />
    bean>

延伸阅读:

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规