Skip to content

Commit dbd83a3

Browse files
committed
spring configuration
spring configuration using xml, annotation and java configuration
1 parent 401ed8f commit dbd83a3

File tree

8 files changed

+175
-0
lines changed

8 files changed

+175
-0
lines changed

spring-core-config/.classpath

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
4+
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
5+
<classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
6+
<classpathentry kind="output" path="target/classes"/>
7+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
8+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-core/3.2.3.RELEASE/spring-core-3.2.3.RELEASE.jar"/>
9+
<classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar"/>
10+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-beans/3.2.3.RELEASE/spring-beans-3.2.3.RELEASE.jar"/>
11+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-context/3.2.3.RELEASE/spring-context-3.2.3.RELEASE.jar"/>
12+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-aop/3.2.3.RELEASE/spring-aop-3.2.3.RELEASE.jar"/>
13+
<classpathentry kind="var" path="M2_REPO/aopalliance/aopalliance/1.0/aopalliance-1.0.jar" sourcepath="M2_REPO/aopalliance/aopalliance/1.0/aopalliance-1.0-sources.jar"/>
14+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-expression/3.2.3.RELEASE/spring-expression-3.2.3.RELEASE.jar"/>
15+
</classpath>

spring-core-config/pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.hmkcode</groupId>
6+
<artifactId>spring-core-container</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>spring-core-container</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.springframework</groupId>
20+
<artifactId>spring-core</artifactId>
21+
<version>3.2.3.RELEASE</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework</groupId>
25+
<artifactId>spring-beans</artifactId>
26+
<version>3.2.3.RELEASE</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.springframework</groupId>
30+
<artifactId>spring-context</artifactId>
31+
<version>3.2.3.RELEASE</version>
32+
</dependency>
33+
</dependencies>
34+
</project>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.hmkcode;
2+
3+
import org.springframework.context.ApplicationContext;
4+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5+
import org.springframework.context.support.ClassPathXmlApplicationContext;
6+
7+
import com.hmkcode.spring.beans.MyBean;
8+
import com.hmkcode.spring.beans.MyBeanAnnotated;
9+
import com.hmkcode.spring.config.JavaConfig;
10+
11+
12+
public class App
13+
{
14+
public static void main( String[] args )
15+
{
16+
17+
18+
ApplicationContext ctxXML = new ClassPathXmlApplicationContext("config/XMLConfig.xml");
19+
MyBean myBean = (MyBean) ctxXML.getBean("myBean");
20+
21+
System.out.println( myBean);
22+
23+
ApplicationContext ctxAnnotation = new ClassPathXmlApplicationContext("config/XMLConfig-Annotation.xml");
24+
myBean = (MyBean) ctxAnnotation.getBean("myBean2");
25+
26+
System.out.println( myBean);
27+
28+
ApplicationContext ctxJavaConfig = new AnnotationConfigApplicationContext(JavaConfig.class);
29+
myBean = (MyBean) ctxJavaConfig.getBean("myBean3");
30+
31+
System.out.println( myBean);
32+
33+
}
34+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.hmkcode.spring.beans;
2+
3+
import org.springframework.beans.factory.annotation.Required;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component(value="myBean2")
8+
public class MyBean {
9+
10+
private String name;
11+
12+
public String getName() {
13+
return name;
14+
}
15+
16+
@Value(value="BeanName")
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return "MyBean [name=" + name + "]";
24+
}
25+
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.hmkcode.spring.beans;
2+
3+
import org.springframework.beans.factory.annotation.Required;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component(value="b")
8+
public class MyBeanAnnotated {
9+
10+
private String name;
11+
12+
public String getName() {
13+
return name;
14+
}
15+
16+
@Required
17+
@Value(value="annotationBean")
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return "MyBean [name=" + name + "]";
25+
}
26+
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.hmkcode.spring.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
import com.hmkcode.spring.beans.MyBean;
7+
8+
@Configuration
9+
public class JavaConfig {
10+
11+
@Bean(name = "myBean3")
12+
public MyBean myBean(){
13+
MyBean myBean = new MyBean();
14+
myBean.setName("myBeanJavaConfig");
15+
return myBean;
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
xmlns:context="http://www.springframework.org/schema/context"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans.xsd
7+
http://www.springframework.org/schema/context
8+
http://www.springframework.org/schema/context/spring-context.xsd">
9+
10+
<context:component-scan base-package="com.hmkcode.spring.beans"/>
11+
12+
</beans>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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.xsd">
6+
7+
<bean id="myBean" class="com.hmkcode.spring.beans.MyBean">
8+
<property name="name" value="xmlBean" />
9+
</bean>
10+
</beans>

0 commit comments

Comments
 (0)