要在-server.xml和application.xml中都加入这个:
public class LazyInitBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
for (String beanName : beanFactory.getBeanDefinitionNames()) {
System. out.println("loadBeanName:" +beanName);
beanFactory.getBeanDefinition(beanName).setLazyInit( false);
}
}
}
先记下
* <context:property-placeholder/>是将properties加载到Spring上下文中,接下来在定义bean的时候就能用${xx.xx}来访问了。
*
是org.springframework.beans.factory.config.PropertiesFactoryBean的简化,声明了一个bean,可以用于注入。
这两种bean如何得到呢?
对于placeHolder 似乎是存在了environment
对于util:properties 他的beanName就是id名
注意webApplicationContext和ApplicationContext是不同的, 后者可以用最简单的注入的方法获得.
@Configuration
@PropertySource("/foo/bar/services.properties")
public class ServiceConfiguration {
@Autowired Environment environment;
@Bean public javax.sql.DataSource dataSource( ){
String user = this.environment.getProperty("jdbc.driverClassName");
...
}
}
But again you will need @Bean public static PropertySourcePlaceholderConfigurer pspc() in Spring 3.1 example
关于zookeeper加载到配置的问题解决
<util:properties id= "naja" location="classpath:/naja-web.properties" />
public class ZooKeeperPropertiesFactoryBean implements FactoryBean
private final Properties props = new Properties();
@Override
public Properties getObject() throws Exception {
if (props .isEmpty()) {
System. out.println("FactoryBean new" + hashCode());
System.out.println(ConfigManager .getInstance().getConfig(ConfigType. COUGAR_MASTER_DATABASE));
props.put("cougar.master.jdbc.username" , "sa" );
//props.putAll(zooProperties);
}
return props ;
}
@Override
public Class<?> getObjectType() {
return Properties.class;
}
@Override
public boolean isSingleton() {
return true ;
} }
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
Environment environment = appContext .getEnvironment();
System. out .println(environment.getProperty(“java.version” ));
近期评论