步驟:
- 首先要導入spring-aop-4.3.17.RELEASE.jar(使用maven添加)
- 在配置文件applicationContext.xml引入新的命名空間(即為xml約束)
- 開啟注解掃描所要使用注解的類的包名
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-4.3.xsd "> 9 10 <!-- base-package即為所要添加注解的類中的包名 --> 11 <context:component-scan base-package="po"></context:component-scan> 12 </beans>
- 在javabean類上使用@Component("id名")
- @Service("id名") service層
- @Controller("id名") web層
- @Repository("id名") dao層