Java/Spring/IoC Injection
Содержание
Dependant Check
/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/
///////////////////////////////////////////////////////////////////////////////////////
//File: depcheck.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="simpleBean1" class="SimpleBean" dependency-check="simple">
<property name="someInt">
<value>16</value>
</property>
</bean>
<bean id="simpleBean2" class="SimpleBean" dependency-check="objects">
<property name="nestedSimpleBean">
<ref local="nestedSimpleBean"/>
</property>
</bean>
<bean id="simpleBean3" class="SimpleBean" dependency-check="all">
<property name="someInt">
<value>16</value>
</property>
</bean>
<bean id="nestedSimpleBean" class="SimpleBean"/>
</beans>
///////////////////////////////////////////////////////////////////////////////////////
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class SimpleBean {
private int someInt;
private SimpleBean nestedSimpleBean;
public void setSomeInt(int someInt) {
this.someInt = someInt;
}
public void setNestedSimpleBean(SimpleBean nestedSimpleBean) {
this.nestedSimpleBean = nestedSimpleBean;
}
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"build/depcheck.xml"));
SimpleBean simpleBean1 = (SimpleBean)factory.getBean("simpleBean1");
SimpleBean simpleBean2 = (SimpleBean)factory.getBean("simpleBean2");
SimpleBean simpleBean3 = (SimpleBean)factory.getBean("simpleBean3");
}
}
Hello World Xml With DI
/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/
//File: beans.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="provider" class="HelloWorldModel">
<constructor-arg>
<value>This is a configurable message</value>
</constructor-arg>
</bean>
<bean id="renderer" class="StandardOutView">
<property name="model">
<ref local="provider"/>
</property>
</bean>
</beans>
///////////////////////////////////////////////////////////////////////////////////////
public interface Model {
public String getMessage();
}
///////////////////////////////////////////////////////////////////////////////////////
public interface View {
public void render();
public void setModel(Model m);
public Model getModel();
}
///////////////////////////////////////////////////////////////////////////////////////
public class StandardOutView implements View {
private Model model = null;
public void render() {
if (model == null) {
throw new RuntimeException(
"You must set the property model of class:"
+ StandardOutView.class.getName());
}
System.out.println(model.getMessage());
}
public void setModel(Model m) {
this.model = m;
}
public Model getModel() {
return this.model;
}
}
///////////////////////////////////////////////////////////////////////////////////////
public class HelloWorldModel implements Model {
String mess;
public HelloWorldModel(String m){
mess = m;
}
public String getMessage() {
return mess;
}
}
///////////////////////////////////////////////////////////////////////////////////////
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class HelloWorldXmlWithDI {
public static void main(String[] args) throws Exception {
// get the bean factory
BeanFactory factory = getBeanFactory();
View mr = (View) factory.getBean("renderer");
mr.render();
}
private static BeanFactory getBeanFactory() throws Exception {
// get the bean factory
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"build/beans.xml"));
return factory;
}
}
Lookup Performance
/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/
///////////////////////////////////////////////////////////////////////////////////////
//File: lookup.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="helper" class="MyHelper" singleton="false"/>
<bean id="abstractLookupBean" class="AbstractLookupDemoBean">
<lookup-method name="getMyHelper" bean="helper"/>
</bean>
<bean id="standardLookupBean" class="StandardLookupDemoBean">
<property name="myHelper">
<ref local="helper"/>
</property>
</bean>
<bean id="factoryLookupBean" class="BeanFactoryAwareLookupDemoBean"/>
</beans>
///////////////////////////////////////////////////////////////////////////////////////
public interface DemoBean {
public MyHelper getMyHelper();
public void someOperation();
}
///////////////////////////////////////////////////////////////////////////////////////
public class MyHelper {
public void doSomethingHelpful() {
// do something!
}
}
///////////////////////////////////////////////////////////////////////////////////////
public class StandardLookupDemoBean implements DemoBean {
private MyHelper helper;
public void setMyHelper(MyHelper helper) {
this.helper = helper;
}
public MyHelper getMyHelper() {
return this.helper;
}
public void someOperation() {
helper.doSomethingHelpful();
}
}
///////////////////////////////////////////////////////////////////////////////////////
public abstract class AbstractLookupDemoBean implements DemoBean {
public abstract MyHelper getMyHelper();
public void someOperation() {
getMyHelper().doSomethingHelpful();
}
}
///////////////////////////////////////////////////////////////////////////////////////
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
public class BeanFactoryAwareLookupDemoBean implements BeanFactoryAware,
DemoBean {
private BeanFactory factory = null;
public void setBeanFactory(BeanFactory factory) throws BeansException {
this.factory = factory;
}
public MyHelper getMyHelper() {
return (MyHelper) factory.getBean("helper");
}
public void someOperation() {
getMyHelper().doSomethingHelpful();
}
}
///////////////////////////////////////////////////////////////////////////////////////
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
import org.springframework.util.StopWatch;
public class LookupPerformance {
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"build/lookup.xml"));
DemoBean abstractBean = (DemoBean) factory.getBean("abstractLookupBean");
DemoBean factoryBean = (DemoBean) factory.getBean("factoryLookupBean");
testPerf(abstractBean);
testPerf(factoryBean);
}
public static void testPerf(DemoBean bean) {
StopWatch stopWatch = new StopWatch();
stopWatch.start("perfTest");
for (int x = 0; x < 1000000; x++) {
MyHelper helper = bean.getMyHelper();
helper.doSomethingHelpful();
}
stopWatch.stop();
System.out.println("1000000 gets took "
+ stopWatch.getTotalTimeSeconds() + " seconds");
}
}
Model View Injection in Spring
/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/
///////////////////////////////////////////////////////////////////////////////////////
//File: beans.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="provider" class="HelloWorldModel">
<constructor-arg>
<value>This is a configurable message</value>
</constructor-arg>
</bean>
<bean id="renderer" class="StandardOutView">
<property name="model">
<ref local="provider"/>
</property>
</bean>
</beans>
///////////////////////////////////////////////////////////////////////////////////////
public interface View {
public void render();
public void setModel(Model m);
public Model getModel();
}
///////////////////////////////////////////////////////////////////////////////////////
public interface Model {
public String getMessage();
}
///////////////////////////////////////////////////////////////////////////////////////
public class StandardOutView implements View {
private Model model = null;
public void render() {
if (model == null) {
throw new RuntimeException(
"You must set the property model of class:"
+ StandardOutView.class.getName());
}
System.out.println(model.getMessage());
}
public void setModel(Model m) {
this.model = m;
}
public Model getModel() {
return this.model;
}
}
///////////////////////////////////////////////////////////////////////////////////////
public class HelloWorldModel implements Model {
String mess;
public HelloWorldModel(String m){
mess = m;
}
public String getMessage() {
return mess;
}
}
///////////////////////////////////////////////////////////////////////////////////////
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class HelloWorldXml {
public static void main(String[] args) throws Exception {
// get the bean factory
BeanFactory factory = getBeanFactory();
View mr = (View) factory.getBean("renderer");
Model mp = (Model) factory.getBean("provider");
mr.setModel(mp);
mr.render();
}
private static BeanFactory getBeanFactory() throws Exception {
// get the bean factory
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"build/beans.xml"));
return factory;
}
}
Simple Inject By ID
/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/
///////////////////////////////////////////////////////////////////////////////////////
//File: beans.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- injecting built-in vals sample -->
<bean id="injectSimple" class="InjectSimple">
<property name="name">
<value>John Smith</value>
</property>
<property name="age">
<value>35</value>
</property>
<property name="height">
<value>1.78</value>
</property>
<property name="isProgrammer">
<value>true</value>
</property>
<property name="ageInSeconds">
<value>1103760000</value>
</property>
</bean>
<!-- oracle bean used for a few examples -->
<bean id="oracle" name="wiseworm" class="BookwormOracle"/>
<!-- injecting reference sample (using id) -->
<bean id="injectRef" class="InjectRef">
<property name="oracle">
<ref local="oracle"/>
</property>
</bean>
<!-- inject ref sample (using name) -->
<!-- <bean id="injectRef" class="InjectRef">
<property name="oracle">
<ref bean="wiseworm"/>
</property>
</bean> -->
</beans>
///////////////////////////////////////////////////////////////////////////////////////
public interface Oracle {
public String defineMeaningOfLife();
}
///////////////////////////////////////////////////////////////////////////////////////
public class InjectRef {
private Oracle oracle;
public void setOracle(Oracle oracle) {
this.oracle = oracle;
System.out.println(oracle.defineMeaningOfLife());
}
}
///////////////////////////////////////////////////////////////////////////////////////
public class BookwormOracle implements Oracle {
private Encyclopedia enc;
public void setEncyclopedia(Encyclopedia enc) {
this.enc = enc;
}
public String defineMeaningOfLife() {
return "Encyclopedia"s are a waste of money - use the Internet";
}
}
///////////////////////////////////////////////////////////////////////////////////////
public class Encyclopedia {
}
///////////////////////////////////////////////////////////////////////////////////////
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class InjectSimple {
private String name;
private int age;
private float height;
private boolean isProgrammer;
private Long ageInSeconds;
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"build/beans.xml"));
InjectSimple simple = (InjectSimple)factory.getBean("injectSimple");
factory.getBean("injectRef");
System.out.println(simple);
}
public void setAgeInSeconds(Long ageInSeconds) {
this.ageInSeconds = ageInSeconds;
}
public void setIsProgrammer(boolean isProgrammer) {
this.isProgrammer = isProgrammer;
}
public void setAge(int age) {
this.age = age;
}
public void setHeight(float height) {
this.height = height;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "Name :" + name + "\n"
+ "Age:" + age + "\n"
+ "Age in Seconds: " + ageInSeconds + "\n"
+ "Height: " + height + "\n"
+ "Is Programmer?: " + isProgrammer;
}
}
Simple Inject By Name
/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/
///////////////////////////////////////////////////////////////////////////////////////
//File: beans.xml
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- injecting built-in vals sample -->
<bean id="injectSimple" class="InjectSimple">
<property name="name">
<value>John Smith</value>
</property>
<property name="age">
<value>35</value>
</property>
<property name="height">
<value>1.78</value>
</property>
<property name="isProgrammer">
<value>true</value>
</property>
<property name="ageInSeconds">
<value>1103760000</value>
</property>
</bean>
<!-- oracle bean used for a few examples -->
<bean id="oracle" name="wiseworm" class="BookwormOracle"/>
<!-- injecting reference sample (using id)
<bean id="injectRef" class="InjectRef">
<property name="oracle">
<ref local="oracle"/>
</property>
</bean>
-->
<!-- inject ref sample (using name) -->
<bean id="injectRef" class="InjectRef">
<property name="oracle">
<ref bean="wiseworm"/>
</property>
</bean>
</beans>
///////////////////////////////////////////////////////////////////////////////////////
public interface Oracle {
public String defineMeaningOfLife();
}
///////////////////////////////////////////////////////////////////////////////////////
public class BookwormOracle implements Oracle {
private Encyclopedia enc;
public void setEncyclopedia(Encyclopedia enc) {
this.enc = enc;
}
public String defineMeaningOfLife() {
return "Encyclopedia"s are a waste of money - use the Internet";
}
}
///////////////////////////////////////////////////////////////////////////////////////
public class Encyclopedia {
}
///////////////////////////////////////////////////////////////////////////////////////
public class InjectRef {
private Oracle oracle;
public void setOracle(Oracle oracle) {
this.oracle = oracle;
System.out.println(oracle.defineMeaningOfLife());
}
}
///////////////////////////////////////////////////////////////////////////////////////
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class InjectSimple {
private String name;
private int age;
private float height;
private boolean isProgrammer;
private Long ageInSeconds;
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"build/beans.xml"));
InjectSimple simple = (InjectSimple)factory.getBean("injectSimple");
factory.getBean("injectRef");
System.out.println(simple);
}
public void setAgeInSeconds(Long ageInSeconds) {
this.ageInSeconds = ageInSeconds;
}
public void setIsProgrammer(boolean isProgrammer) {
this.isProgrammer = isProgrammer;
}
public void setAge(int age) {
this.age = age;
}
public void setHeight(float height) {
this.height = height;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "Name :" + name + "\n"
+ "Age:" + age + "\n"
+ "Age in Seconds: " + ageInSeconds + "\n"
+ "Height: " + height + "\n"
+ "Is Programmer?: " + isProgrammer;
}
}