import com.duxiu.modules.beetlsql.beetlsqldao;
import org.beetl.sql.core.sqlmanager;
import org.springframework.beans.beansexception;
import org.springframework.beans.factory.support.defaultlistablebeanfactory;
import org.springframework.context.applicationcontext;
import org.springframework.context.applicationcontextaware;
import org.springframework.context.configurableapplicationcontext;
import org.springframework.core.io.resource;
import org.springframework.core.io.support.pathmatchingresourcepatternresolver;
import org.springframework.core.io.support.resourcepatternresolver;
import java.io.ioexception;
import java.util.objects;
import java.util.stream.stream;
public class daofactorybean implements applicationcontextaware {
@override
public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception {
configurableapplicationcontext context = (configurableapplicationcontext) applicationcontext;
defaultlistablebeanfactory beanfactory = (defaultlistablebeanfactory) context.getbeanfactory();
resourcepatternresolver rpr = new pathmatchingresourcepatternresolver(applicationcontext);
sqlmanager sqlmanager = applicationcontext.getbean(sqlmanager.class);
try {
resource[] resources = rpr.getresources("classpath:com/duxiu/**/*.class");
stream.of(resources).map(f -> {
try {
return f.geturi().getpath().split("(classes/)|(!/)")[1].replace("/", ".").replace(".class", "");
} catch (ioexception e) {
e.printstacktrace();
return null;
}
}).filter(objects::nonnull).foreach(f -> {
try {
class aclass = class.forname(f);
boolean match = stream.of(aclass.getannotations()).anymatch(c -> c instanceof beetlsqldao);
if (match && !beanfactory.containsbean(aclass.getsimplename())) {
system.out.println(sqlmanager.getmapper(aclass));
//beanfactory.registersingleton(aclass.getsimplename(),sqlmanager.getmapper(aclass));
}
} catch (classnotfoundexception e) {
e.printstacktrace();
}
});
} catch (ioexception e) {
e.printstacktrace();
}
system.out.println(applicationcontext.getbean(sqlmanager.class));
/*if(!beanfactory.containsbean(beanname)){
beandefinitionbuilder beandefinitionbuilder= beandefinitionbuilder.rootbeandefinition(beanclass);
beandefinitionbuilder.addpropertyvalue("host", host);
beandefinitionbuilder.addpropertyvalue("port", port);
beandefinitionbuilder.addpropertyvalue("database", database);
beandefinitionbuilder.setinitmethodname("init");
beandefinitionbuilder.setdestroymethodname("destroy");
beanfactory.registerbeandefinition(beanname, beandefinitionbuilder.getbeandefinition());
logger.info("add {} to bean container.", beanname);
}*/
}
}
posted on 2016-03-23 10:47
simone 阅读(1644)
评论(0) 编辑 收藏