NutzCN Logo
问答 Ioc注入失败,应该如何处理
发布于 2677天前 作者 文涛(wentao) 3472 次浏览 复制 上一个帖子 下一个帖子
标签:

自己想做个简单的数据统计,不用web,调用ioc注入失败,请问应该如何处理
配置文件 dao.js

var ioc = {
    conf : {
        type : "org.nutz.ioc.impl.PropertiesProxy",
        fields : {
            paths : ["custom/"]
        }
    },
    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"}]
    }
};

db.properties

# db_local
db_local.url=jdbc:mysql://127.0.0.1:3306/work
db_local.username=root
db_local.password=
db_local.validationQuery=select 1
db_local.maxActive=50
db_local.testWhileIdle=true
db_local.filters=mergeStat
db_local.connectionProperties=druid.stat.slowSqlMillis=2000
db_local.defaultAutoCommit=true

代码:

public static Ioc ioc;

    static {
        ioc = new NutIoc(new JsonLoader("ioc/dao.js"));
    }

    public static void main(String[] args) {

        Dao dao = ioc.get(NutDao.class, "dao");
        System.out.println(Json.toJson(dao.query("asset", null)));
    }

报错 信息:

2017-8-24 15:20:9.147 DEBUG [main] Found 1 resource by src( ioc/dao.js ) , regex( ^(.+[.])(js|json)$ )
2017-8-24 15:20:9.149 DEBUG [main] loading ioc js config from [dao.js]
2017-8-24 15:20:9.448 DEBUG [main] Using 95 castor for Castors
2017-8-24 15:20:9.458 DEBUG [main] Loaded 5 bean define from path=[ioc/dao.js] --> [dataSourceDxYufabu, dataSource, dao, conf, dao_dx_yufabu]
2017-8-24 15:20:9.462 INFO [main] NutIoc init begin ...
2017-8-24 15:20:9.469 INFO [main] ... NutIoc init complete
2017-8-24 15:20:9.474 DEBUG [main] Get 'dao'<class org.nutz.dao.impl.NutDao>
2017-8-24 15:20:9.481 DEBUG [main] Load AopConfigure for anno=org.nutz.ioc.aop.Aop by type=org.nutz.ioc.aop.config.impl.AnnotationAopConfigration
2017-8-24 15:20:9.482 DEBUG [main] 	 >> Load definition name=dao
2017-8-24 15:20:9.493 DEBUG [main] Loading define for name=dao
2017-8-24 15:20:9.513 DEBUG [main] Found IocObject(dao) in JsonLoader(paths=[ioc/dao.js])
2017-8-24 15:20:9.513 DEBUG [main] 	 >> Make...'dao'<class org.nutz.dao.impl.NutDao>
2017-8-24 15:20:9.562 DEBUG [main] Load class org.nutz.dao.impl.NutDao without AOP
2017-8-24 15:20:9.563 DEBUG [main] Save object 'dao' to [app] 
2017-8-24 15:20:9.566 DEBUG [main] Get 'dataSource'<>
2017-8-24 15:20:9.566 DEBUG [main] 	 >> Load definition name=dataSource
2017-8-24 15:20:9.567 DEBUG [main] Loading define for name=dataSource
2017-8-24 15:20:9.600 DEBUG [main] Found IocObject(dataSource) in JsonLoader(paths=[ioc/dao.js])
2017-8-24 15:20:9.600 DEBUG [main] 	 >> Make...'dataSource'<>
2017-8-24 15:20:9.688 DEBUG [main] Load class com.alibaba.druid.pool.DruidDataSource without AOP
2017-8-24 15:20:9.688 DEBUG [main] Save object 'dataSource' to [app] 
2017-8-24 15:20:9.704 DEBUG [main] Get 'conf'<>
2017-8-24 15:20:9.705 DEBUG [main] 	 >> Load definition name=conf
2017-8-24 15:20:9.705 DEBUG [main] Loading define for name=conf
2017-8-24 15:20:9.723 DEBUG [main] Found IocObject(conf) in JsonLoader(paths=[ioc/dao.js])
2017-8-24 15:20:9.724 DEBUG [main] 	 >> Make...'conf'<>
2017-8-24 15:20:9.727 DEBUG [main] Load class org.nutz.ioc.impl.PropertiesProxy without AOP
2017-8-24 15:20:9.728 DEBUG [main] Save object 'conf' to [app] 
2017-8-24 15:20:9.822 DEBUG [main] Found 1 resource by src( custom/ ) , regex( ^.+[.]properties$ )
2017-8-24 15:20:10.43 DEBUG [main] Remove object 'dataSource' from [app] 
2017-8-24 15:20:10.43 DEBUG [main] Remove object 'dao' from [app] 
Exception in thread "main" org.nutz.ioc.IocException: [dao, dataSource] # FAIL to create Ioc Bean name=[dataSource]
	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.ReferValue.get(ReferValue.java:24)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:75)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:210)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	at demo.hello.MainSetup.main(MainSetup.java:32)
Caused by: java.lang.NullPointerException
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
	at com.alibaba.druid.util.JdbcUtils.createDriver(JdbcUtils.java:559)
	at com.alibaba.druid.pool.DruidDataSource.init(DruidDataSource.java:642)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.nutz.ioc.trigger.MethodEventTrigger.trigger(MethodEventTrigger.java:18)
	at org.nutz.ioc.weaver.DefaultWeaver.onCreate(DefaultWeaver.java:72)
	at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
	... 6 more
9 回复

忘记加mysql驱动了吧??

加了,用jetty:run跑起来没问题
pom.xml文件如下

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.iptv</groupId>
  <artifactId>wentao_tools</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>wentao_tools Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.nutz</groupId>
      <artifactId>nutz</artifactId>
      <version>1.r.60</version>
    </dependency>
    <!-- mysql驱动 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.40</version>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <!-- Druid连接池 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.27</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>wentao_tools</finalName>
    <plugins>
      <plugin>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-maven-plugin</artifactId>
        <version>9.2.19.v20160908</version>
      </plugin>
    </plugins>
  </build>
</project>

db.properties是不是被ide转义了,用记事本或notepad++打开看看

改成这样看看输出啥

    public static void main(String[] args) {
        PropertiesProxy conf = ioc.get(PropertiesProxy.class, "conf");
        System.out.println(conf.get("db_local.url"));
        Class.forName("org.gjt.mm.mysql.Driver");
        Dao dao = ioc.get(NutDao.class, "dao");
        System.out.println(Json.toJson(dao.query("asset", null)));
    }

db_local.url 输出是如下:

jdbc:mysql://127.0.0.1:3306/work

Dao dao = ioc.get(NutDao.class, "dao"); 这行依然报错

加一行

db_local.driverClassName=com.mysql.jdbc.Driver

我知道了, 前缀写错了

args : ["com.alibaba.druid.pool.DruidDataSource", "db_local."], // 原本是 db.

是的,非常感谢,粗心了!

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