NutzCN Logo
问答 NutzIoc注解为属性赋值问题
发布于 2808天前 作者 蛋蛋的忧伤 2533 次浏览 复制 上一个帖子 下一个帖子
标签:

就随便搞了个demo,然后就有问题...
实体: 哦不,java类:

package demo.hello.entity;

import org.nutz.ioc.Ioc;
import org.nutz.ioc.annotation.InjectName;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

@IocBean
public class Person {
	@Inject("abcc")
	private String name;
	
	private int age;
	private Person friend;
	private Ioc ioc;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Person getFriend() {
		return friend;
	}
	public void setFriend(Person friend) {
		this.friend = friend;
	}
	
	public Ioc getIoc() {
		return ioc;
	}
	public void setIoc(Ioc ioc) {
		this.ioc = ioc;
	}
	public Person(String name){
		this.name = name;
	}
	public Person(){}
	
	public String test(){
		System.out.println("this is test!");
		return "hhh";
	}
	
	public void init(){
		System.out.println("person init...");
	}
	public void fetch(){
		System.out.println("person fetch...");
	}
	public void depose(){
		System.out.println("person depose...");
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", friend=" + friend + ", ioc=" + ioc + "]";
	}
	
}

main调用:

@Test
	public void annotationIocTest(){
		Ioc ioc = new NutIoc(new AnnotationIocLoader("demo.hello.entity"));
		Person p = ioc.get(Person.class,"person");
		System.out.println(p.getName());
	}

报错信息:

2017-4-15 10:58:28.470 INFO [main] Select SystemLog as Nutz.Log implement
2017-4-15 10:58:28.959 DEBUG [main] Locations for Scans:
[Dir[path=F:\temp_workspace\nutzdemo], Jar[path=F:\temp_workspace\nutzdemo\WebContent\WEB-INF\lib\nutz-1.r.60.jar], Dir[path=F:\temp_workspace\nutzdemo\build\classes], Jar[path=D:\apache-tomcat-8.0.36\lib\annotations-api.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\catalina-ant.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\catalina-ha.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\catalina-storeconfig.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\catalina-tribes.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\catalina.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\ecj-4.5.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\el-api.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\jasper-el.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\jasper.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\jsp-api.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\servlet-api.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\tomcat-api.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\tomcat-coyote.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\tomcat-dbcp.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\tomcat-i18n-es.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\tomcat-i18n-fr.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\tomcat-i18n-ja.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\tomcat-jdbc.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\tomcat-jni.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\tomcat-util-scan.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\tomcat-util.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\tomcat-websocket.jar], Jar[path=D:\apache-tomcat-8.0.36\lib\websocket-api.jar], Jar[path=F:\temp_workspace\nutzdemo\WebContent\WEB-INF\lib\mysql-connector-java-5.1.39.jar], Jar[path=F:\开发工具包\eclipse\plugins\org.junit_4.12.0.v201504281640\junit.jar], Jar[path=F:\开发工具包\eclipse\plugins\org.hamcrest.core_1.3.0.v201303031735.jar]]
2017-4-15 10:58:28.970 DEBUG [main] Found 4 resource by src( demo/hello/entity/ ) , regex( ^.+[.]class$ )
2017-4-15 10:58:28.988 DEBUG [main] Found @IocBean : class demo.hello.entity.Person
2017-4-15 10:58:29.2 DEBUG [main] Found @IocBean : class demo.hello.entity.Pet
2017-4-15 10:58:29.77 DEBUG [main] Using 95 castor for Castors
2017-4-15 10:58:29.85 INFO [main] Found 2 classes in 1 base-packages!
beans = ["person", "pet"]
2017-4-15 10:58:29.88 INFO [main] NutIoc init begin ...
2017-4-15 10:58:29.96 INFO [main] ... NutIoc init complete
2017-4-15 10:58:29.97 DEBUG [main] Get 'person'<class demo.hello.entity.Person>
2017-4-15 10:58:29.100 DEBUG [main] Load AopConfigure for anno=org.nutz.ioc.aop.Aop by type=org.nutz.ioc.aop.config.impl.AnnotationAopConfigration
2017-4-15 10:58:29.101 DEBUG [main] 	 >> Load definition name=person
2017-4-15 10:58:29.102 DEBUG [main] Found IocObject(person) in AnnotationIocLoader(packages=[demo.hello.entity])
2017-4-15 10:58:29.102 DEBUG [main] 	 >> Make...'person'<class demo.hello.entity.Person>
2017-4-15 10:58:29.103 DEBUG [main] Load class demo.hello.entity.Person without AOP
2017-4-15 10:58:29.104 DEBUG [main] Save object 'person' to [app] 
2017-4-15 10:58:29.109 DEBUG [main] Get 'abcc'<>
2017-4-15 10:58:29.110 DEBUG [main] 	 >> Load definition name=abcc
2017-4-15 10:58:29.110 DEBUG [main] Remove object 'person' from [app] 
Exception in thread "main" org.nutz.ioc.IocException: [person, abcc] # For object [abcc] - type:[]
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:218)
	at org.nutz.ioc.val.ReferValue.get(ReferValue.java:24)
	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 demo.hello.ioc.NutzIocDemo.main(NutzIocDemo.java:38)
Caused by: org.nutz.ioc.ObjectLoadException: Object 'abcc' without define!
	at org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:174)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:180)
	... 7 more

10 回复

为何会这样写@inject?

因为我看文档是这么玩的,然后我就照抄了...

为对象的字段注入
这个更加简单,比如:

@IocBean
public class MyObject {
    
    @Inject("abcc")
    private String name;
    
    @Inject("true")
    private boolean live;
    
    @Inject("refer:another")
    private AnotherObject obj;
    
    ...
    

文档链接发一下,这种写法已经不支持了

常量就不应该走注入

可是这不是常量啊....我这就是个属性而已啊,我只是想写个demo......

abcd前面加个冒号

对ioc来说就是个常量,固定值,这种支持在未来的版本也许也会删掉

@Inject(":abcc")

demo洗完了.
老大啊,文档写的不够全面啊,虽然相比同类框架,咱们的文档算是很好的,但还需改进啊,希望nutz越来越好.

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