最近学习jfinal和spring整合,总觉的IocInterceptor实现注入有点麻烦,就自己通过ControllerFactory实现注入。
package com.demo.jfinal.core;
import com.jfinal.core.Controller;
import com.demo.common.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
/**
* ControllerFactory
*/
public class ControllerFactory extends com.jfinal.core.ControllerFactory {
private ThreadLocal<Map<Class<? extends Controller>, Controller>> buffers = new ThreadLocal<Map<Class<? extends Controller>, Controller>>() {
protected Map<Class<? extends Controller>, Controller> initialValue() {
return new HashMap<Class<? extends Controller>, Controller>();
}
};
public Controller getController(Class<? extends Controller> controllerClass) throws InstantiationException, IllegalAccessException {
Controller ret = buffers.get().get(controllerClass);
if (ret == null) {
ret = controllerClass.newInstance();
autoInject(ret);
buffers.get().put(controllerClass, ret);
}
return ret;
}
/**
* 自动注入
* @param ret controller
*/
public void autoInject(Controller ret) {
Field[] fields = ret.getClass().getDeclaredFields();
for (Field field : fields) {
Object bean = null;
if (field.isAnnotationPresent(Autowired.class)) {
try {
bean = BeanFactory.getBean(field.getName());
} catch (Exception e) {
}
if (null == bean) {
try {
bean = BeanFactory.getBean(field.getType());
} catch (Exception e) {
}
}
if (null == bean) {
Autowired annotation = field.getAnnotation(Autowired.class);
if (annotation.required()) {
throw new RuntimeException(String.format(
"Error creating bean with name '%s': Injection of autowired dependencies failed.",
field.getName()));
}
}
} else if (field.isAnnotationPresent(Resource.class)) {
Resource annotation = field.getAnnotation(Resource.class);
bean = BeanFactory.getBean(annotation.name().trim().length() == 0 ? field.getName() : annotation.name());
} else
continue;
try {
if (bean != null) {
field.setAccessible(true);
field.set(ret, bean);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}BeanFactory可以通过实现ApplicationContextAware注入ApplicationContext,也可以通过springPlugin直接注入到ControllerFactory。
项目:jfinal注解扫描