1

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

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

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

environment没有接线。我能做些什么来解决它?

4

1 回答 1

1

我发现了什么。类似的问题已经遇到过: same1,一些解决方案

似乎问题没有连接,但最后一个答案是最好的解决方案

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

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

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

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

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

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

于 2013-11-13T05:12:02.533 回答