Spring Setter方法注入

Setter方法输入

实体类Rumenz.java/Holder.java

package com.rumenz;

public class Rumenz {

    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


package com.rumenz;

public class Holder {

    private Rumenz rumenz;
    public Rumenz getRumenz() {
        return rumenz;
    }

    public void setRumenz(Rumenz rumenz) {
        this.rumenz = rumenz;
    }
}

手动输入方式

配置Beans.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">


    <bean id="rumenz" class="com.rumenz.Rumenz">
        <property name="id" value="123"/>
        <property name="name" value="入门小站"/>
    </bean>
    <bean id="holder" class="com.rumenz.Holder">
        <property name="rumenz" ref="rumenz"/>
    </bean>
</beans>

调用

package com.rumenz;
public class DemoApplication {

    public static void main(String[] args) {
        DefaultListableBeanFactory db=new DefaultListableBeanFactory();
        XmlBeanDefinitionReader xr=new XmlBeanDefinitionReader(db);
        xr.loadBeanDefinitions("Beans.xml");

        Holder bean = db.getBean(Holder.class);

        System.out.println(bean.getRumenz().getName());
    }
}
  • Java 注解配置元信息

配置文件Beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">


    <bean id="rumenz" class="com.rumenz.Rumenz">
        <property name="id" value="123"/>
        <property name="name" value="入门小站"/>
    </bean>
</beans>

调用

package com.rumenz;


import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

public class DemoApplication {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();

        XmlBeanDefinitionReader xr=new XmlBeanDefinitionReader(ac);

        xr.loadBeanDefinitions("Beans.xml");
        ac.register(DemoApplication.class);
        ac.refresh();
        Holder bean = ac.getBean(Holder.class);
        System.out.println(bean.getRumenz().getName());
        ac.close();
    }

    @Bean
    public Holder holder(Rumenz rumenz){
        Holder h=new Holder();
        h.setRumenz(rumenz);
        return h;
    }


}
  • API 配置元信息

配置文件Beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">


    <bean id="rumenz" class="com.rumenz.Rumenz">
        <property name="id" value="123"/>
        <property name="name" value="入门小站"/>
    </bean>
</beans>

调用

package com.rumenz;

ramework.context.annotation.Bean;

public class DemoApplication {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();


        BeanDefinition beanDefinition=createBeanDefinition();

        ac.registerBeanDefinition("holder",beanDefinition);

        XmlBeanDefinitionReader xr=new XmlBeanDefinitionReader(ac);

        xr.loadBeanDefinitions("Beans.xml");
        ac.register(DemoApplication.class);
        ac.refresh();
        Holder bean = ac.getBean(Holder.class);
        System.out.println(bean.getRumenz().getName());
        ac.close();

    }

    private static BeanDefinition createBeanDefinition() {
        BeanDefinitionBuilder beanDefinitionBuilder=BeanDefinitionBuilder.genericBeanDefinition(Holder.class);
        beanDefinitionBuilder.addPropertyReference("rumenz","rumenz");
        return beanDefinitionBuilder.getBeanDefinition();
    }


}

自动模式注入

  • byType

配置文件Beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">


    <bean id="rumenz" class="com.rumenz.Rumenz">
        <property name="id" value="123"/>
        <property name="name" value="入门小站"/>
    </bean>
    <bean id="holder" class="com.rumenz.Holder" autowire="byType">
    </bean>
</beans>

调用

package com.rumenz;

public class DemoApplication {

    public static void main(String[] args) {
        DefaultListableBeanFactory db=new DefaultListableBeanFactory();
        XmlBeanDefinitionReader xr=new XmlBeanDefinitionReader(db);
        xr.loadBeanDefinitions("Beans.xml");
        Holder bean = db.getBean(Holder.class);

        System.out.println(bean.getRumenz().getName());

    }


}
  • byName

配置文件Beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">


    <bean id="rumenz" class="com.rumenz.Rumenz">
        <property name="id" value="123"/>
        <property name="name" value="入门小站"/>
    </bean>
    <bean id="holder" class="com.rumenz.Holder" autowire="byName">
    </bean>
</beans>

调用

package com.rumenz;

public class DemoApplication {

    public static void main(String[] args) {
        DefaultListableBeanFactory db=new DefaultListableBeanFactory();
        XmlBeanDefinitionReader xr=new XmlBeanDefinitionReader(db);
        xr.loadBeanDefinitions("Beans.xml");
        Holder bean = db.getBean(Holder.class);

        System.out.println(bean.getRumenz().getName());

    }


}

Spring Setter方法注入

原文: https://rumenz.com/rumenbiji/spring-setter-injection.html

原文 

https://segmentfault.com/a/1190000023265401

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » Spring Setter方法注入

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址