Skip to content

Commit 211d3f7

Browse files
committed
spring-@Profile
1 parent 89e3c82 commit 211d3f7

File tree

6 files changed

+162
-0
lines changed

6 files changed

+162
-0
lines changed

spring-@profile/.classpath

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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/junit/junit/4.11/junit-4.11.jar"/>
9+
<classpathentry kind="var" path="M2_REPO/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar"/>
10+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-core/3.1.0.RELEASE/spring-core-3.1.0.RELEASE.jar"/>
11+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-asm/3.1.0.RELEASE/spring-asm-3.1.0.RELEASE.jar"/>
12+
<classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar"/>
13+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-beans/3.1.0.RELEASE/spring-beans-3.1.0.RELEASE.jar"/>
14+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-context/3.1.0.RELEASE/spring-context-3.1.0.RELEASE.jar"/>
15+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-aop/3.1.0.RELEASE/spring-aop-3.1.0.RELEASE.jar"/>
16+
<classpathentry kind="var" path="M2_REPO/aopalliance/aopalliance/1.0/aopalliance-1.0.jar"/>
17+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-expression/3.1.0.RELEASE/spring-expression-3.1.0.RELEASE.jar"/>
18+
<classpathentry kind="var" path="M2_REPO/org/springframework/spring-context-support/3.1.0.RELEASE/spring-context-support-3.1.0.RELEASE.jar"/>
19+
<classpathentry kind="var" path="M2_REPO/cglib/cglib-nodep/2.2/cglib-nodep-2.2.jar"/>
20+
</classpath>

spring-@profile/pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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-profile</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>spring-profile</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<org.springframework.version>3.1.0.RELEASE</org.springframework.version>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>junit</groupId>
21+
<artifactId>junit</artifactId>
22+
<version>4.11</version>
23+
<scope>test</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework</groupId>
27+
<artifactId>spring-core</artifactId>
28+
<version>${org.springframework.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework</groupId>
32+
<artifactId>spring-beans</artifactId>
33+
<version>${org.springframework.version}</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework</groupId>
37+
<artifactId>spring-context</artifactId>
38+
<version>${org.springframework.version}</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework</groupId>
42+
<artifactId>spring-context-support</artifactId>
43+
<version>${org.springframework.version}</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>cglib</groupId>
47+
<artifactId>cglib-nodep</artifactId>
48+
<version>2.2</version>
49+
<scope>compile</scope>
50+
</dependency>
51+
</dependencies>
52+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.hmkcode;
2+
3+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
4+
5+
6+
import com.hmkcode.vo.Person;
7+
8+
public class App
9+
{
10+
public static void main( String[] args )
11+
{
12+
13+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
14+
ctx.getEnvironment().setActiveProfiles("development"); //or production
15+
ctx.scan("com.hmkcode.spring"); // register all @Configuration classes
16+
//ctx.register(SpringConfigDev.class);
17+
ctx.refresh();
18+
Person person = (Person)ctx.getBean("person");
19+
20+
21+
System.out.println("Person: "+person);
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.hmkcode.spring;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.Profile;
6+
7+
import com.hmkcode.vo.Person;
8+
9+
@Configuration
10+
@Profile("development")
11+
public class SpringConfigDev {
12+
13+
@Bean
14+
public Person person(){
15+
16+
Person person = new Person();
17+
person.setId(1);
18+
person.setName("dev-person");
19+
20+
return person;
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.hmkcode.spring;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.Profile;
6+
7+
import com.hmkcode.vo.Person;
8+
9+
@Configuration
10+
@Profile("production")
11+
public class SpringConfigPro {
12+
13+
@Bean
14+
public Person person(){
15+
16+
Person person = new Person();
17+
person.setId(2);
18+
person.setName("pro-person");
19+
20+
return person;
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.hmkcode.vo;
2+
3+
public class Person {
4+
5+
private int id;
6+
private String name;
7+
public int getId() {
8+
return id;
9+
}
10+
public void setId(int id) {
11+
this.id = id;
12+
}
13+
public String getName() {
14+
return name;
15+
}
16+
public void setName(String name) {
17+
this.name = name;
18+
}
19+
20+
public String toString(){
21+
return "Id: "+id+" - Name: "+name;
22+
}
23+
}

0 commit comments

Comments
 (0)