本人新手,单数据源分库分表已经可用,但不知道如何添加另外的数据源获取另一个dao。
37 回复
@IocBean
public class MyBeans{
@Inject
protected PropertiesProxy conf;
@Inject
protected AppContext appContext;
@IocBean(name="dao2")
publicDao getDao2() throws Exception {
String path = "shardingjdbc.yaml2";
InputStream ins = appContext.getResourceLoader().get(path);
if (ins == null) {
File f = new File(path);
if (f.exists() && f.canRead()) {
ins = new FileInputStream(f);
} else {
throw new RuntimeException("no such shardingjdbc configure file=" + path);
}
}
return new NutDao(ShardingDataSourceFactory.createDataSource(Streams.readBytesAndClose(ins)));
}
}
protected static String PRE = "shardingjdbc2.";
@PropDoc(group = "shardingjdbc2", value = "配置文件路径", defaultValue = "shardingjdbc2.yaml")
public static final String PROP_PATH = PRE + "path";
@Inject
protected PropertiesProxy conf;
@Inject
protected AppContext appContext;
@IocBean(name="dao2")
public Dao getDao2() throws Exception {
String path = conf.get(PROP_PATH, "shardingjdbc2.yaml");
InputStream ins = appContext.getResourceLoader().get(path);
if (ins == null) {
File f = new File(path);
if (f.exists() && f.canRead()) {
ins = new FileInputStream(f);
} else {
throw new RuntimeException("no such shardingjdbc configure file=" + path);
}
}
return new NutDao(ShardingDataSourceFactory.createDataSource(Streams.readBytesAndClose(ins)));
}
这个写法有问题吗?为什么获取不到文件。
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:240)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:270)
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.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:202)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
at org.nutz.mvc.WhaleFilter.doFilter(WhaleFilter.java:159)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:206)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:530)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:347)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:256)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:247)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:140)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131)
at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:382)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:708)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:626)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.nutz.lang.born.BorningException: Fail to born or cast to 'com.bbieat.pay.module.zhangdanModule'because: java.lang.NullPointerException
at org.nutz.lang.born.EmptyArgsConstructorBorning.born(EmptyArgsConstructorBorning.java:17)
at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:78)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:117)
... 46 more
Caused by: java.lang.NullPointerException
at com.bbieat.pay.bean.MyBeans.getDao2(MyBeans.java:32)
at com.bbieat.pay.module.zhangdanModule.<init>(zhangdanModule.java:33)
at com.bbieat.pay.module.zhangdanModule$FC$62febe830a4a6764c634d9e263964cee.invoke(zhangdanModule.java)
at org.nutz.lang.born.AbstractConstructorBorning.call(AbstractConstructorBorning.java:25)
at org.nutz.lang.born.EmptyArgsConstructorBorning.born(EmptyArgsConstructorBorning.java:14)
... 48 more
@IocBean加上后dao2的配置文件是能获取到了两个都崩了,
dataSources:
ami: !!com.alibaba.druid.pool.DruidDataSource
driverClassName: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@**.***.com:1522:ami
username: *****
password: *****
filters: stat
这Oracle的配置格式是有问题吗
For the reason: Unable to find property 'ami' on class: io.shardingjdbc.core.yaml.sharding.YamlShardingConfiguration
at org.nutz.lang.Invoking.invoke(Invoking.java:195)
at org.nutz.lang.Mirror.invoke(Mirror.java:1120)
at org.nutz.ioc.impl.ObjectMakerImpl$1.born(ObjectMakerImpl.java:80)
at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:78)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:117)
... 51 more
Caused by: org.yaml.snakeyaml.error.YAMLException: Unable to find property 'ami' on class: io.shardingjdbc.core.yaml.sharding.YamlShardingConfiguration
at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:132)
at org.yaml.snakeyaml.introspector.PropertyUtils.getProperty(PropertyUtils.java:121)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.getProperty(Constructor.java:322)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.constructJavaBean2ndStep(Constructor.java:240)
at org.yaml.snakeyaml.constructor.Constructor$ConstructMapping.construct(Constructor.java:189)
at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:345)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182)
at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:141)
at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:127)
at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:450)
at org.yaml.snakeyaml.Yaml.loadAs(Yaml.java:444)
at io.shardingjdbc.core.api.ShardingDataSourceFactory.unmarshal(ShardingDataSourceFactory.java:126)
at io.shardingjdbc.core.api.ShardingDataSourceFactory.createDataSource(ShardingDataSourceFactory.java:98)
at com.bbieat.pay.bean.MyBeans.getDao2(MyBeans.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.nutz.lang.Invoking$NullArgInvoker.invoke(Invoking.java:77)
at org.nutz.lang.Invoking.invoke(Invoking.java:192)
... 55 more
缩进错了
dataSources:
ami:
driverClassName: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@**.***.com:1522:ami
username: *****
password: *****
filters: stat
org.nutz.ioc.IocException: IocBean[zhangdanModule] throw Exception when creating
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:240)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:270)
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.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:202)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
at org.nutz.mvc.WhaleFilter.doFilter(WhaleFilter.java:159)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:206)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:530)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:347)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:256)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:247)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:140)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131)
at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:382)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:708)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:626)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: IocBean[zhangdanModule] fail at field=[dao2]
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:40)
at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:67)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
... 46 more
Caused by: org.nutz.ioc.IocException: IocBean[dao2] throw Exception when creating
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:152)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:240)
at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:60)
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:32)
... 48 more
Caused by: org.nutz.lang.InvokingException: Fail to invoke [com.bbieat.pay.bean.MyBeans].getDao2() by args:
For the reason: null
at org.nutz.lang.Invoking.invoke(Invoking.java:195)
at org.nutz.lang.Mirror.invoke(Mirror.java:1120)
at org.nutz.ioc.impl.ObjectMakerImpl$1.born(ObjectMakerImpl.java:80)
at org.nutz.ioc.weaver.DefaultWeaver.born(DefaultWeaver.java:78)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:117)
... 51 more
Caused by: java.lang.NullPointerException
at io.shardingjdbc.core.yaml.sharding.YamlShardingRuleConfiguration.getShardingRuleConfiguration(YamlShardingRuleConfiguration.java:72)
at io.shardingjdbc.core.yaml.sharding.YamlShardingConfiguration.getShardingRule(YamlShardingConfiguration.java:50)
at io.shardingjdbc.core.api.ShardingDataSourceFactory.createDataSource(ShardingDataSourceFactory.java:99)
at com.bbieat.pay.bean.MyBeans.getDao2(MyBeans.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.nutz.lang.Invoking$NullArgInvoker.invoke(Invoking.java:77)
at org.nutz.lang.Invoking.invoke(Invoking.java:192)
... 55 more
debug看了下conf里面没有shardingjdbc的路径,这段是不是有问题?
protected static String PRE = "shardingjdbc2.";
@PropDoc(group = "shardingjdbc2", value = "配置文件路径", defaultValue = "shardingjdbc2.yaml")
public static final String PROP_PATH = PRE + "path";
@Inject
protected PropertiesProxy conf;
dataSources:
ami: !!com.alibaba.druid.pool.DruidDataSource
driverClassName: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@hz.bbieat.com:1522:ami
username:
password:
filters: stat
shardingRule:
tables:
public ShardingRuleConfiguration getShardingRuleConfiguration() throws SQLException {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
result.setDefaultDataSourceName(defaultDataSourceName);
for (Map.Entry<String, YamlTableRuleConfiguration> entry : tables.entrySet()) { // 这里就是报错的72行, tables不配就是null了
YamlTableRuleConfiguration tableRuleConfig = entry.getValue();
tableRuleConfig.setLogicTable(entry.getKey());
result.getTableRuleConfigs().add(tableRuleConfig.build());
}
添加回复
请先登陆