组件装配
-
< context:component-scan >元素还会自动注册AutowiredAnnotationBeanPostProcessor实例,该实例可以自动装配具有@Autowired和@Resource、@Inject注解的属性
-
@Autowired注解自动装配具有兼容类型的单个Bean属性
- 构造器,普通字段(即使是非public),一切具有参数的方法都可以应用@Authwired注解
- 默认情况下,所有使用@Authwired注解的属性都需要被设置.当Spring找不到匹配的Bean装配属性时,会抛出异常, 若某一属性允许不被设置,可以设置@Authwired注解的required属性为false
- 默认情况下,当IOC容器里存在多个类型兼容的Bean时,通过类型的自动装配将无法工作。此时可以在**@Qualifier**注解里提供Bean的名称。Spring允许对方法入参标注@Qualifiter已指定注入Bean的名称
- @Authwired注解也可以应用在数组类型的属性上,此时Spring将会把所有匹配的Bean进行自动装配
- @Authwired注解也可以应用在集合属性上,此时Spring读取该集合的类型信息,然后自动装配所有与之兼容的Bean
- @Authwired注解用在java.util.Map上时,若该Map的键值为String,那么Spring将自动装配与之Map值类型兼容的Bean,此时Bean的名称作为键值
-
### 使用@Resource或@Inject自动装配Bean,- Spring还支持@Resource和@Inject注解,这两个注解和@Autowired注解的功能类似,- @Resource注解要求提供一个Bean名称的属性,若该属性为空,则自动采用标注的标量或方法名作为Bean的名称,- @Inject和@Autowired注解一样也是按类型匹配注入的Bean,但没有reqired属性,- 建议使用@Autowired注解
例子程序
基本结构
还有一个上文的配置文件.xml
TestObject.java
package com.spring.beans.annotation;
public class TestObject {
}
UserRepository.java
package com.spring.beans.annotation.repository;
public interface UserRepository {
void save();
}
UserRepositoryImpl.java
package com.spring.beans.annotation.repository;
import com.spring.beans.annotation.TestObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository("userRepository")//这里的名字可以让多个repository存在时,找到这个repository
public class UserRepositoryImpl implements UserRepository {
@Autowired(required = false)
private TestObject testObject;
@Override
public void save() {
System.out.println("UserRepository Save...");
System.out.println(testObject);
}
}
UserJdbcRepository.java
package com.spring.beans.annotation.repository;
import org.springframework.stereotype.Repository;
@Repository
public class UserJdbcRepository implements UserRepository{
@Override
public void save() {
System.out.println("UserJdbcRepository save...");
}
}
UserService.java
package com.spring.beans.annotation.service;
import com.spring.beans.annotation.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
@Qualifier("userRepositoryImpl") //指定装配哪一个repository
private UserRepository userRepository;
//另外一种方法
// @Autowired
// public void setUserRepository(@Qualifier("userRepositoryImpl") UserRepository userRepository) {
// this.userRepository = userRepository;
// }
public void add(){
System.out.println("UserService add...");
userRepository.save();
}
}
beans-annotation.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--指定spring IOC 容器扫描的包-->
<!--可以通过resource-pattern指定扫描的资源-->
<!--<context:component-scan
base-package="com.spring.beans.annotation"
resource-pattern="repository/*.class">
</context:component-scan>-->
<!--exclude-filter 子节点指定排除哪些指定表达式的组件-->
<!--context:include-filter 子节点指定包含哪些表达式的组件,该子节点需要 use-default-filters 配合使用-->
<context:component-scan
base-package="com.spring.beans.annotation">
<!-- 只包含时需要添加这个属性use-default-filters="false"-->
<!--不包含<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>-->
<!--只包含<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>-->
<!--不包含这个接口以及接口的实现类<context:exclude-filter type="assignable" expression="com.spring.beans.annotation.repository.UserRepository"/>-->
<!--只包含这个接口以及接口的实现类<context:include-filter type="assignable" expression="com.spring.beans.annotation.repository.UserRepository"/>-->
</context:component-scan>
</beans>
Main.java
package com.spring.beans.annotation;
import com.spring.beans.annotation.controller.UserController;
import com.spring.beans.annotation.repository.UserRepository;
import com.spring.beans.annotation.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml");
// TestObject to = (TestObject) ctx.getBean("testObject");
// System.out.println(to);
UserController userController =(UserController) ctx.getBean("userController");
System.out.println(userController);
userController.execute();
// UserService userService = (UserService) ctx.getBean("userService");
// System.out.println(userService);
//
// UserRepository userRepository = (UserRepository) ctx.getBean("userRepository");
// System.out.println(userRepository);
}
}
运行结果