/** * aop自动注入注解 * @version 1.0 * @create_at 2017年7月24日下午9:21:12 */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD}) @Documented public @interface AopBean { } package com.information.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) @Documented public @interface Aop { String value() default ""; } //次注解可配合service层拦截器使用 @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD,ElementType.TYPE}) @Documented public @interface Interceptor { String value() default ""; Class<? extends com.jfinal.aop.Interceptor>[] target(); } /** * aop注入插件 * @version 1.0 * @create_at 2017年7月25日上午8:14:05 */ public class AopBeanPlugin<T> implements IPlugin{ private static final Log LOG=Log.getLog(AopBeanPlugin.class); private List<String> beanList=new ArrayList<String>(); @SuppressWarnings("rawtypes") private List<Class> excludeClasses=new ArrayList<Class>(); private boolean autoScan; public AopBeanPlugin<T> setPackageName(String... packageName){ if(packageName.length==0||packageName==null) throw new NullPointerException("packageName can not be null"); for(String pack:packageName){ beanList.add(pack); } return this; } public boolean isAutoScan() { return autoScan; } @SuppressWarnings("rawtypes") public AopBeanPlugin setAutoScan(boolean autoScan) { this.autoScan = autoScan; return this; } @Override public boolean start() { List<Class<? extends T>> targetClass = PackageUtil.scanPackage(beanList); for(Class<? extends T> target:targetClass){ if(excludeClasses.contains(target)){ continue; } Annotation[] annotations = target.getAnnotations(); if(target.getAnnotations() != null){ Object object = null; String value = null; for(Annotation annotation : annotations){ if(annotation instanceof Aop){ object= Duang.duang(target.getName(),target); value=target.getAnnotation(Aop.class).value(); }else if(annotation instanceof Interceptor){ Class<? extends com.jfinal.aop.Interceptor>[] interceptorClass = ((Interceptor) annotation).target(); value =((Interceptor) annotation).value(); object = Duang.duang(target.getName(),target,interceptorClass); } } String simpleName=target.getSimpleName().substring(1, target.getSimpleName().length()); String key=StrKit.toLowerCaseFirst(target.getSimpleName())+simpleName; if(object != null){ if(StrKit.isNotEmpoty(value)){ AopManger.beanMap.put(value, object); }else{ AopManger.beanMap.put(key, object); } LOG.info("create aop bean "+object); } } continue; } return true; } @Override public boolean stop() { return true; } public AopBeanPlugin<T> addExcludeClasses(Class<?>... clazzes) { if (clazzes != null) { for (Class<?> clazz : clazzes) { excludeClasses.add(clazz); } } return this; } } } /** * aop对象注入拦截器 * @version 1.0 * @create_at 2017年7月24日下午9:18:12 */ public class AopInterceptor implements Interceptor{ private ApplicationContext ctx; public AopInterceptor(ApplicationContext ctx){ this.ctx = ctx; } public AopInterceptor(){} @Override public void intercept(Invocation inv) { Controller controller = inv.getController(); Field[] fields = controller.getClass().getDeclaredFields(); //controller层aop的自动注入 for (Field field : fields) { Object bean = null; if (field.isAnnotationPresent(AopBean.class)){ bean = AopManger.beanMap.get(field.getName()); Class<?> cla = field.getType(); //service层aop的自动注入 for(Field f : cla.getDeclaredFields()){ Object serviceBean = null; if(f.isAnnotationPresent(AopBean.class)){ serviceBean = AopManger.beanMap.get(f.getName()); }else if(f.isAnnotationPresent(Autowired.class)){ serviceBean = ctx.getBean(f.getName()); } if(serviceBean != null){ try { f.setAccessible(true); f.set(bean, serviceBean); } catch (IllegalArgumentException | IllegalAccessException e) { throw new NullPointerException("Field can not be null"); } } } }else if(field.isAnnotationPresent(Autowired.class)){ bean = ctx.getBean(field.getName()); }else{ continue ; } try { if (bean != null) { field.setAccessible(true); field.set(controller, bean); } } catch (Exception e) { throw new RuntimeException(e); } } inv.invoke(); } } 具体使用方式如下 @Aop public class SystemAdminService extends BaseService{ } public class LoginController extends BaseController{ @AopBean private SystemRoleService systemRoleService; @Autowired private OnlineManger onlineManger; } 注:(此方法只对controller和service层有效)