学习之spring注解DI疑惑

接口定义

package com;

public interface IPaly {
    void say();
}

接口实现类

package com;

import org.springframework.stereotype.Service;

//@Service(value = "Play") //默认的value为类名首字母小写
@Service
public class Play implements IPaly {

    public void say()
    {
        System.out.println("我是注解!!!"+this.getClass());
    }    
}
package com;

import org.springframework.stereotype.Service;

//@Service(value = "Play2")
@Service
public class Play2 implements IPaly {

    public void say() {
        System.out.println("我是注解!!!" + this.getClass());
    }
}

service接口注入

package com;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

//配置非单例
@Scope("prototype")
@Service
public class Person {
    @Autowired
    @Qualifier("play")
//    @Resource(name="play2")
    private IPaly user;

    public void say() {
        user.say();
    }
}

要么使用限制名,要么使用Resource注入并指定名字,否则会报错

错误描述的意思就是有两个接口实现

 

posted @ 2016-03-10 12:45  wujf  阅读(209)  评论(0编辑  收藏  举报