组件扫描(component scanning): Spring 能够从 classpath 下自动扫描,
侦测和实例化具有特定注解的组件.
特定组件包括
<context:component-scan base-package="com.anqi.anotation" use-default-filters="false">
<context:include-filter type="assignable"
expression="org.springframework.stereotype.Repository"/>
context:component-scan>
import org.springframework.stereotype.Component;
@Component
public class TestObject {
}
import org.springframework.stereotype.Controller;
@Controller
public class UserController {
public void execute() {
System.out.println("UserController excute");
}
}
public interface UserRepository {
void save();
}
import org.springframework.stereotype.Repository;
@Repository("userRepository")
public class UserRepositoryIml implements UserRepository {
public void save() {
System.out.println("UserRespository save..");
}
}
import org.springframework.stereotype.Service;
@Service
public class UserService {
public void add() {
System.out.println("UserService ..add");
}
}
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new
ClassPathXmlApplicationContext("bean-annotation.xml");
TestObject testObject = (TestObject) ctx.getBean("testObject");
System.out.println(testObject);
UserController userController = (UserController) ctx.getBean("userController");
System.out.println(userController);
UserService userService = (UserService) ctx.getBean("userService");
System.out.println(userService);
UserRepositoryIml userRepositoryIml = (UserRepositoryIml) ctx.getBean("userRepository");
System.out.println(userRepositoryIml);
}
}
@Autowired 注解自动装配具有兼容类型的单个 Bean属性
默认情况下, 当 IOC 容器里存在多个类型兼容的 Bean 时, 通过类型的自动装配将无法工作. 此时可以在 @Qualifier 注解里提供 Bean 的名称. Spring 允许对方法的入参标注 @Qualifiter 已指定注入 Bean 的名称
@Authwired 注解也可以应用在数组类型的属性上, 此时 Spring 将会把所有匹配的 Bean 进行自动装配.
@Authwired 注解也可以应用在集合属性上, 此时 Spring 读取该集合的类型信息, 然后自动装配所有与之兼容的 Bean.
@Authwired 注解用在 java.util.Map 上时, 若该 Map 的键值为 String, 那么 Spring将自动装配与之 Map 值类型兼容的 Bean, 此时 Bean 的名称作为键值
<context:component-scan base-package="com.anqi.anotation">
context:component-scan>
import org.springframework.stereotype.Component;
@Component
public class TestObject {
}
@Controller
public class UserController {
@Autowired
private UserService userService;
public void execute() {
System.out.println("UserController excute");
userService.add();
}
}
public interface UserRepository {
void save();
}
@Repository("userRespository")
//修改bean的名称,在 main 函数 可以 getBean("userRespository")来获取
public class UserRepositoryIml implements UserRepository {
@Autowired
private TestObject testObject;
public void save() {
System.out.println("UserRespository save..");
System.out.println(testObject);
}
}
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
/*
与上方效果一样
@Autowired
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}*/
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
public void add() {
System.out.println("UserService ..add");
userRepository.save();
}
}
假如 TestObject 没有注解,即没被加载到 IOC 中, 那么引用将会报错
public class TestObject {
}
那么如何让引用的值为 null 而不是报错呢
@Repository("userRespository")
public class UserRepositoryIml implements UserRepository {
@Autowired(required=false)
private TestObject testObject;
public void save() {
System.out.println("UserRespository save..");
System.out.println(testObject);
}
}
如果有两个类实现了统一接口呢
@Repository
public class UserJdbcRepositoryIpl implements UserRepository {
public void save() {
System.out.println("UserJdbcRepositoryIpl...");
}
}
@Repository("userRespository")
public class UserRepositoryIml implements UserRepository {
@Autowired(required=false)
private TestObject testObject;
public void save() {
System.out.println("UserRespository save..");
System.out.println(testObject);
}
}
可以采取 @Qualifier(“userJdbcRepositoryIpl”) 来指定 首字母也要小写
@Service
public class UserService {
@Autowired
@Qualifier("userJdbcRepositoryIpl")
private UserRepository userRepository;
/*
与上方效果一样
@Autowired
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}*/
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
public void add() {
System.out.println("UserService ..add");
userRepository.save();
}
}
Spring 4.x 中可以为子类注入子类对应的泛型类型的成员变量的引用
<context:component-scan base-package="com.anqi.generic.di">context:component-scan>
public class BaseRepository<T> {
}
public class BaseService<T> {
@Autowired
private BaseRepository repository;
public void add() {
System.out.println("BaseService add..");
System.out.println(repository);
}
}
@Repository
public class UserRepository extends BaseRepository<User>{
}
@Service
public class UserService extends BaseService<User>{
}
public class User {
}
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new
ClassPathXmlApplicationContext("bean-generic-di.xml");
UserService service = (UserService) ctx.getBean("userService");
service.add();
//BaseService add..
//com.anqi.generic.di.UserRepository@4d3167f4
}
}