乐趣区

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;}
}

手动输出形式

  • XML 资源配置元信息

配置 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());

    }


}

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

退出移动版