1.(在 classpath中扫描组件)
•组件扫描(componentscanning): Spring 能够从 classpath下自动扫描, 侦测和实例化具有特定注解的组件.
•特定组件包括:
–@Component: 基本注解, 标识了一个受 Spring 管理的组件
–@Respository: 标识持久层组件
–@Service: 标识服务层(业务层)组件
–@Controller: 标识表现层组件
•对于扫描到的组件, Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称
•当在组件类上使用了特定的注解之后, 还需要在 Spring 的配置文件中声明 <context:component-scan>:
–base-package 属性指定一个需要扫描的基类包,Spring 容器将会扫描这个基类包里及其子包中的所有类.
–当需要扫描多个包时, 可以使用逗号分隔.
–如果仅希望扫描特定的类而非基包下的所有类,可使用 resource-pattern 属性过滤特定的类
<!--指定Spring IOC容器扫描的包-->
<!--可以通过resource-pattern指定扫描的资源-->
<!--<context:component-scan base-package="com.spring.annotation" resource-pattern="service/*.class"></context:component-scan>-->
–<context:include-filter> 子节点表示要包含的目标类
–<context:exclude-filter> 子节点表示要排除在外的目标类
–<context:component-scan> 下可以拥有若干个 <context:include-filter> 和 <context:exclude-filter> 子节点
•<context:include-filter> 和 <context:exclude-filter> 子节点支持多种类型的过滤表达式:
package com.spring.annotation.repository;
/**
* Created by Administrator on 2018/4/9.
*/
public interface UserRepository {
void save();
}
package com.spring.annotation.repository;
import org.springframework.stereotype.Repository;
/**
* Created by Administrator on 2018/4/9.
*/
@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository{
public void save(){
System.out.println("UserRepositoryImp的save");
}
}
package com.spring.annotation.service;
import com.spring.annotation.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by Administrator on 2018/4/9.
*/
@Service
public class UserService {
public void save(){
System.out.println("UserService的save方法");
}
}
package com.spring.annotation.service;
import com.spring.annotation.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by Administrator on 2018/4/9.
*/
@Service
public class UserService {
public void save(){
System.out.println("UserService的save方法");
}
}
注意:在使用<context:include-filter>要将Spring默认的过滤器关掉:
2.(组件装配)
•<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 的名称作为键值
•Spring 还支持 @Resource 和 @Inject 注解,这两个注解和 @Autowired注解的功用类似
•@Resource 注解要求提供一个 Bean 名称的属性,若该属性为空,则自动采用标注处的变量或方法名作为Bean 的名称
•@Inject 和 @Autowired注解一样也是按类型匹配注入的 Bean, 但没有 reqired属性
•建议使用 @Autowired注解
package com.spring.annotation.controller;
import com.spring.annotation.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
/**
* Created by Administrator on 2018/4/9.
*/
@Controller
public class UserController {
@Autowired
private UserService userService;
public void execute(){
System.out.println("UserController的execute");
userService.save();;
}
}
package com.spring.annotation.service;
import com.spring.annotation.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by Administrator on 2018/4/9.
*/
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public void save(){
System.out.println("UserService的save方法");
userRepository.save();
}
}
package com.spring.annotation.repository;
import org.springframework.stereotype.Repository;
/**
* Created by Administrator on 2018/4/9.
*/
@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository{
public void save(){
System.out.println("UserRepositoryImp的save");
}
}
如果在使用过程中注入的属性名和bean的名称不一致,可以使用Qualifier指定(以下是两种方式,一种是属性,另一种是方法参数)