JFinal整合Spring開發

思路大概是這樣子的,首先須要初始化Spring的容器,把全部註解類加入到容器中,Spring裏的AnnotationConfigApplicationContext類完成了這一步,只需傳入包路徑就能完成咱們須要的操做,java

因此SpringPlugin的實現是這樣的spring

package com.nmtx.plugins.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.jfinal.plugin.IPlugin;

/**
 * spring
 * 
 * @author lianghao
 *
 */
public class SpringPlugin implements IPlugin {
	private String[] packages;

	public SpringPlugin(String... packages) {
		this.packages = packages;
	}

	public boolean start() {
		SpringInterceptor.setApplicationContext(new AnnotationConfigApplicationContext(packages));
		return true;
	}

	public boolean stop() {
		return true;
	}

}

上面第一步就完成了,初始化完之後,須要攔截全部的Controller請求,注入全部的Controller屬性,因此須要一個攔截器,具體實現以下app

package com.nmtx.plugins.spring;

import java.lang.reflect.Field;

import javax.annotation.Resource;

import org.springframework.context.ApplicationContext;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

public class SpringInterceptor implements Interceptor {

	private static ApplicationContext applicationContext;

	public void intercept(Invocation ai) {
		Controller controller = ai.getController();
		Field[] fields = controller.getClass().getDeclaredFields();
		for (Field field : fields) {
			Object bean = null;
			Resource resource = field.getAnnotation(Resource.class);
			if (resource != null) {
				String name = resource.name();
				Class<?> classType = resource.type();
				if (!classType.getName().equals("java.lang.Object")) {
					bean = applicationContext.getBean(classType);
				} else if (StrKit.notBlank(resource.name())) {
					bean = applicationContext.getBean(name);
				} else {
					bean = applicationContext.getBean(field.getName() + "Impl");
				}
				try {
					if (bean != null) {
						field.setAccessible(true);
						field.set(controller, bean);
					}
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
		}
		ai.invoke();
	}

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public static void setApplicationContext(ApplicationContext applicationContext) {
		SpringInterceptor.applicationContext = applicationContext;
	}

}

通過這兩步,Spring集成就完成了,接下來就是怎麼使用了,無需添加任何xml配置,只需添加以下配置便可ide

/**
	 * 定義插件,指定掃描路徑
	 */
	@Override
	public void configPlugin(Plugins me) {
		me.add(new SpringPlugin("com.nmtx.manager"));
	}

    /**
	 * 添加全局攔截器
	 */
	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new SpringInterceptor());
	}

在Controller中和SpringMVC使用就沒什麼太大區別了具體使用以下this

Controller層插件

package com.nmtx.manager.controller.permission;


import javax.annotation.Resource;



public class UserController extends Controller {
   
     /** 只需一個註解**/
	 @Resource
	 private UserService userService;


}

Service層code

/**也是一個註解**、
@Service
public class UserServiceImpl implements UserService {
    
    @Resource
	private User userDao;
}

Dao層xml

@Repository
public class User extends Model<User> {

}

Spring包依賴get

<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.2.5.RELEASE</version>
			<scope>provided</scope>
		</dependency>
相關文章
相關標籤/搜索