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 @@ -5,8 +5,8 @@ RemoteSystemsTempFiles/
55bin /
66.metadata /
77docs /* .autosave
8- docs /* .autosave
98.recommenders /
109build /
1110.gradle /
1211.DS_Store
12+ .idea /
Original file line number Diff line number Diff line change 1414 </description >
1515
1616 <dependencies >
17- <dependency >
18- <groupId >junit</groupId >
19- <artifactId >junit</artifactId >
20- <version >4.11</version >
21- <scope >test</scope >
22- </dependency >
2317 <dependency >
2418 <groupId >org.mockito</groupId >
2519 <artifactId >mockito-all</artifactId >
5044 <artifactId >javax.inject</artifactId >
5145 <version >1</version >
5246 </dependency >
47+ <dependency >
48+ <groupId >junit</groupId >
49+ <artifactId >junit</artifactId >
50+ <version >4.12</version >
51+ </dependency >
5352 </dependencies >
5453
5554 <build >
Original file line number Diff line number Diff line change 1+ package com .baeldung .beanfactory ;
2+
3+ import org .junit .Test ;
4+ import org .springframework .beans .factory .BeanFactory ;
5+ import org .springframework .beans .factory .xml .XmlBeanFactory ;
6+ import org .springframework .core .io .ClassPathResource ;
7+ import org .springframework .core .io .Resource ;
8+
9+ import static org .junit .Assert .assertFalse ;
10+ import static org .junit .Assert .assertTrue ;
11+
12+ public class BeanFactoryWithClassPathResource {
13+
14+ @ Test
15+ public void createBeanFactoryAndCheckEmployeeBean () {
16+
17+ Resource res = new ClassPathResource ("spring-app.xml" );
18+ BeanFactory factory = new XmlBeanFactory (res );
19+ Employee emp = (Employee ) factory .getBean ("employee" );
20+
21+ assertFalse (factory .isSingleton ("employee" ));
22+ assertTrue (factory .getBean ("employee" ) instanceof Employee );
23+ assertTrue (factory .isTypeMatch ("employee" , Employee .class ));
24+ assertTrue (factory .getAliases ("employee" ).length > 0 );
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beanfactory ;
2+
3+ public class Employee {
4+
5+ private String name ;
6+ private int age ;
7+
8+ public Employee (String name , int age ) {
9+ this .name = name ;
10+ this .age = age ;
11+ }
12+
13+ public String getName () {
14+ return name ;
15+ }
16+
17+ public void setName (String name ) {
18+ this .name = name ;
19+ }
20+
21+ public int getAge () {
22+ return age ;
23+ }
24+
25+ public void setAge (int age ) {
26+ this .age = age ;
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <beans xmlns =" http://www.springframework.org/schema/beans"
3+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
4+ xsi : schemaLocation =" http://www.springframework.org/schema/beans
5+ http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" >
6+
7+ <bean id =" employee" class =" com.baeldung.beanfactory.Employee" >
8+ <constructor-arg name =" name" value =" Hello! My name is Java" />
9+ <constructor-arg name =" age" value =" 18" />
10+ </bean >
11+
12+ <alias name =" employee" alias =" empalias" />
13+ </beans >
You can’t perform that action at this time.
0 commit comments