我正在使用 Spring 4.3.0。我正在编写一个 SDK,其中包含以下类,
提供者.java
@ComponentScan
@Service
//@PropertySource("classpath:application.properties")
public class Providers {
@Autowired
ApplicationContext context;
public Providers(){
}
public Providers(ApplicationContext applicationContext){
this.context = applicationContext;
}
//...Other SDK component code
}
ProvidersBuilder.java
public class ProvidersBuilder {
//Set providers property
public Providers build() throws LifecycleException, InsufficientPropertiesException {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
StandardEnvironment env = new StandardEnvironment();
context.setEnvironment(env);
if(cond1){
context.getEnvironment().addActiveProfile("profile1");
}
if(cond2){
context.getEnvironment().addActiveProfile("profile2");
}
...etc
context.setParent(null);
context.register(Providers.class);
context.refresh();
Providers Providers = new Providers(context);
return Providers;
}
}
我对 Spring-Junit 测试类有以下配置,
SpringContextLoader.java
@ComponentScan(basePackages = "com.providers.global")
@PropertySource("classpath:application.properties")
public class SpringContextLoader {
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(SpringContextLoader.class);
}
}
在我的一个测试课中,我正在尝试打印所有配置文件,
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringContextLoader.class)
public class ProvidersTest{
@Autowired
ApplicationContext context;
@Before
public void beforeMethod() throws Exception {
String[] profiles = context.getEnvironment().getActiveProfiles();
if(profiles != null && profiles.length > 0){
for (String string : profiles) {
logger.info(String.format("Active Profiles in test::%s",string));
}
}
}
@Test
public void activateProviders() throws Exception{
...invoking test call
}
}
在日志中,我只能看到在 application.properties 中配置的配置文件,但我想获取在 ProvidersBuilder.java 中动态添加的配置文件。
基本上我只会为特定配置文件运行 ProvidersTest,因为我使用以下注释 @IfProfileValue(name = "spring.profiles.active", values = { "charging" }) 因为应用程序上下文总是返回应用程序中配置的默认配置文件。 properties 这个类永远没有机会运行。
谁能帮我解决这个问题。为什么在 ProvidersBuilder.java 中添加的配置文件在 ProvidersTest.java 中不可用?
**编辑 1 **
SpringContextLoader.java
@ComponentScan(basePackages = "com.providers.global")
@PropertySource("classpath:application.properties")
public class SpringContextLoader {
@Bean(name = "ConfigApplicationContext")
public AnnotationConfigApplicationContext applicationContext() {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext();
return context;
}
}
现在我们没有在应用程序的任何地方创建新的 AnnotationConfigApplicationContext。
ProvidersBuilder.java
public class ProvidersBuilder {
@Autowired
@Qualifier("ConfigApplicationContext")
public AnnotationConfigApplicationContext context;
//Set providers property
public Providers build() throws LifecycleException, InsufficientPropertiesException {
context.setEnvironment(env); **Here i am getting getting NullPointerException**
if(cond1){
context.getEnvironment().addActiveProfile("profile1");
}
if(cond2){
context.getEnvironment().addActiveProfile("profile2");
}
...etc
context.setParent(null);
context.register(Providers.class);
context.refresh();
Providers Providers = new Providers(context);
return Providers;
}
}
在 ProvidersBuilder.java 中,使用 @Autowired 获取“AnnotationConfigApplicationContext 上下文”时它返回 null。