Java 使用带@configuration注释的类访问属性

Java 使用带@configuration注释的类访问属性,java,spring-boot,Java,Spring Boot,我正在尝试使用下面的方法从属性文件中获取所有属性 我遇到一个异常,无法实例化FilterConfiguration类。从属性文件访问属性的最佳方法是什么 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Pro

我正在尝试使用下面的方法从属性文件中获取所有属性

我遇到一个异常,无法实例化FilterConfiguration类。从属性文件访问属性的最佳方法是什么

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
@Configuration
@PropertySource("classpath:configuration.properties")
public class FilterConfigurations {

    @Autowired
    private Environment env;

    public  String filePath = env.getProperty("file.path");

}



import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.cisco.sso.daas.models.User;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class FileOperations<T> implements Operations<T> {

    private ObjectMapper objectMapper;
    //private FilterConfigurations fc;
    private File file;
    //private String filePath = fc.filePath;
    // this is going to come from the property file.
    private String filePath = "C:\\Users\\phyadavi\\SYSLOG-NG\\platform\\syslogng\\FilteringService\\src\\main\\resources\\seed.txt";

    // private T targetClass;

    public FileOperations() {
        this.objectMapper = new ObjectMapper();
        this.file = new File(filePath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.getMessage();
            }
        }
    }

    private void saveImpl(Object serializableObject) throws IOException {
        String jsonData = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(serializableObject);
        BufferedWriter br = new BufferedWriter(new FileWriter(file));
        br.write(jsonData);
        br.close();
    }

    @Override
    public void save(List<T> serializableObjects) throws IOException {
        saveImpl(serializableObjects);
    }

    @Override
    public void save(T serializableObject) throws IOException {
        saveImpl(serializableObject);
    }

    @Override
    public List<T> get() throws IOException {
        // JavaType type =
        // objectMapper.getTypeFactory().constructCollectionType(ArrayList.class,
        // targetClass.getClass());
        // List<T> list = objectMapper.readValue(file, type);
        List<T> list = objectMapper.readValue(file, new TypeReference<List<User>>() {
        });
        if (list == null) {
            List<T> list1 = new ArrayList<T>();
            return list1;
        }
        return list;
    }

}

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-11-10 13:19:40.510 ERROR 7324 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'filterConfigurations' defined in file [C:\Users\phyadavi\SYSLOG-NG\platform\syslogng\FilteringService\target\classes\com\cisco\sso\daas\application\FilterConfigurations.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.cisco.sso.daas.application.FilterConfigurations$$EnhancerBySpringCGLIB$$58eb5e77]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1155) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
    at com.cisco.sso.daas.application.ConfigurationServiceApplication.main(ConfigurationServiceApplication.java:14) [classes/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.cisco.sso.daas.application.FilterConfigurations$$EnhancerBySpringCGLIB$$58eb5e77]: Constructor threw exception; nested exception is java.lang.NullPointerException
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1147) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    ... 17 common frames omitted
Caused by: java.lang.NullPointerException: null
    at com.cisco.sso.daas.application.FilterConfigurations.<init>(FilterConfigurations.java:17) ~[classes/:na]
    at com.cisco.sso.daas.application.FilterConfigurations$$EnhancerBySpringCGLIB$$58eb5e77.<init>(<generated>) ~[classes/:na]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_144]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_144]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_144]
    at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:1.8.0_144]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    ... 19 common frames omitted
import org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.context.annotation.PropertySource;
导入org.springframework.core.env.Environment;
导入org.springframework.stereotype.Component;
@组成部分
@配置
@PropertySource(“classpath:configuration.properties”)
公共类筛选器配置{
@自动连线
私人环境署;
公共字符串filePath=env.getProperty(“file.path”);
}
导入java.io.BufferedWriter;
导入java.io.File;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入com.cisco.sso.daas.models.User;
导入com.fasterxml.jackson.core.type.TypeReference;
导入com.fasterxml.jackson.databind.ObjectMapper;
公共类FileOperations实现操作{
私有对象映射器对象映射器;
//专用过滤器配置fc;
私有文件;
//私有字符串filePath=fc.filePath;
//这将来自属性文件。
私有字符串filePath=“C:\\Users\\phyadavi\\SYSLOG-NG\\platform\\syslogng\\FilteringService\\src\\main\\resources\\seed.txt”;
//私人T-targetClass;
公共文件操作(){
this.objectMapper=新的objectMapper();
this.file=新文件(filePath);
如果(!file.exists()){
试一试{
createNewFile();
}捕获(IOE异常){
e、 getMessage();
}
}
}
私有void saveImpl(对象序列化对象)引发IOException{
字符串jsonData=objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(serializableObject);
BufferedWriter br=新的BufferedWriter(新的文件写入程序(文件));
br.write(jsonData);
br.close();
}
@凌驾
public void save(List serializableObjects)引发IOException{
saveImpl(序列化对象);
}
@凌驾
public void save(T serializableObject)引发IOException{
saveImpl(serializableObject);
}
@凌驾
public List get()引发IOException{
//JavaType类型=
//objectMapper.getTypeFactory().ConstructionCollectionType(ArrayList.class,
//targetClass.getClass());
//List List=objectMapper.readValue(文件,类型);
List List=objectMapper.readValue(文件,新类型引用(){
});
if(list==null){
List list1=新的ArrayList();
返回列表1;
}
退货清单;
}
}
启动ApplicationContext时出错。要显示自动配置报告,请在启用“调试”的情况下重新运行应用程序。
2017-11-10 13:19:40.510错误7324---[main]o.s.boot.SpringApplication:应用程序启动失败
org.springframework.beans.factory.BeanCreationException:创建名为“FilterConfiguration”的bean时出错,该名称在文件[C:\Users\phyadavi\SYSLOG-NG\platform\syslogng\FilteringService\target\classes\com\cisco\sso\daas\application\FilterConfiguration.class]中定义:bean实例化失败;嵌套异常为org.springframework.beans.beanstantiationException:未能实例化[com.cisco.sso.daas.application.FilterConfiguration$$EnhancerBySpringCGLIB$$58eb5e77]:构造函数引发异常;嵌套异常是java.lang.NullPointerException
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.InstanceBean(AbstractAutowireCapableBeanFactory.java:1155)~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
在org.springframework.beans.factory.support.DefaultListableBeanFactory.PreInstanceSingleton(DefaultListableBeanFactory.java:761)~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)~[spring-context-4.3.12.RELEASE.jar:4.3.12.RELEASE]
在org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)~[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)[spring-boot-1.5.8.RELEASE.jar:1.5.8.RELEASE]
在org.spr
String getFilePath(){

    return env.getProperty("file.path");
}
@Value("${file.path}")
private String filePath; 
@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.javabycode.springmvc.configuration" })
@PropertySource(value = { "classpath:jdbc.properties" })
public class MyHibernateConfig {

    @Autowired
    private Environment environment;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
        dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
        dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
        return dataSource;
    }
}