Spring_11_Spring注解开发(Annotation)
Spring注解开发
-
注解环境搭建
-
在Spring4之后要使用注解开发,必须保证AOP的包导入了。
-
使用注解需要Context约束
-
开启注解驱动支持
<context:component-scan base-package="com.shi"/> //添加需要扫描的包
-
* 注解说明
- @Autowired 自动装配通过类型、名字。 如果Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value="xxx")
- @Nullable 字段标记这个注解,说明该字段可以为NULL
- @Resource 自动装配通过名字、类型
- @Component 说明这个类被Spring管理了 就是<bean>
- @Value("施歌") 给属性添加值。 注解可以加在属性上,也可以加载set方法上
- @component有几个衍生注解,其含义和@component一致,都相等于<bean>,只是每层的名字不同
1. Dao层: @Repository
2. Service层: @Service
3. Controller层: @Controller
-@Scope("singleton") 作用域,括号中传作用域值
-@Configuration 将一个类变成bean配置类
- @Bean 返回一个bean
- @ComponentScan("com.shi")扫描包
- 一般bean在XML中进行管理,属性利用注解进行赋值。

浙公网安备 33010602011771号