Spring基于注解的DI之环境搭建
对于DI使用注解,将不再需要在Spring配置文件中声明Bean实例。
Spring中使用注解,需要在原有Spring运行环境基础之上再做一些改变,这是基本环境的搭建步骤完成以下三个步骤
一、导入AOP的Jar包。因为注解的后台实现用到了AOP编程
spring-aop-4.2.1.RELEASE.jar
二、更换配置文件头,即添加相应的约束
<?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>
以上约束在spring-framework-4.2.1.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html
文件中
三、需要在Spring配置文件中配置组件扫描器,用于在指定的基本包中扫描注解
<?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">
<!-- 配置组件扫描器 -->
<context:component-scan base-package="com.xxx.xxx"></context:component-scan>
</beans>
至此,Spring基于注解的DI之环境搭建完毕