Skip to content
This repository has been archived by the owner on Dec 15, 2021. It is now read-only.

Commit

Permalink
Simplify SPR-9756
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeams committed Sep 5, 2012
1 parent 090bbb7 commit 150afa6
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 88 deletions.
27 changes: 6 additions & 21 deletions SPR-9756/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -77,7 +63,6 @@
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
Expand Down
85 changes: 32 additions & 53 deletions SPR-9756/src/main/java/org/springsource/investigation/App.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
}
2 changes: 0 additions & 2 deletions SPR-9756/src/main/resources/dev-app.properties

This file was deleted.

6 changes: 3 additions & 3 deletions SPR-9756/src/main/resources/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" /> -->
Expand Down
1 change: 0 additions & 1 deletion SPR-9756/src/main/resources/prod-app.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
#### Credentials ###
appId=prod
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

}

0 comments on commit 150afa6

Please sign in to comment.