注解开发流程:
1)Spring的注解开发除了四个核心jar还需要aop包的支持,引入pring-aop-4.3.2.RELEASE.jar.
2)在配置文件中引入约束schema.
3)开启注解扫描.
1.注解约束schema:spring-context.xsd配置.
在配置文件中需要引入注解的约束:
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
原文如下:
The context
tags deal with ApplicationContext
configuration that relates to plumbing - that is, not usually beans that are important to an end-user but rather beans that do a lot of grunt work in Spring, such as BeanfactoryPostProcessors
. The following snippet references the correct schema so that the tags in the context
namespace are available to you.
<?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"> <!-- bean definitions here -->
</beans>
2.开启注解扫描:
<!-- 开启注解扫描
到包里面扫描类、方法、属性上面是否有注解 (开发一般建议使用)
-->
<context:component-scan base-package="cn.com.yves"></context:component-scan>
<!--
只扫描属性上面的注解
-->
<!-- <context:annotation-config></context:annotation-config> -->
注意:
- 1.在类上使用注解的时候,默认的是调用的无参构造方法来实例化对象的.
- 2如果是多个不同的包可以在配置多个值在base-package属性中,比如配置扫描cn.com.yves和cn.com.utils包下java类,中间用逗号
,
隔开<context:component-scan base-package="cn.com.yves,cn.com.utils"></context:component-scan>