Spring配置:创建一个字符串属性

时间:2015-10-07 13:48:47

标签: java spring jndi

2个问题:

  1. 您知道在Spring上使用JNDI资源的“最佳”方式吗? 配置?

  2. 如何将静态attribut configPath转换为我可以的“值” 在这两种方法中使用,还在其他bean上使用?

    @Configuration
    @ComponentScan(basePackages = {"com.fdilogbox.report.serveur"})
    @EnableAspectJAutoProxy
    public class SpringConfig {
    
        public static final String JNDI_RESOURCES = "java:comp/env/report/config";
        public static String configPath;
    
        @PostConstruct
        public void initConfig() throws IllegalArgumentException {
            try {
                final JndiTemplate template = new JndiTemplate();
                configPath = (String) template.lookup(JNDI_RESOURCES);
                Log4jConfigurer.initLogging(configPath + "/log4j.xml");
            } catch (NamingException | FileNotFoundException ex) {
                throw new IllegalArgumentException("Unable to start logging, because non-file resource!", ex);
            }
        }
    
        @Bean
        public static PropertySourcesPlaceholderConfigurer properties() {
            try {
                final JndiTemplate template = new JndiTemplate();
                configPath = (String) template.lookup(JNDI_RESOURCES);
            } catch (NamingException ex) {
                throw new IllegalArgumentException("JNDI resource not found : " + JNDI_RESOURCES, ex);
            }
            PropertySourcesPlaceholderConfigurer  pspc = new PropertySourcesPlaceholderConfigurer ();
            Resource[] resources = new FileSystemResource[]{new FileSystemResource(configPath + "/application.properties")};
            pspc.setLocations(resources);
            pspc.setIgnoreUnresolvablePlaceholders(true);
            return pspc;
        }
    }
    

0 个答案:

没有答案