【问题标题】:Spring bean wiring in different contexts不同上下文中的 Spring bean 接线
【发布时间】:2013-11-11 04:03:13
【问题描述】:

对于数据源层,我使用以下 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;
        }
    }
...

要运行测试,我通过 @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 {
...

而且效果很好。

在创建 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;
    }
}

但是现在,我在InfrastructureContextConfiguration 的以下行中得到了 NullPointerException:

dataSource.setDriverClassName(environment.getProperty("jdbc.driver"));

environment 未连接。我可以做些什么来解决它?

【问题讨论】:

    标签: java spring spring-mvc configuration


    【解决方案1】:

    我发现了什么。已经遇到了类似的问题: same1, some solutions

    seems the problem is not connected, but the last answer is the best solution

    总计: 实际上,@Inject 注入的字段不能为空。它必须抛出异常。结果,如果它为空,则根本没有应用注释。因此,主要原因是类路径中没有实现。

    所以我在 web.pom 中添加了以下内容。它解决了问题:

    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    

    作为我可以使用的替代选项:

    1. @Resource 而不是@Inject,并且已经设置了环境。

    2. 将环境作为参数传递给构造函数,而不是通过注释连接它。但最好的情况,恕我直言,是修复 jar 依赖。

    【讨论】:

      猜你喜欢
      • 2013-03-12
      • 2014-09-12
      • 2015-02-24
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多