File: context.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="target1" class="SimpleBean"
dependency-check="simple">
<property name="someInt" value="1"/>
</bean>
<bean id="target2" class="SimpleBean"
dependency-check="objects">
<property name="nestedSimpleBean" ref="nestedSimpleBean"/>
</bean>
<bean id="target3" class="SimpleBean"
dependency-check="all">
<property name="nestedSimpleBean" ref="nestedSimpleBean"/>
<property name="someInt" value="1"/>
</bean>
<bean id="nestedSimpleBean" class="SimpleBean"/>
</beans>
File: Main.java
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
public static void main(String[] args) {
XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("context.xml"));
System.out.println(bf.getBean("target1"));
System.out.println(bf.getBean("target2"));
System.out.println(bf.getBean("target3"));
}
}
class SimpleBean {
private int someInt;
private SimpleBean nestedSimpleBean;
public void setSomeInt(int someInt) {
this.someInt = someInt;
}
public void setNestedSimpleBean(SimpleBean nestedSimpleBean) {
this.nestedSimpleBean = nestedSimpleBean;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append("SimpleBean");
sb.append("{someInt=").append(someInt);
sb.append(", nestedSimpleBean=").append(nestedSimpleBean);
sb.append('}');
return sb.toString();
}
}
|