我配了两个数据源
db.properties配置如下:
mysql
db.url=jdbc:mysql://XX.XX.XX.XX:XXXX/nutzwk?useUnicode=true&characterEncoding=utf8
db.username=root
db.password=XXXXXXXX
db.validationQuery=select 1
db.maxActive=100
db.testWhileIdle=true
db.filters=mergeStat
db.connectionProperties=druid.stat.slowSqlMillis=2000
db.defaultAutoCommit=true
db2.url=jdbc:mysql://XX.XX.XX.XX:XXXX/ca?useUnicode=true&characterEncoding=utf8
db2.username=root
db2.password=XXXXXXXX
db2.validationQuery=select 1
db2.maxActive=100
db2.testWhileIdle=true
db2.filters=mergeStat
db2.connectionProperties=druid.stat.slowSqlMillis=2000
db2.defaultAutoCommit=true
dao.js配置如下:
var ioc = {
conf : {
type : "org.nutz.ioc.impl.PropertiesProxy",
fields : {
paths : ["config/custom/"]
}
},
dataSource2 : {
factory : "$conf#make",
args : ["com.alibaba.druid.pool.DruidDataSource", "db2."],
type : "com.alibaba.druid.pool.DruidDataSource",
events : {
create : "init",
depose : 'close'
}
},
dao2 : {
type : "org.nutz.dao.impl.NutDao",
args : [{refer:"dataSource2"}]
/*
fields : {
executor : {refer:"cacheExecutor"}
}
/
},
dataSource : {
factory : "$conf#make",
args : ["com.alibaba.druid.pool.DruidDataSource", "db."],
type : "com.alibaba.druid.pool.DruidDataSource",
events : {
create : "init",
depose : 'close'
}
},
dao : {
type : "org.nutz.dao.impl.NutDao",
args : [{refer:"dataSource"}],
fields : {
executor : {refer:"cacheExecutor"}
}
},
cacheExecutor : {
type : "org.nutz.plugins.cache.dao.CachedNutDaoExecutor",
fields : {
cacheProvider : {refer:"cacheProvider"},
cachedTableNames : ["sys_user", "sys_role", "sys_menu"]
}
},
/
// 基于内存的简单LRU实现
cacheProvider : {
type : "org.nutz.plugins.cache.dao.impl.provider.MemoryDaoCacheProvider",
fields : {
cacheSize : 10000 // 缓存的对象数
},
events : {
create : "init"
}
}
/
// 基于Ehcache的DaoCacheProvider
cacheProvider : {
type : "org.nutz.plugins.cache.dao.impl.provider.EhcacheDaoCacheProvider",
fields : {
cacheManager : {refer:"cacheManager"} // 引用ehcache.json中定义的CacheManager
},
events : {
create : "init"
}
}
};
Service类代码如下:
package com.alphacos.ca.services.ib;
import cn.wizzer.common.base.Service;
import com.alphacos.ca.models.ib.Ib_trade;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.IocBean;
/*
* @author Lampard
* @time 2017-01-10 14:48:05
*
*/
@IocBean(args={"dao:refer:dao2"})
public class IbTradeService extends Service {
public IbTradeService(Dao dao) {
super(dao);
}
}
Control类代码如下:
package com.alphacos.ca.controllers.platform.ib;
import cn.wizzer.common.annotation.SLog;
import cn.wizzer.common.base.Result;
import cn.wizzer.common.filter.PrivateFilter;
import cn.wizzer.common.page.DataTableColumn;
import cn.wizzer.common.page.DataTableOrder;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import com.alphacos.ca.models.ib.Ib_trade;
import com.alphacos.ca.services.ib.IbTradeService;
/**
* @author Lampard
* @time 2017-01-10 14:48:05
*
*/
@IocBean
@At("/platform/ib/ibtrade")
@Filters({ @By(type = PrivateFilter.class)})
public class IbTradeController {
private static final Log log = Logs.get();
@Inject
IbTradeService ibTradeService;
@At("")
@Ok("beetl:/platform/ib/index.html")
@RequiresAuthentication
public void index() {
}
@At
@Ok("beetl:/platform/ib/add.html")
@RequiresAuthentication
public void add() {
}
@At
@Ok("json")
@RequiresPermissions("platform.ib.add")
@SLog(tag = "Add", msg = "Add:IB_TRADE")
public Object addDo(@Param("..") Ib_trade ib_trade, HttpServletRequest req) {
try {
ibTradeService.insert(ib_trade);
return Result.success("system.success");
} catch (Exception e) {
return Result.error("system.error");
}
}
@At("/edit/?")
@Ok("beetl:/platform/ib/edit.html")
@RequiresAuthentication
public Object edit(String id) {
return ibTradeService.fetch(id);
}
@At
@Ok("json")
@RequiresPermissions("platform.ib.edit")
@SLog(tag = "Edit", msg = "Edit:IB_TRADE")
public Object editDo(@Param("..") Ib_trade ib_trade, HttpServletRequest req) {
try {
ibTradeService.updateIgnoreNull(ib_trade);
return Result.success("system.success");
} catch (Exception e) {
return Result.error("system.error");
}
}
@At({"/delete","/delete/?"})
@Ok("json")
@RequiresPermissions("platform.ib.delete")
@SLog(tag = "Delete", msg = "Delete:IB_TRADE")
public Object delete(String id,@Param("ids") String[] ids, HttpServletRequest req) {
try {
if(ids!=null&&ids.length>0){
ibTradeService.delete(ids);
}else{
ibTradeService.delete(id);
}
return Result.success("system.success");
} catch (Exception e) {
return Result.error("system.error");
}
}
@At
@Ok("json:full")
@RequiresAuthentication
public Object data(@Param("length") int length, @Param("start") int start, @Param("draw") int draw, @Param("::order") List<DataTableOrder> order, @Param("::columns") List<DataTableColumn> columns) {
Cnd cnd = Cnd.NEW();
return ibTradeService.data(length, start, draw, order, columns, cnd, null);
}
}
编译加载没有报错,第一个数据源所有表能够正常使用,第二个数据源报错:
2017-01-10 23:39:52,445 cn.wizzer.common.plugin.PluginAopConfigure.getInterceptorPairList(PluginAopConfigure.java:33) DEBUG - skip -- com.alphacos.ca.controllers.platform.ib.IbTradeController
2017-01-10 23:39:52,446 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'ibTradeController' to [app]
2017-01-10 23:39:52,446 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'ibTradeService'<class com.alphacos.ca.services.ib.IbTradeService>
2017-01-10 23:39:52,446 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) DEBUG - >> Load definition name=ibTradeService
2017-01-10 23:39:52,446 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:169) DEBUG - Found IocObject(ibTradeService) in AnnotationIocLoader(packages=[cn.wizzer, com.alphacos.ca])
2017-01-10 23:39:52,447 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) DEBUG - >> Make...'ibTradeService'<class com.alphacos.ca.services.ib.IbTradeService>
2017-01-10 23:39:52,447 cn.wizzer.common.plugin.PluginAopConfigure.getInterceptorPairList(PluginAopConfigure.java:33) DEBUG - skip -- com.alphacos.ca.services.ib.IbTradeService
2017-01-10 23:39:52,447 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) DEBUG - Load class com.alphacos.ca.services.ib.IbTradeService without AOP
2017-01-10 23:39:52,447 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'ibTradeService' to [app]
2017-01-10 23:39:52,447 org.nutz.ioc.impl.ScopeContext.remove(ScopeContext.java:84) DEBUG - Remove object 'ibTradeService' from [app]
2017-01-10 23:39:52,447 org.nutz.ioc.impl.ScopeContext.remove(ScopeContext.java:84) DEBUG - Remove object 'ibTradeController' from [app]
2017-01-10 23:39:52,447 cn.wizzer.common.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) DEBUG - [GET ]URI=/nutzwk/platform/ib/ibtrade 3ms
2017-01-10 23:39:52,447 org.nutz.mvc.impl.processor.FailProcessor.process(FailProcessor.java:28) WARN - Error@/platform/ib/ibtrade :
org.nutz.ioc.IocException: [ibTradeController, ibTradeService] # FAIL to create Ioc Bean name=[ibTradeService]
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:149)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:26)
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:31)
at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:56)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:135)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:116)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at cn.wizzer.common.processor.GlobalsSettingProcessor.process(GlobalsSettingProcessor.java:37)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at cn.wizzer.common.processor.LogTimeProcessor.process(LogTimeProcessor.java:21)
at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:198)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at cn.wizzer.common.filter.RouteFilter.doFilter(RouteFilter.java:33)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:789)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1437)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.nutz.castor.FailToCastObjectException: Fail to cast from <java.lang.String> to <org.nutz.dao.Dao> for {dao:refer:dao2} because:
BorningException:Fail to born 'org.nutz.dao.Dao'
by args: [
@(dao:refer:dao2)] because: No suitable Constructor or Factory Method!!
at org.nutz.castor.Castors.cast(Castors.java:264)
at org.nutz.castor.Castors.castTo(Castors.java:318)
at org.nutz.lang.Lang.array2ObjectArray(Lang.java:1122)
at org.nutz.lang.born.Borns.evalWithArgs(Borns.java:97)
at org.nutz.lang.born.Borns.eval(Borns.java:58)
at org.nutz.lang.Mirror.getBorning(Mirror.java:952)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:107)
... 56 more
Caused by: org.nutz.lang.born.BorningException: Fail to born 'org.nutz.dao.Dao'
by args: [
@(dao:refer:dao2)] because: No suitable Constructor or Factory Method!!
at org.nutz.lang.Mirror.born(Mirror.java:988)
at org.nutz.castor.castor.String2Object.cast(String2Object.java:16)
at org.nutz.castor.castor.String2Object.cast(String2Object.java:9)
at org.nutz.castor.Castors.cast(Castors.java:252)
... 62 more