读取侦听器

时间:2015-08-20 07:24:23

标签: java spring spring-mvc spring-boot

我的要求是,我需要在spring boot中启动服务器时初始化一些应用程序资源。为了初始化这些资源,我需要一堆属性。所以,我已将这些属性保存在外部属性文件中,并且我正在尝试在启动Spring引导时读取自定义侦听器中的属性。问题是,我无法在侦听器中获取任何属性值。我可以在应用程序启动后阅读它们,没有任何问题。但是,当应用程序启动时,我需要在侦听器内部使用它们。我正在低于例外...如何解决它。帮我吧!

    2015-08-20 02:58:59.585 ERROR 9376 --- [ost-startStop-1] o.a.c.c.C.[.[localhost].[/shared]        : Exception sending context initialized ev
ent to listener instance of class com.org.man.api.initializer.PropertyInitializerListener

java.lang.NoSuchMethodError: com.org.man.api.beans.property.ConfigProperties.getConfigNames()Ljava/util/List;
        at com.org.man.api.beans.property.PropertyBeanParser.initializeConfigProperties(PropertyBeanParser.java:33)
        at com.org.man.api.initializer.J2eeInitializer.getJ2eePresets(J2eeInitializer.java:79)
        at com.org.man.api.initializer.J2eeInitializer.initialize(J2eeInitializer.java:36)
        at com.org.man.api.initializer.PropertyInitializerListener.contextInitialized(PropertyInitializerListener.java:81)
        at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)

2015-08-20 02:58:59.592 ERROR 9376 --- [ost-startStop-1] o.apache.catalina.core.StandardContext   : One or more listeners failed to start. F
ull details will be found in the appropriate container log file
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Fac
tory method 'viewControllerHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: The resources may not be ac
cessed if they are not currently started
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
        ... 23 common frames omitted
Caused by: java.lang.IllegalStateException: The resources may not be accessed if they are not currently started
        at org.apache.catalina.webresources.StandardRoot.validate(StandardRoot.java:245)
        at org.apache.catalina.webresources.StandardRoot.getResource(StandardRoot.java:212)

听众代码

public class PropertyInitializerListener implements ServletContextListener {
private static final String INITIALIZED = PropertyInitializerListener.class.getName() + ".INITIALIZED";
private J2eeInitializer initializer;

@Autowired
PropertyBeanParser parser;

public void contextDestroyed(ServletContextEvent event) {
    if (initializer != null) {
        initializer.terminate();
    }

    ServletContext context = event.getServletContext();
    context.removeAttribute(FileSearcher.CONFIG_FILE_PROP);
}

public void contextInitialized(ServletContextEvent event) {
    ServletContext context = event.getServletContext();

    if (context.getAttribute(INITIALIZED) != null) {
        throw new IllegalStateException(
            "Already initialized - " +
            "check whether you have multiple <listener> definitions in your web.xml!");
    }
    ConfigBean presets = super.getPresets();
    presets = parser.initializeConfigProperties();
    SmapiDebug.setSaveMode(true);
    SmapiDebug.info("contextInitialized");

PropertyBeanparser代码

@Configuration    

@EnableConfigurationProperties({ConfigProperties.class,LoggingProperties.class,
            InstrumentationProperties.class,KeyeventProperties.class})
        public class PropertyBeanParser {

            @Autowired
            private ConfigProperties configProperties;

            @Autowired
            private LoggingProperties loggingProperties;

            @Autowired
            private InstrumentationProperties instrumentationProperties;

            @Autowired
            private KeyeventProperties keyeventProperties;

            public ConfigBean initializeConfigProperties(){

                ConfigBean configBean = new ConfigBean();
                try{
                    if(configProperties.getConfigNames()!=null && configProperties.getConfigValues()!=null) {
                        if(configProperties.getConfigNames().size()==configProperties.getConfigValues().size()){
                            for(int i=0;i<=configProperties.getConfigNames().size();i++){
                                ConfigVarDefinitionBean var = new ConfigVarDefinitionBean(configProperties.getConfigNames().get(i),
                                        configProperties.getConfigValues().get(i));
                                configBean.addConfigVarDefinition(var);
                            }
                        }
                        else{
                            throw new Exception("number of names and values are not matching");
                        }
                    }

                }
                catch(Exception e){
                    e.getMessage();
                }
                return configBean;
            }
        }

ConfigProperties类

@Configuration
@ConfigurationProperties(locations = "file:config.properties", prefix = "config")
public class ConfigProperties {

    private List<String> configNames = new ArrayList<String>();
    private List<String> configValues = new ArrayList<String>();
    public List<String> getConfigNames() {
        return configNames;
    }
    public void setConfigNames(List<String> configNames) {
        this.configNames = configNames;
    }
    public List<String> getConfigValues() {
        return configValues;
    }
    public void setConfigValues(List<String> configValues) {
        this.configValues = configValues;
    }
}

Config.Properties

config.configNames[0]=test1
config.configNames[1]=Testserver
config.configNames[2]=ResourceId
config.configNames[3]=AdaptorName
config.configNames[4]=runLevel

config.configValues[0]=ServiceComp
config.configValues[1]=Test
config.configValues[2]=instance2
config.configValues[3]=test
config.configValues[4]=localhost

2 个答案:

答案 0 :(得分:2)

问题是,在Spring启动期间无法在侦听器内检索属性。因此,为了在启动时进行一些初始化,我们可以在通过实现CommandLineRunner设置@SpringBootApplication批注的类中添加run方法。如果你这样做,那个run方法将在完成SpringApplication的run方法之前执行。这就是我试过的方法。

@SpringBootApplication
public class SpringResource implements CommandLineRunner {

    /**
     * @param args
     */
    @Autowired
    PropertyTest test;

    public void run(String... args){
        test.print();
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringResource.class, args);

    }
}

PropertyTest类

@Configuration
@EnableConfigurationProperties({ConfigProperties.class})
@Controller
public class PropertyTest {

    @Autowired
    ConfigProperties config;


    @RequestMapping(value = "/dummy", method = RequestMethod.GET)
    @ResponseBody
    public void print() {
        // TODO Auto-generated method stub
        for(int i=0;i<config.getConfigNames().size();i++)
        System.out.println("Im in Property test method. :)" +config.getConfigNames().get(i)+" "+config.getConfigValues().get(i));
    }

}

答案 1 :(得分:0)

我是从我的手机回答,但可能是听众的问题,你没有自动装配你创建的PropertyBeanparser,你打破了春天的魔法......我认为在主应用类springboot中声明一个监听器并在内部弹出“流动”希望有所帮助