Score
0
Watch 15 Star 27 Fork 4

northern / JFinal_IOCJava

Join us
Explore and code with more than 2 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
JFinal的ioc插件。实现注解扫描自动注入~~ spread retract

Clone or download
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README.md

JFinal的ioc插件

JFinal_ioc是JFinal的ioc插件,目的就是为了像spring的注入功能一样方便简单。


习惯在spring下通过注解的方式扫描service层,然后在controller的注入使用,在JFinal下自己写个插件和一个全局拦截器来实习这些功能。
希望各位大神给予宝贵意见

遵循JFinal的传统,依然是0配置,so bean的添加在JFinalConfig的继承类中

JFinal_IOC插件
可以通过add()方法和addPackage()方法添加bean
bean可实现单例模式。可以自定义获取bean的key值
获取bean的方法getBean(String key)、getBean(Class<?> key)
当然最重要的功能是可以通过注解@Server和@Autowired来实现ioc

示例

@Override
public void configPlugin(Plugins me) {

	//添加插件
	Ioc ioc = Ioc.getIoc();
	me.add(ioc);
	//自动扫描com.blog.service下有@Server注解的类,后一个参数为true时为single模式
	ioc.addPackage("com.blog.service",true);
	//添加单个bean
	//ioc.add("articleManager",ArticleManager.class,false);
}

@Override
public void configInterceptor(Interceptors me) {
	//配置全局拦截器,当controller里字段加@Autowired注解后自动根据其类型或value自动注入
	me.add(new AutowiredInterceptor());
}

/**
*Controller类
*/
public class BlogController extends Controller {

	//两种注入都可以
	@Autowired
	private ArticleManager articleManager;
	//= (ArticleManager)Ioc.getBean(ArticleManager.class);

测试用的类在com.test.Test里有

package com.test;


import java.lang.reflect.Field;

import com.anna.config.Ioc.Autowired;
import com.anna.config.Ioc.Ioc;
import com.blog.service.ArticleManager;

public class Test {
	

	public void print(){
		System.out.println("Hello Ioc");
	}
	
	public static void main(String...args){
		Ioc ioc = Ioc.getIoc();
		
		ioc.addPackage("com.blog.service",true);
		ioc.add("test",Test.class,true);
		Test test = (Test) Ioc.getBean("test");
		
		ArticleManager articleManager = (ArticleManager)Ioc.getBean(ArticleManager.class);
		
		articleManager.pri();
		
		test.print();
	}
}

Comments ( 3 )

Sign in for post a comment

Java
1
https://gitee.com/northersong/JFinal_IOC.git
git@gitee.com:northersong/JFinal_IOC.git
northersong
JFinal_IOC
JFinal_IOC
master

Help Search