Properties配置文件转Bean对象
来源:互联网 发布:js打砖块游戏图片素材 编辑:程序博客网 时间:2024/06/06 03:05
在XML配置文件中配置的类代码:
public class AnnotationBean extends PropertyPlaceholderConfigurer implements BeanPostProcessor, InitializingBean{private static transient Log logger=LogFactory.getLog(AnnotationBean .class);private java.util.propertise pros;@SuppressWarnings("unchecked")private Class[] enableClassList={String.class};@SuppressWarnings("unchecked")public void setEnableClssList(Class[] enableClassList){ this.enableClassList=enableClassList;}public Object postProcessAfterInitialization(Object bean,String beanName) throws BeansException{ Field[] fields=bean.getClass().getDeclaredFields(); for(Field field:fields){ if(logger.isDebugEnabled()){ StringBuilder sb=new StringBuilder(); sb.append("========="); .append(field.getType()) .append("=========") .append(field.getName) .append("=========") .append(field.isAnnotionPresent(Properties.class)); logger.debug(sb.toString); } //TODO if(field.isAnnotationPresent(Properties.class)){ if(filterType(field.getType().toString())){ Properties p=field.getAnnotation(Properties .class); RefletionUtils.makeAccessible(field); String pName; if("".equals(p.name())){ pName=field.getName(); }else{ pName=p.name(); } //中文转码 String propVal=pros.getProperty(pName); String trans=""; if(propVal != null){ trans=new String(propVal.getByte("ISO-8859-1"),"UTF-8"); } field.set(bean,trans); } } } return bean;}@SuppressWarnings("unchecked")private boolean filterType(String type){if(type != null){ for(Class c:enableClassList){ if(c.toString().equals(type)){ return true; } } return false; }else{ return true; }}public Object postProcessBeforeInitialization(Obejct bean,String beanName)throws BeansException{ return bean;}public void afterPropertiesSet()throws Exception{pros=margeProperties();}}
applicationContext.xml文件中的配置代码:
<bean id="propertyConfig" class="xxx.xxx.AnnotationBean "> <property name="order" value="1"/> <property name="ignoreUnresolvblePlaceholders" value="true"/> <property name="locations"> <list> <value>classpath:xxxx.properties</value> </list> </property></bean>
其中用到的自定义注解类代码:
@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)public @interface Properties{String name() default "";}
关于自定义注解在配置文件绑定的bean对象的属相上使用,
这种配置方式就是可以把配置文件映射成Java的bean对象。
一般常用的还有读取配置文件,用文件流的方式读取,网上讲解示例偏多。
以上代码纯属手打,可能有错误单词。
0 0
- Properties配置文件转Bean对象
- Spring 中注入bean的properties配置文件位置问题解决
- Spring 中注入bean的properties配置文件位置问题解决
- Spring设置与读取.properties配置文件的bean
- java 获取spring配置文件中bean 对象
- spring配置文件 properties转yml
- Properties配置文件
- Properties配置文件
- Properties配置文件
- properties配置文件
- Properties配置文件
- properties配置文件
- bean 对象转 XML
- spring框架中Bean的基本属性及调用外部properties等配置文件的方法介绍
- 用Dom4j和SAX解析配置文件成java Bean对象
- 取得Spring配置文件中的Bean对象的通用方法
- struts2的struts.properties配置文件详解(转)
- Spring properties定义bean
- C++11 Intro
- Watson Explorer 入门(6):开发和部署UIMA管道
- git bash 上传项目到github
- 《物联网安全基础》笔记二
- POJ 3261 Milk Patterns (后缀数组 + 二分)
- Properties配置文件转Bean对象
- JavaScriptCore全面解析 (下篇)
- MySql查询系统时间,SQLServer查询系统时间,Oracle查询系统时间
- JSP中页面跳转response.sendRedirect()和request.getRequestDispatcher()的区别
- delete引起的debug错误(有关内存释放的一些问题)
- Prometheus 实战于源码分析之collector
- 神经网络:比原来更容易学习了
- 使用Spring封装的Hibernate findByExample方法
- WMS系统