@Autowired和@Resource都是通过注解的方式自动装配对象
@Autowired是按类型装配
package spring.annotations.repository;
public interface UserRepository {
public void save();
}
package spring.annotations.repository;
import org.springframework.stereotype.Repository;
@Repository //让spring容器扫描配置文件并自动实例化
public class UserRepositoryImpl implements UserRepository {
@Override
public void save() {
System.out.println("save user");
}
}
package spring.annotations.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import spring.annotations.repository.UserRepository;
@Service
public class UserService {
@Autowired //按类型注入实例化对象
UserRepository userRepository;
public void add(){
System.out.println("UserService add");
userRepository.save();
}
}
1、当UserRepositoryImpl没有添加自动扫描注解 NoSuchBeanDefinitionException,bean对象未被定义
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [spring.annotations.repository.UserRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2、可以添加(required=false)解决启动报错的问题
3、当再建一个UserJdbcRepositoryImpl 实现UserRepository
package spring.annotations.repository;
import org.springframework.stereotype.Repository;
@Repository
public class UserJdbcRepositoryImpl implements UserRepository {
@Override
public void save() {
System.out.println("save user");
}
}
启动时候会报不止一个bean实例
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [spring.annotations.repository.UserRepository] is defined: expected single matching bean but found 2: userJdbcRepositoryImpl,userRepositoryImpl
解决方法:
a、在userRepositoryImpl类中添加指定bean的名称
package spring.annotations.repository;
import org.springframework.stereotype.Repository;
@Repository("userRepository") //指定bean名称
public class UserJdbcRepositoryImpl implements UserRepository {
@Override
public void save() {
System.out.println("jdbc save user");
}
}
b、添加 @Qualifier注解,指定装配具体的bean
package spring.annotations.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import spring.annotations.repository.UserRepository;
@Service
public class UserService {
@Autowired(required=false)
@Qualifier("userRepositoryImpl")
UserRepository userRepository;
public void add(){
System.out.println("UserService add");
userRepository.save();
}
}
c、使用@Resource注解,指定装配bean的名称
package spring.annotations.service;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import spring.annotations.repository.UserRepository;
@Service
public class UserService {
@Resource(name="userJdbcRepositoryImpl")
UserRepository userRepository;
public void add(){
System.out.println("UserService add");
userRepository.save();
}
}