<?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-4.3.xsd">
<!-- 通过注解分别创建Dao、Service、Controller(控制网页跳转逻辑servlet)★
通过给bean上添加某些注解,可以迅速的将bean加入到容器中
Spring 中有四个注解
@Controller 控制器;我们推荐给控制器层(servlet包下的这些)的组件加这个注解
@Service 业务逻辑;我们推荐业务逻辑层的组件添加这个注解;BookService
@Repository 给数据库层(持久化层,dao层)的组件添加这个注解
@Component 给不属于以上几层的组件添加这个注解;
注解可以随便加;
Spring底层不会去验证你的这个组件,是否如你注解所说就是一个dao层的或者就是一个servlet层的组件;
1. 添加注解
2. 告诉Spring自动扫描注解:依赖于context名称空间
3. 导入aop包,支持注解模式,否则会报错 classnotfound`
base-package 为添加注解文件类路径下共同包名
若没有共同包名,可以分开添加
eg:导入两个包
<context:component-scan base-package="dao"></context:component-scan>
<context:component-scan base-package="service"></context:component-scan>
# id默认类名首字母小写
# 改id名@Repository("bookdaohaha") 改实例类型 @Scope(value = "prototype")
-->
<!-- 注解和bean配置相结合 -->
<context:component-scan base-package="spring3"></context:component-scan>
</beans>
Spring IOC 通过注解分别创建Dao、Service、Controller
最新推荐文章于 2021-09-16 08:57:02 发布