java扫盲系列(4)-Spring 基于注解的配置

之前看了Spring可以通过xml配置实现beans的依赖注入,所有class的元数据和依赖通过xml配置会让配置越来越多,越来越复杂。从Spring2.5开始,可以通过注解完成之前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-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <context:annotation-config/>
   <!-- bean definitions go here -->

</beans>

注解必学和理解的几个注解

@Required
@Autowired
@Qualifier
JSR-250 Annotations

详细看 《w3cschool Spring 基于注解的配置》

基于 Java 的配置

这个东西持保留意见,可能是为了些更灵活的应用场景

Spring的事件处理 和自定义事件

这个没什么好说的,就是在beans的生命周期中的各个节点增加些事件触发,具体的看示例更好看懂 w3cschool Spring 中的事件处理

标签: 无

发表评论: