【问题标题】:How to Autowired properties file to Environment variable?如何将属性文件自动连接到环境变量?
【发布时间】:2016-03-26 19:25:34
【问题描述】:

我的 Maven 项目无法将我的属性文件自动装配到环境变量中。我正在使用弹簧 4.2.3。

这是我运行 maven:test 时的错误

INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@57e1b0c: startup date [Sun Dec 20 08:55:34 PST 2015]; root of context hierarchy
Dec 20, 2015 8:55:34 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from URL [file:/home/bryan-1/workspace/2015dec_riot/test/target/test-classes/applicationContext.xml]
Dec 20, 2015 8:55:34 AM org.springframework.context.support.PropertySourcesPlaceholderConfigurer loadProperties
INFO: Loading properties file from class path resource [application.it.properties]
Dec 20, 2015 8:55:34 AM org.springframework.context.support.ClassPathXmlApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'JMSProperties' defined in file [/home/bryan-1/workspace/2015dec_riot/test/target/classes/test/JMSProperties.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [test.JMSProperties$$EnhancerBySpringCGLIB$$b873400b]: Constructor threw exception; nested exception is java.lang.NullPointerException

这是我的 applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd"
    xmlns:context="http://www.springframework.org/schema/context">

    <context:component-scan base-package="test" />
    <context:property-placeholder location="classpath:/application.it.properties" />

    <!-- bean definitions here -->
</beans>

这是我的属性类:

@Configuration
public class JMSProperties {

    ....

    @Autowired
    private Environment env;

    public JMSProperties() {

        ....
    }

    ....
}

这是Maven默认生成的测试用例:

public class AppTest
    extends TestCase
{

    @Autowired
    JMSProperties properties;

    private static final Logger logger = LoggerFactory.getLogger(AppTest.class);

    AbstractApplicationContext context = new ClassPathXmlApplicationContext("classpath*:/applicationContext.xml");

    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        logger.info(properties.getMatchMakerPassword());
    }
}

我的依赖:

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.3.RELEASE</version>
        </dependency>
        <!-- logging framework dependencies -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-ext</artifactId>
            <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.2</version>
        </dependency>
    </dependencies>

【问题讨论】:

标签: maven spring-4


【解决方案1】:

由于异常是 JMSProperties 的构造函数中的 NPE,因此了解那里的代码会有所帮助。如果不是很有帮助,您的帖子:

 public JMSProperties() {
        ....
    }

我的猜测是你尝试在那里使用 env 。此时env为null,因为spring要先实例化类,再注入字段。因此是 NPE。

【讨论】:

  • 不是这样的。我从构造函数中删除了所有内容。并有一个 String get() 方法来返回值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-29
  • 1970-01-01
  • 2011-07-13
相关资源
最近更新 更多