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文件
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">
调用
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
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">
调用
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
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">
调用
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
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">
调用
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
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">
调用
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());
}
}