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 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()); } }
<?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; } }
<?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(); } }
<?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()); } }
<?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