Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simplify SPR-9756

  • Loading branch information...
commit 150afa6e3d1964ab8959a67cc2a70d32e0c115c3 1 parent 090bbb7
@cbeams cbeams authored
View
27 SPR-9756/pom.xml
@@ -10,17 +10,16 @@
<properties>
<java-version>1.6</java-version>
-<!-- <org.springframework-version>3.1.1.RELEASE</org.springframework-version> -->
<org.springframework-version>3.2.0.BUILD-SNAPSHOT</org.springframework-version>
<org.slf4j-version>1.5.10</org.slf4j-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
- <repository>
- <id>s2</id>
- <name>s2 snapshot</name>
- <url>http://repo.springsource.org/libs-snapshot</url>
- </repository>
+ <repository>
+ <id>s2</id>
+ <name>s2 snapshot</name>
+ <url>http://repo.springsource.org/libs-snapshot</url>
+ </repository>
</repositories>
<dependencies>
@@ -42,20 +41,7 @@
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
-<!-- Logging -->
-
- <dependency>
- <groupId>org.apache.tomcat</groupId>
- <artifactId>tomcat-servlet-api</artifactId>
- <version>7.0.8</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.faces</groupId>
- <artifactId>jsf-api</artifactId>
- <version>1.2_08</version>
- <scope>optional</scope>
- </dependency>
+ <!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
@@ -77,7 +63,6 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
- <scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
View
85 SPR-9756/src/main/java/org/springsource/investigation/App.java
@@ -1,68 +1,47 @@
package org.springsource.investigation;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
-
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
-import org.springframework.stereotype.Service;
-
-abstract class BaseConfig {
-
- @Autowired
- protected Environment environment;
-
- @Bean
- public AppIdStringWrapper connectionFactoryLocator()
- {
- return new AppIdStringWrapper(environment.getProperty("appId"));
- }
-}
-
-class AppIdStringWrapper {
- private String profileName;
-
- public AppIdStringWrapper(String profileName) {
- this.profileName = profileName;
- }
-
- public String getProfileName() {
- return this.profileName;
- }
-}
-
@Profile("prod")
@Configuration
@PropertySource({ "classpath:prod-app.properties" })
-class ProdConfig extends BaseConfig {
-}
-
-@Target(ElementType.TYPE)
-@Retention(RetentionPolicy.RUNTIME)
-@Profile("dev")
-@interface Dev {
-}
-
-@Target(ElementType.TYPE)
-@Retention(RetentionPolicy.RUNTIME)
-@Profile("prod")
-@interface Prod {
-}
-
-@Service
public class App {
- @Autowired
- AppIdStringWrapper appIdStringWrapper;
-
- public void testApp()
- {
- }
+ @Autowired
+ protected Environment environment;
+
+ @Autowired
+ protected ConfigurableApplicationContext context;
+
+ @Bean
+ public String foo() {
+ this.environment.getProperty("appId");
+ //assertThat(this.context.getEnvironment(), sameInstance(this.environment));
+ System.out.println("testing this.environment");
+ test(this.environment);
+ System.out.println("testing this.context.getBF().getBean(Env.class)");
+ test((Environment)this.context.getBeanFactory().getBean("environment"));
+ System.out.println("testing this.context.getEnv()");
+ test(this.context.getEnvironment());
+ return "bogus";
+ }
+
+ private void test(Environment env) {
+ if (this.context.getEnvironment() == env) {
+ System.out.println("environment belongs to autowired app context");
+ }
+ else if (this.context.getParent().getEnvironment() == env){
+ System.out.println("environment belongs to PARENT OF autowired app context");
+ }
+ else {
+ System.out.println("environment is of unknown origin");
+ }
+ }
}
View
2  SPR-9756/src/main/resources/dev-app.properties
@@ -1,2 +0,0 @@
-#### Credentials ###
-appId=dev
View
6 SPR-9756/src/main/resources/log4j.xml
@@ -11,9 +11,9 @@
</appender>
<!-- 3rdparty Loggers -->
- <logger name="org.springframework.core.env">
- <level value="DEBUG" />
- </logger>
+<!-- <logger name="org.springframework.core.env"> -->
+<!-- <level value="DEBUG" /> -->
+<!-- </logger> -->
<!-- <logger name="org.springframework.web.context.support"> -->
<!-- <level value="DEBUG" /> -->
View
1  SPR-9756/src/main/resources/prod-app.properties
@@ -1,2 +1 @@
-#### Credentials ###
appId=prod
View
23 SPR-9756/src/test/java/org/springsource/investigation/ReproTests.java
@@ -17,22 +17,29 @@
package org.springsource.investigation;
import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
+import org.springframework.context.support.GenericApplicationContext;
+import org.springframework.core.env.ConfigurableEnvironment;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
public class ReproTests {
+ @SuppressWarnings("unchecked")
@Test
public void repro() {
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
- ctx.getEnvironment().addActiveProfile("prod");
- ctx.register(App.class);
- ctx.refresh();
- String testBean = ctx.getBean("testBean", String.class);
- assertThat(testBean, equalTo("testBeanValue"));
- String appId = ctx.getEnvironment().getProperty("appId");
- assertThat(appId, equalTo("prod"));
+ ConfigurableApplicationContext parent = new GenericApplicationContext();
+ parent.refresh();
+
+ AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
+ child.setParent(parent);
+ child.refresh();
+
+ ConfigurableEnvironment env = child.getBean(ConfigurableEnvironment.class);
+ assertThat("UNKNOWN ENV", env, anyOf(sameInstance(parent.getEnvironment()), sameInstance(child.getEnvironment())));
+ assertThat("EXPECTED CHILD CTX ENV", env, sameInstance(child.getEnvironment()));
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.