- @Resource默认按照名称方式进行bean匹配,@Autowired默认按照类型方式进行bean匹配
- @Resource(import javax.annotation.Resource;)是J2EE的注解,@Autowired( import org.springframework.beans.factory.annotation.Autowired;)是Spring的注解
Spring属于第三方的,J2EE是Java自己的东西。使用@Resource可以减少代码和Spring之间的耦合。
二、@Resource注入
现在有一个接口Human和两个实现类ManImpl、WomanImpl,在service层的一个bean中要引用了接口Human,这种情况处理如下:接口Human
package testwebapp.com.wangzuojia.service;
public interface Human {
public void speak();
public void walk();
}
实现类ManImpl
package testwebapp.com.wangzuojia.service.impl;
import org.springframework.stereotype.Service;
import testwebapp.com.wangzuojia.service.Human;
@Service
public class ManImpl implements Human {
public void speak() {
System.out.println(" man speaking!");
}
public void walk() {
System.out.println(" man walking!");
}
}
实现类WomanImpl
package testwebapp.com.wangzuojia.service.impl;
import org.springframework.stereotype.Service;
import testwebapp.com.wangzuojia.service.Human;
@Service
public class WomanImpl implements Human {
public void speak() {
System.out.println(" woman speaking!");
}
public void walk() {
System.out.println(" woman walking!");
}
}
主调类SequenceServiceImpl
package testwebapp.com.wangzuojia.service.impl;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import testwebapp.com.wangzuojia.dao.SequenceMapper;
import testwebapp.com.wangzuojia.service.Human;
import testwebapp.com.wangzuojia.service.SequenceService;
@Service
public class SequenceServiceImpl implements SequenceService {
@Resource
private SequenceMapper sequenceMapper;
public void generateId(Map<String, String> map) {
sequenceMapper.generateId(map);
}
//起服务此处会报错
@Resource
private Human human;
}
这时候启动tomcat会包如下错误:
严重: Exception sendingcontext initialized event to listener instance of classorg.springframework.web.context.ContextLoaderListenerorg.springframework.beans.factory.BeanCreationException: Error creating beanwith name 'sequenceServiceImpl': Injection of resource dependencies failed;nested exception isorg.springframework.beans.factory.NoUniqueBeanDefinitionException: Noqualifying bean of type [testwebapp.com.wangzuojia.service.Human] is defined:expected single matching beanbut found 2: manImpl,womanImpl atorg.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:311) atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) 。。。
报错的地方给我们提示了:but found 2: manImpl,womanImpl 意思是Human有两个实现类。解决方案如下:
package testwebapp.com.wangzuojia.service.impl;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import testwebapp.com.wangzuojia.dao.SequenceMapper;
import testwebapp.com.wangzuojia.service.Human;
import testwebapp.com.wangzuojia.service.SequenceService;
@Service
public class SequenceServiceImpl implements SequenceService {
@Resource
private SequenceMapper sequenceMapper;
public void generateId(Map<String, String> map) {
sequenceMapper.generateId(map);
}
@Resource(name = "manImpl")//注意是manImpl不是ManImpl,因为使用@Service,容器为我们创建bean时默认类名首字母小写
private Human human;
}
这样启动服务就不会报错了。如果是使用的@Autowired注解,要配上@Qualifier("manImpl"),代码如下
package testwebapp.com.wangzuojia.service.impl;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import testwebapp.com.wangzuojia.dao.SequenceMapper;
import testwebapp.com.wangzuojia.service.Human;
import testwebapp.com.wangzuojia.service.SequenceService;
@Service
public class SequenceServiceImpl implements SequenceService {
@Resource
private SequenceMapper sequenceMapper;
public void generateId(Map<String, String> map) {
sequenceMapper.generateId(map);
}
@Autowired
@Qualifier("manImpl")
private Human human;
}
从上面的案例当中我们可以了解到如内容:
(1) @Resource 是J2EE当中的注解它在使用的时候使用名称进行注解,并且如果存在多个实现时我们可以根据名字的不同从而选择不同的实现类.@Resource(name=".....")
(2)@Autowited是Spring的注解它在使用的时候,如果存在多个实现类的时候,需要使用@Qualifier("manImpl")这个合格的标志来选择看看应该选择哪个实现类。