Мой проект maven не удалил мой файл свойств в переменную окружения. Я использую весну 4.2.3.Как файл Autwired properties для переменной среды?
Это ошибка при запуске Maven: тест
INFO: Refreshing org[email protected]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>
Это руководство помогло alot - http://examples.javacodegeeks.com/enterprise-java/spring/load-environment-configurations-and-properties-with-spring-example/ –