[英]Spring bean wiring in different contexts
For datasource layer I use the following Spring Configuration file: 对于数据源层,我使用以下Spring配置文件:
@Configuration
@ComponentScan(basePackages = {"com.savdev.springmvcexample.repository", "com.savdev.springmvcexample.config"})
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {"com.savdev.springmvcexample.repository"})
public class InfrastructureContextConfiguration {
...
@Configuration
@Profile(value = "file_based")
@PropertySource("classpath:/db/config/file_based.properties")
public static class FileBasedConfiguration {
@Inject
private Environment environment;
@Bean
public DataSource dataSource() {
BasicDataSource dataSource = new org.apache.commons.dbcp.BasicDataSource();
dataSource.setDriverClassName(environment.getProperty("jdbc.driver"));
dataSource.setUrl(environment.getProperty("jdbc.url"));
dataSource.setUsername(environment.getProperty("jdbc.username"));
dataSource.setPassword(environment.getProperty("jdbc.password"));
return dataSource;
}
}
...
To run tests I load this configuration via @ContextConfiguration
: 要运行测试,我通过
@ContextConfiguration
加载此配置:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { InfrastructureContextConfiguration.class, HsqldbEmbeddableDbStarterContextConfiguration.class })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
@Transactional()
@ActiveProfiles(profiles = {"file_based", "test_data"} )
public abstract class AbstractJpaJavaTestBase {
...
And it works fine. 而且效果很好。
The same InfrastructureContextConfiguration class is used in web module when DispatcherServlet is created: 创建DispatcherServlet时,将在Web模块中使用相同的InfrastructureContextConfiguration类:
public class SpringMvcExampleWebApplicationInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
registerDispatcherServlet(servletContext);
}
private void registerDispatcherServlet(final ServletContext servletContext) {
WebApplicationContext dispatcherContext = createContext(WebMvcContextConfiguration.class, InfrastructureContextConfiguration.class);
DispatcherServlet dispatcherServlet = new DispatcherServlet(dispatcherContext);
dispatcherServlet.setContextInitializers( new SpringMvcExampleProfilesInitializer());
ServletRegistration.Dynamic dispatcher;
dispatcher = servletContext.addServlet("dispatcher", dispatcherServlet);
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
private WebApplicationContext createContext(final Class<?>... annotatedClasses) {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.register(annotatedClasses);
return context;
}
}
But now, I'm getting NullPointerException in the following line of InfrastructureContextConfiguration
: 但是现在,我在
InfrastructureContextConfiguration
的以下行中得到NullPointerException:
dataSource.setDriverClassName(environment.getProperty("jdbc.driver"));
The environment
is not wired. environment
未连接。 What can I do to resolve it? 我该怎么解决?
What I found. 我发现了什么。 The similar issue already have been met: same1, some solutions
已经遇到了类似的问题: same1,一些解决方案
seems the problem is not connected, but the last answer is the best solution 似乎问题未解决,但最后的答案是最好的解决方案
total: Actually, the field that is injected with @Inject cannot be null. total:实际上,使用@Inject注入的字段不能为null。 It must throws exception.
它必须引发异常。 As a result if it is null then - the annotation has not been applied at all.
结果,如果它为null,则-注释根本没有被应用。 As a result the main reason is the absence of its implementation in classpath.
结果,主要原因是没有在类路径中实现它。
So I added the following in my web.pom. 因此,我在web.pom中添加了以下内容。 and it resolved the problem:
它解决了问题:
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
As alternative options I could use: 作为替代选项,我可以使用:
@Resource instead of @Inject, and environment has been set. @Resource而不是@Inject,并且已设置环境。
Passed the envirionment as argument into the constructor, instead of wiring it via annotation. 将环境作为参数传递给构造函数,而不是通过注释进行连接。 But the best case, IMHO, is fix jar dependecy.
但是最好的情况,恕我直言,是修复罐依赖。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.