File tree Expand file tree Collapse file tree
src/main/java/com/baeldung/beanfactory Expand file tree Collapse file tree Original file line number Diff line number Diff line change 5050 <artifactId >javax.inject</artifactId >
5151 <version >1</version >
5252 </dependency >
53+ <dependency >
54+ <groupId >junit</groupId >
55+ <artifactId >junit</artifactId >
56+ <version >4.12</version >
57+ </dependency >
5358 </dependencies >
5459
5560 <build >
Original file line number Diff line number Diff line change 11package com .baeldung .beanfactory ;
22
3+ import org .junit .Test ;
34import org .springframework .beans .factory .BeanFactory ;
45import org .springframework .beans .factory .xml .XmlBeanFactory ;
56import org .springframework .core .io .ClassPathResource ;
67import org .springframework .core .io .Resource ;
78
9+ import static org .junit .Assert .assertFalse ;
10+ import static org .junit .Assert .assertTrue ;
11+
812public class BeanFactoryWithClassPathResource {
913
14+ Resource res = new ClassPathResource ("spring-app.xml" );
15+ BeanFactory factory = new XmlBeanFactory (res );
16+
17+ @ Test
1018 public void createBeanFactory () {
1119
12- Resource res = new ClassPathResource ("spring-app.xml" );
13- BeanFactory factory = new XmlBeanFactory (res );
1420 Employee emp = (Employee ) factory .getBean ("employee" );
21+
22+ assertFalse (factory .isSingleton ("employee" ));
23+
24+ assertTrue (factory .getBean ("employee" ) instanceof Employee );
25+
26+ assertTrue (factory .isTypeMatch ("employee" , Employee .class ));
27+
28+ //we have empalias as an alias for employee
29+ assertTrue (factory .getAliases ("employee" ).length > 0 );
1530 }
1631}
Original file line number Diff line number Diff line change 88 <constructor-arg name =" name" value =" Hello! My name is Java" />
99 <constructor-arg name =" age" value =" 18" />
1010 </bean >
11+
12+ <alias name =" employee" alias =" empalias" />
1113</beans >
You can’t perform that action at this time.
0 commit comments