public @interface Service { }
import java.util.HashMap; import java.util.Map; import com.jfinal.log.Logger; public abstract class ServiceUtils { private final static Logger logger = Logger.getLogger(ServiceUtils.class); private final static Map<Object, Object> map = new HashMap<Object, Object>(); public static void put(Object key, Object value) { if(containsKey(key)){ logger.debug("key:"+key+" instance is exist !!!"); return; } if(containsValue(value)){ logger.debug("value:"+value+" instance is exist !!!"); return; } map.put(key, value); } @SuppressWarnings("unchecked") public static <T> T get(Object key) { return (T)map.get(key); } /** * key 存在,但 value 可能为 null */ public static boolean containsKey(Object key) { return map.containsKey(key); } public static boolean containsValue(Object value) { return map.containsValue(value); } }
import java.util.ArrayList; import java.util.List; import java.util.Set; import org.reflections.Reflections; import com.jfinal.aop.Enhancer; import com.jfinal.kit.StrKit; import com.jfinal.log.Logger; import com.jfinal.plugin.IPlugin; public class ServicePlugin implements IPlugin { private final static Logger logger = Logger.getLogger(ServicePlugin.class); private List<String> basePackages = new ArrayList<String>(); public ServicePlugin addBasePackages( String... basePackages) { for (String basePackage : basePackages) { this.basePackages.add(basePackage); } return this; } public ServicePlugin addBasePackages( List<String> basePackages) { if (basePackages != null) { this.basePackages.addAll(basePackages); } return this; } @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public boolean start() { Reflections reflections = new Reflections(basePackages); Set<Class<?>> serviceClasses = reflections.getTypesAnnotatedWith(Service.class); String serviceName = null; Object object = null; // 循环处理自动注册映射 for (Class service : serviceClasses) { serviceName = service.getSimpleName(); serviceName = StrKit.firstCharToLowerCase(serviceName); object = Enhancer.enhance(service); ServiceUtils.put(serviceName, object); logger.debug("key:"+serviceName+" proxy instance is "+object.getClass()); } return true; } @Override public boolean stop() { return true; } }
import java.lang.reflect.Field; import javax.annotation.Resource; import com.jfinal.aop.Interceptor; import com.jfinal.aop.Invocation; import com.jfinal.core.Controller; public class ServiceInterceptor implements Interceptor { @Override public void intercept(Invocation ai) { Controller controller = ai.getController(); Field[] fields = controller.getClass().getDeclaredFields(); for (Field field : fields) { Object bean = null; if (field.isAnnotationPresent(Resource.class)){ bean = ServiceUtils.get(field.getName()); }else{ continue ; } try { if (bean != null) { field.setAccessible(true); field.set(controller, bean); } } catch (Exception e) { throw new RuntimeException(e); } } ai.invoke(); } }
public class AppConfig extends JFinalConfig { private Routes routes = null; /** * 配置常量 */ @Override public void configConstant(Constants me) { // 上传文件命名规则 OreillyCos.setFileRenamePolicy(new RandomFileRenamePolicy()); } /** * 配置路由 */ @Override public void configRoute(Routes me) { routes = me; } /** * 配置插件 */ @SuppressWarnings("unchecked") @Override public void configPlugin(Plugins me) { //业务层插件 me.add(new ServicePlugin().addBasePackages("com.nlm")); } /** * 配置全局拦截器 */ @Override public void configInterceptor(Interceptors me) { me.addGlobalActionInterceptor(new ServiceInterceptor()); } /** * 配置处理器 */ @Override public void configHandler(Handlers me) { } }
import com.jfinal.aop.Before; import com.jfinal.plugin.activerecord.Db; import com.jfinal.plugin.activerecord.Record; import com.jfinal.plugin.activerecord.tx.Tx; import com.nlm.web.plugin.service.Service; @Service public class UserService { @Before(Tx.class) public void login(){ Record record = new Record() .set("realName", "王五"); Db.save("T_SYS_USER","id", record); record = new Record() .set("code","admin") .set("name","超级管理员"); Db.save("T_SYS_ROLE","id", record); } }
@ControllerKey(value="/login",viewPath="/") public class LoginController extends BaseController { @Resource private UserService userService; public void index(){ userService.login(); render("login.html"); } }