2个问题:
您知道在Spring上使用JNDI资源的“最佳”方式吗? 配置?
如何将静态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;
}
}