Spring开启注解配置

步骤:

  1. 首先要导入spring-aop-4.3.17.RELEASE.jar(使用maven添加)
  2. 在配置文件applicationContext.xml引入新的命名空间(即为xml约束)
  3. 开启注解扫描所要使用注解的类的包名
 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层

posted @ 2020-11-28 20:55  想变强的小王  阅读(1809)  评论(0)    收藏  举报