Skip to content

Commit 150afa6

Browse files
committed
Simplify SPR-9756
1 parent 090bbb7 commit 150afa6

File tree

6 files changed

+56
-88
lines changed

6 files changed

+56
-88
lines changed

Diff for: SPR-9756/pom.xml

+6-21
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@
1010

1111
<properties>
1212
<java-version>1.6</java-version>
13-
<!-- <org.springframework-version>3.1.1.RELEASE</org.springframework-version> -->
1413
<org.springframework-version>3.2.0.BUILD-SNAPSHOT</org.springframework-version>
1514
<org.slf4j-version>1.5.10</org.slf4j-version>
1615
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1716
</properties>
1817
<repositories>
19-
<repository>
20-
<id>s2</id>
21-
<name>s2 snapshot</name>
22-
<url>http://repo.springsource.org/libs-snapshot</url>
23-
</repository>
18+
<repository>
19+
<id>s2</id>
20+
<name>s2 snapshot</name>
21+
<url>http://repo.springsource.org/libs-snapshot</url>
22+
</repository>
2423
</repositories>
2524

2625
<dependencies>
@@ -42,20 +41,7 @@
4241
<artifactId>spring-webmvc</artifactId>
4342
<version>${org.springframework-version}</version>
4443
</dependency>
45-
<!-- Logging -->
46-
47-
<dependency>
48-
<groupId>org.apache.tomcat</groupId>
49-
<artifactId>tomcat-servlet-api</artifactId>
50-
<version>7.0.8</version>
51-
<scope>provided</scope>
52-
</dependency>
53-
<dependency>
54-
<groupId>javax.faces</groupId>
55-
<artifactId>jsf-api</artifactId>
56-
<version>1.2_08</version>
57-
<scope>optional</scope>
58-
</dependency>
44+
<!-- Logging -->
5945
<dependency>
6046
<groupId>org.slf4j</groupId>
6147
<artifactId>slf4j-api</artifactId>
@@ -77,7 +63,6 @@
7763
<groupId>junit</groupId>
7864
<artifactId>junit</artifactId>
7965
<version>4.10</version>
80-
<scope>test</scope>
8166
</dependency>
8267
<dependency>
8368
<groupId>log4j</groupId>
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,47 @@
11
package org.springsource.investigation;
22

3-
import java.lang.annotation.ElementType;
4-
import java.lang.annotation.Retention;
5-
import java.lang.annotation.RetentionPolicy;
6-
import java.lang.annotation.Target;
7-
83
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.ApplicationContext;
5+
import org.springframework.context.ConfigurableApplicationContext;
96
import org.springframework.context.annotation.Bean;
107
import org.springframework.context.annotation.Configuration;
118
import org.springframework.context.annotation.Profile;
129
import org.springframework.context.annotation.PropertySource;
1310
import org.springframework.core.env.Environment;
14-
import org.springframework.stereotype.Service;
15-
16-
abstract class BaseConfig {
17-
18-
@Autowired
19-
protected Environment environment;
20-
21-
@Bean
22-
public AppIdStringWrapper connectionFactoryLocator()
23-
{
24-
return new AppIdStringWrapper(environment.getProperty("appId"));
25-
}
26-
}
27-
28-
class AppIdStringWrapper {
29-
private String profileName;
30-
31-
public AppIdStringWrapper(String profileName) {
32-
this.profileName = profileName;
33-
}
34-
35-
public String getProfileName() {
36-
return this.profileName;
37-
}
38-
}
39-
4011

4112
@Profile("prod")
4213
@Configuration
4314
@PropertySource({ "classpath:prod-app.properties" })
44-
class ProdConfig extends BaseConfig {
45-
}
46-
47-
@Target(ElementType.TYPE)
48-
@Retention(RetentionPolicy.RUNTIME)
49-
@Profile("dev")
50-
@interface Dev {
51-
}
52-
53-
@Target(ElementType.TYPE)
54-
@Retention(RetentionPolicy.RUNTIME)
55-
@Profile("prod")
56-
@interface Prod {
57-
}
58-
59-
@Service
6015
public class App {
6116

62-
@Autowired
63-
AppIdStringWrapper appIdStringWrapper;
64-
65-
public void testApp()
66-
{
67-
}
17+
@Autowired
18+
protected Environment environment;
19+
20+
@Autowired
21+
protected ConfigurableApplicationContext context;
22+
23+
@Bean
24+
public String foo() {
25+
this.environment.getProperty("appId");
26+
//assertThat(this.context.getEnvironment(), sameInstance(this.environment));
27+
System.out.println("testing this.environment");
28+
test(this.environment);
29+
System.out.println("testing this.context.getBF().getBean(Env.class)");
30+
test((Environment)this.context.getBeanFactory().getBean("environment"));
31+
System.out.println("testing this.context.getEnv()");
32+
test(this.context.getEnvironment());
33+
return "bogus";
34+
}
35+
36+
private void test(Environment env) {
37+
if (this.context.getEnvironment() == env) {
38+
System.out.println("environment belongs to autowired app context");
39+
}
40+
else if (this.context.getParent().getEnvironment() == env){
41+
System.out.println("environment belongs to PARENT OF autowired app context");
42+
}
43+
else {
44+
System.out.println("environment is of unknown origin");
45+
}
46+
}
6847
}

Diff for: SPR-9756/src/main/resources/dev-app.properties

-2
This file was deleted.

Diff for: SPR-9756/src/main/resources/log4j.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
</appender>
1212

1313
<!-- 3rdparty Loggers -->
14-
<logger name="org.springframework.core.env">
15-
<level value="DEBUG" />
16-
</logger>
14+
<!-- <logger name="org.springframework.core.env"> -->
15+
<!-- <level value="DEBUG" /> -->
16+
<!-- </logger> -->
1717

1818
<!-- <logger name="org.springframework.web.context.support"> -->
1919
<!-- <level value="DEBUG" /> -->

Diff for: SPR-9756/src/main/resources/prod-app.properties

-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
#### Credentials ###
21
appId=prod

Diff for: SPR-9756/src/test/java/org/springsource/investigation/ReproTests.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,29 @@
1717
package org.springsource.investigation;
1818

1919
import org.junit.Test;
20+
import org.springframework.beans.factory.annotation.Autowired;
21+
import org.springframework.context.ConfigurableApplicationContext;
2022
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
23+
import org.springframework.context.support.GenericApplicationContext;
24+
import org.springframework.core.env.ConfigurableEnvironment;
2125

2226
import static org.hamcrest.CoreMatchers.*;
2327
import static org.junit.Assert.*;
2428

2529
public class ReproTests {
30+
@SuppressWarnings("unchecked")
2631
@Test
2732
public void repro() {
28-
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
29-
ctx.getEnvironment().addActiveProfile("prod");
30-
ctx.register(App.class);
31-
ctx.refresh();
32-
String testBean = ctx.getBean("testBean", String.class);
33-
assertThat(testBean, equalTo("testBeanValue"));
34-
String appId = ctx.getEnvironment().getProperty("appId");
35-
assertThat(appId, equalTo("prod"));
33+
ConfigurableApplicationContext parent = new GenericApplicationContext();
34+
parent.refresh();
35+
36+
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
37+
child.setParent(parent);
38+
child.refresh();
39+
40+
ConfigurableEnvironment env = child.getBean(ConfigurableEnvironment.class);
41+
assertThat("UNKNOWN ENV", env, anyOf(sameInstance(parent.getEnvironment()), sameInstance(child.getEnvironment())));
42+
assertThat("EXPECTED CHILD CTX ENV", env, sameInstance(child.getEnvironment()));
3643
}
3744

3845
}

0 commit comments

Comments
 (0)