Java/Spring/IoC Bean Name
Bean Name Aliasing
/*
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>
<!-- aliasing examples -->
<bean id="name1" name="name2,name3,name4" class="java.lang.String"/>
</beans>
//////////////////////////////////////////////////////////////////////////////
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class BeanNameAliasing {
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource("build/beans.xml"));
String s1 = (String)factory.getBean("name1");
String s2 = (String)factory.getBean("name2");
String s3 = (String)factory.getBean("name3");
String s4 = (String)factory.getBean("name4");
System.out.println((s1 == s2));
System.out.println((s2 == s3));
System.out.println((s3 == s4));
String[] x = factory.getAliases("name3");
System.out.println("");
}
}
Bean Name Example
/*
Pro Spring
By Rob Harrop
Jan Machacek
ISBN: 1-59059-461-4
Publisher: Apress
*/
/////////////////////////////////////////////////////////////////////////////////////
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="fooBean" class="AutoBean"/>
<bean id="barBean" class="AutoBean"/>
</beans>
/////////////////////////////////////////////////////////////////////////////////////
public class AutoBean {
public void foo() {
System.out.println("foo()");
}
}
/////////////////////////////////////////////////////////////////////////////////////
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class BeanNameExample {
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"build/bnapc.xml");
AutoBean fooBean = (AutoBean)ctx.getBean("fooBean");
AutoBean barBean = (AutoBean)ctx.getBean("barBean");
fooBean.foo();
barBean.foo();
}
}
Define Bean 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>
<!-- naming examples -->
<bean id="string1" class="java.lang.String"/>
<bean name="string2" class="java.lang.String"/>
<bean class="java.lang.String"/>
</beans>
///////////////////////////////////////////////////////////////////////////////
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class BeanNaming {
public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"build/beans.xml"));
String s1 = (String)factory.getBean("string1");
String s2 = (String)factory.getBean("string2");
String s3 = (String)factory.getBean("java.lang.String");
}
}