Webcam Chat QuickBooks Advice international calling cards international phone cards
JavaBeat Java Books Certifications Certifications Kits Articles Tutorials Tips QNA Book Store Interview Questions SCJP 1.5 SCJP 1.6 SCWCD 5.0 SCBCD 5.0 SCEA SCJA Feeds

Spring IOC Inject By ID

Topic :
Feedback Request New QnA Print Email

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

Topic :
Feedback Request New QnA Print Email

All api java java 6.0 java 7.0 jdbc jsp servlet ejb jndi jms ejb 3.0 j2ee jee 5.0 jee 6.0 jsf struts spring Hibernate ajax JBoss Seam netbeans eclipse ant xml maven dojo junit javafx j2me log4j ESB JBoss Apache Quartz scjp mysql oracle gwt openjpa jmx yui google-guice android JBoss scwcd 5.0 scjp 1.5 scjp 1.6 scja scbcd 5.0

JavaBeat Website (2004-2011), India
javabeat | advertise | about us | contact | useful resources
Copyright (2004 - 2011), JavaBeat


Technology Blogs
Technology blogs Technology Blogs
blog log