package com.woyo.reader.cms.remote.service;
import java.lang.reflect.Field;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.stereotype.Service;
import org.springframework.util.ReflectionUtils;
import com.woyoframework.modules.spring.exetend.PropertyConfig;
@Service
public class Processor extends InstantiationAwareBeanPostProcessorAdapter{
public boolean postProcessAfterInstantiation(Object bean, String beanName){
ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() {
@Override
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
RequestFor p =field.getAnnotation(RequestFor.class);
if(p!=null){
String value = p.value();
if(!value.isEmpty()){
System.out.print(value);
}else{
System.out.print("date=="+p.date());
}
}
}
});
return true;
}
}
demo2:
package com.woyoframework.modules.spring.exetend;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import org.springframework.beans.BeansException;
import org.springframework.beans.SimpleTypeConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
import org.springframework.stereotype.Service;
import org.springframework.util.ReflectionUtils;
@Service
public class ConfigAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter {
@Autowired
private ExtendedPropertyPlaceholderConfigurer propertyConfigurer;
private SimpleTypeConverter typeConverter = new SimpleTypeConverter();
/**
* <p>通过config配置变量,bean初始化以后设置properties文件里面的值</p>
* {@inheritDoc}
*/
@Override
public boolean postProcessAfterInstantiation(final Object bean, String beanName) throws BeansException {
ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() {
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
PropertyConfig cfg = field.getAnnotation(PropertyConfig.class);
if (cfg != null) {
if (Modifier.isStatic(field.getModifiers())) {
throw new IllegalStateException("@PropertyConfig annotation is not supported on static fields");
}
String key = cfg.value().length() <= 0 ? field.getName() : cfg.value();
Object value = propertyConfigurer.getProperty(key);
if (cfg.required() && value == null) {
throw new NullPointerException(bean.getClass().getSimpleName() + "." + field.getName()
+ "is requred,but not been configured");
} else if (value != null) {
Object _value = typeConverter.convertIfNecessary(value, field.getType());
ReflectionUtils.makeAccessible(field);
field.set(bean, _value);
}
}
}
});
return true;
}
}
分享到:
相关推荐
java8 源码 Java注解 简介 由于无论在Java后台或者Android开发中我们经常遇到注解这个...通过@Target进行添加到注解中,说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Ann
该demo 主要通过java注解功能和反射机制 来仿xtuils 的数据库功能 和findViewByID 功能
ElementType.ANNOTATION_TYPE /**包**/ ElementType.PACKAGE /**表示该注解能写在类型变量的声明语句中。 java8新增**/ ElementType.TYPE_PARAMETER /**表示该注解能写在使用类型的任何语句中。 java8新增**/ Elem
java 注解的小demo,适合入门 pom,大家可以下载,直接导入到eclipse或idea中即可
│ │ ├── annotation │ │ │ └── PassToken.java │ │ ├── common │ │ │ ├── Result.java │ │ │ └── constants │ │ │ ├── Common.java │ │
springboot+dubbo的四种配置方式:starter、xml、javaconfig、annotation,可从github上下载:https://github.com/denkbug/dubbo-demo
java高手真经 全光盘源代码 打包rar 第1部分(2个程序包) HelloWorld.zip 01.Java入门样例HelloWorld demo.zip 03.Eclipse入门样例...javaannotation.zip 28.Java注释符编程 javafeature.zip 29.Java5.0语言新特性
看到那些要积分的很不酸,发布免费版本。 第1部分(2个程序包) HelloWorld.zip 01.Java入门样例HelloWorld demo.zip 03.Eclipse入门...javaannotation.zip 28.Java注释符编程 javafeature.zip 29.Java5.0语言新特性
使用Annotation和反射实现Bean转SQL语句Demo
看到很多人都分卷打包的,下载很是不方便,还浪费积分,我就整合压缩打包到一个包里面,里面包含全部源码 源码目录如下: ...javaannotation.zip 28.Java注释符编程 javafeature.zip 29.Java5.0语言新特性
spring-boot-thymeleaf:simple spring boot thymeleaf demo spring-boot-jpa-thymeleaf-curd:spring boot + jpa + thymeleaf 增删改查示例 spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-...
Java8特性Demo 代码来源: 主要会介绍如何使用默认接口方法,lambda表达式,方法引用和可复用的annotation。 包括流、功能接口、扩展的map以及新的Date API等。 接口的默认方法 Java8允许开发者通过使用关键字...
Java定义注释演示 如何在Java中定义注释。 在您的IDE中运行Hello.java 。
│ │ │ annotation-processors.bin │ │ │ fileContent.lock │ │ │ │ │ ├─fileHashes │ │ │ fileHashes.bin │ │ │ fileHashes.lock │ │ │ resourceHashesCache.bin │ │ │ │ │ └─...
资料包含day03-itheima11-spring-01-aop-xml-methodinvocation-count,day03-itheima11-spring-02-aop-xml-privilege,day03-itheima11-spring-03-aop-xml-salary,day03-itheima11-spring-04-aop-annotation,day03...
demo-05-spring-annotation spring中常用注解 demo-06-java8 java8的一些新特性 demo-07-netty-tomcat 基于netty实现tomcat demo-08-netty-im 基于netty实现即时聊天器 demo-09-netty-lecture netty 源码分析 demo-...
java实现文件上传下载D的JAR包,含xml文件配置方法,及用到的属性注释