栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

SpringPropertyPlaceholderConfigurer和context:property-placeholder

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringPropertyPlaceholderConfigurer和context:property-placeholder

<context:property-placeholder ... />
是与
PropertyPlaceholderConfigurer
等效的XML。所以,选择那个。在
<util:properties/>
简单的工厂一个
java.util.Properties
实例可以注入。

在Spring 3.1(不是3.0 …)中,你可以执行以下操作:

@Configuration@PropertySource("/foo/bar/services.properties")public class ServiceConfiguration {     @Autowired Environment environment;     @Bean public javax.sql.DataSource dataSource( ){         String user = this.environment.getProperty("ds.user");        ...    } }

在Spring 3.0中,你可以使用SpEl批注“访问”使用PropertyPlaceHolderConfigurer机制定义的属性:

@Value("${ds.user}") private String user;

如果要一起删除XML,只需使用Java配置手动注册PropertyPlaceholderConfigurer。我更喜欢3.1方法。但是,如果你使用Spring 3.0方法(因为3.1尚不支持GA …),你现在可以像上面这样定义XML:

@Configuration public class MySpring3Configuration {  @Bean         public static PropertyPlaceholderConfigurer configurer() {   PropertyPlaceholderConfigurer ppc = ...  ppc.setLocations(...);  return ppc;         }         @Bean         public class DataSource dataSource(     @Value("${ds.user}") String user,      @Value("${ds.pw}") String pw,      ...) {  DataSource ds = ... ds.setUser(user); ds.setPassword(pw);   ... return ds;        }}

注意,PPC是使用

static
bean定义方法定义的。这是确保豆类尽早注册所必需的,因为PPC是
BeanFactoryPostProcessor-
它可以在上下文中影响豆类本身的注册,因此必须先进行其他所有事情的注册。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/411255.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号