NutzCN Logo
问答 nutz 只使用 db.properties 创建Datasource 并获取Dao
发布于 2391天前 作者 yy321973351 1756 次浏览 复制 上一个帖子 下一个帖子
标签:

nutz 只使用 db.properties 创建Datasource 并获取Dao

7 回复

不想维护dao.js

使用nutz mvc了吗?? 话说dao.js基本上不需要改动的

使用了 但是不喜欢看见js文件

可以这样写哦

@IocBean
public class MyBeans {

    @IocBean
    public PropertiesProxy conf() {
          return new PropertiesProxy("custom/");
    }
    @IocBean(create="init", depose="close")
    public DataSource dataSource(@Inject PropertiesProxy conf) {
          return conf.make(DruidDataSource.class, "db.");
    }
    @IocBean
    public Dao dao(@Inject DataSource dataSource) {
          return new NutDao(dataSource);
    }
}

其他地方如何获取Dao

18-06-06 16:36:43.752 ERROR [RMI TCP Connection(3)-127.0.0.1] Error happend during start serivce!
java.lang.NullPointerException
	at com.yx.product.AdminStep.init(AdminStep.java:32)
	at org.nutz.mvc.impl.NutLoading.evalSetup(NutLoading.java:278)
	at org.nutz.mvc.impl.NutLoading.load(NutLoading.java:121)
	at org.nutz.mvc.ActionHandler.<init>(ActionHandler.java:19)
	at org.nutz.mvc.NutFilter._init(NutFilter.java:91)
	at org.nutz.mvc.NutFilter.init(NutFilter.java:69)
	at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:285)
	at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:266)
	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4637)
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5282)
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:754)
	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:730)
	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734)
	at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1736)
	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.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
18-06-06 16:36:43.757 INFO [RMI TCP Connection(3)-127.0.0.1] org.nutz.ioc.impl.NutIoc@1689659684 is closing. startup date [18-06-06 16:36:41.857]
	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
18-06-06 16:36:43.757 DEBUG [RMI TCP Connection(3)-127.0.0.1] Depose object 'myDao' ...
	at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
18-06-06 16:36:43.757 DEBUG [RMI TCP Connection(3)-127.0.0.1] Depose object 'conf' ...
	at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:482)
18-06-06 16:36:43.758 DEBUG [RMI TCP Connection(3)-127.0.0.1] Depose object 'dao' ...
	at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:431)
18-06-06 16:36:43.758 DEBUG [RMI TCP Connection(3)-127.0.0.1] Depose object 'dataSource' ...
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
18-06-06 16:36:43.758 INFO [RMI TCP Connection(3)-127.0.0.1] org.nutz.ioc.impl.NutIoc@1689659684 is deposed. startup date [18-06-06 16:36:41.857]
	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.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300)
	at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
	at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
	at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
	at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
	at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
	at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1401)
	at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
	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 sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
	at sun.rmi.transport.Transport$1.run(Transport.java:200)
	at sun.rmi.transport.Transport$1.run(Transport.java:197)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
	at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:748)
18-06-06 16:36:43.756 ERROR [RMI TCP Connection(3)-127.0.0.1] try to depose ioc
06-Jun-2018 16:36:43.762 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal One or more Filters failed to start. Full details will be found in the appropriate container log file
06-Jun-2018 16:36:43.762 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal Context [] startup failed due to previous errors
06-Jun-2018 16:36:43.766 警告 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [ROOT] registered the JDBC driver [com.alibaba.druid.proxy.DruidDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
06-Jun-2018 16:36:43.766 警告 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [ROOT] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
[2018-06-06 04:36:43,783] Artifact product:war exploded: Error during artifact deployment. See server log for details.
06-Jun-2018 16:36:49.483 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [E:\tools\apache-tomcat-8.5.31\webapps\manager]
06-Jun-2018 16:36:49.533 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [E:\tools\apache-tomcat-8.5.31\webapps\manager] has finished in [49] ms


public class ProductServiceImpl implements ProductService { @Inject("dao") protected Dao dao; }
public class AdminStep implements Setup {
    @Inject
    protected ProductService productService;
    @Override
    public void init(NutConfig nc) {
       Ioc ioc= nc.getIoc();
       Dao dao=ioc.get(Dao.class,"dao");
        Daos.createTablesInPackage(dao,"com.yx.product",true);
        for (int i=0;i<15;i++) {
            ProductBean productBean = new ProductBean("1234567"+i, "产品1"
                    , 123.12, "产品规格1", "E:\\image\\01.jpg", "111111111"
                    , "上架");

            try {
                productService.add(productBean, null);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void destroy(NutConfig nc) {

    }
}

在这个地方成功获取Dao

productService 用局部变量,从ioc取,像dao那样

添加回复
请先登陆
回到顶部