开启注解代理配置文件
<!-- 指定扫描com.bean包下所有类中的注解(扫描包时会扫描指定包下的所有子孙包) -->
<context:component-scan base-package="com.bean"></context:component-scan>
将注解添加到类中
- 将对象注册到容器中
@Component —- 通用
@Controller —— web层
@Service ———Service层
@Repository——dao层 - 修改对象的作用范围
@Scope(scopeName=”prototype”|”singleton”) - 值类型注入
@Value(”tom”) - 引用类型注入
@Autowired //自动装配
@Qualifier(“user2”) //当匹配多个类型一致的对象,使用会告诉spring容器自动装配哪个对象
@Resource(name=”user3”)//手动注入指定注入哪个名称的对象 - 初始化|销毁方法
@PostConstruct //对象被创建后执行,init-method
@PerDestory //对象销毁之前执行,destory-method