<?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 "><!-- 通过注解分别创建Dao、Service、Controller(控制器)
通过注解bean上添加某些注解,可以快速的将bean加入到ioc容器中
某个类上添加上任何一个注解都能快速的将这个组件加入到ioc容器管理中;
Spring有四个注解:()就是代表(修改id名字)
@Controller() 控制器;我们推荐给控制器层(servlet包下的这些)的组件加这个注解
@Service() 业务逻辑;我们推荐给业务逻辑层的组件添加这个注解;
@Repository() 给数据库层(持久化层,dao层)的组件添加这个注解
@Component():给不属于以上几层的组件添加这个注解;
@Scope(value=" ")调节单实例还是多实例/*
* 使用注解加入到容器中的组件,和使用配置加入到容器中的组件行为都是默认一样的;
* 1)、组件的id。默认就是组件的类名首字母小写
* 2)、组件的作用域,默认就是单例的
*
* */
注解可以随便加:Spring底层不会去验证你的这个组件,
是否如你注解所说就是一个dao层或者一个servlet层的组件
我们推荐各自层加各自注解;给我们程序员看的;
id:类名首字母小写的名字
使用注解将组件快速的加入到容器中需要几步:
1)、给要添加的组件上标四个注解的 任何一个
2)、告诉Spring自动扫描加了这些注解的组件,依赖Context名称空间
3)、一定要导入aop jar包 支持注解模式的
-->
<!-- context:component-scan :自动组件扫描
base-package:指定扫描的基础包;把基础包及它下面所有的包的所有加了注解的类,自动的扫描进ioc容器中
context:exclude-filter:(可选)扫描的时候可以排除一些不要的组件
type="annotation":指定排除规则:按照注解进行排除。标注了指定注解的组件不要
expression="":注解的全类名
type="assignable":指定排除某个具体的类,按照类排除
expression="":类的全类名
type="regex":还可以写正则表达式
type="aspectj": 后来aspectj表达式
type="custom" :自定义一个TypeFilter;自己写代码决定哪些使用
context:include-filter:(可选)指定扫描包时要包含的类;只扫描进入哪些组件;默认都是全部扫描进来(use-default-filters,true为全扫描进来)
<context:component-scan base-package="">
<context:exclude-filter type="annotation" expression=""/>
<context:include-filter type="annotation" expression=""/>
</context:component-scan>
-->
<context:component-scan base-package="com.guigu" use-default-filters="true">
<context:exclude-filter type="annotation" expression=""/>
<context:include-filter type="annotation" expression=""/>
</context:component-scan>
</beans>
Spring四个注解:Controller、Service、Repository、Component
最新推荐文章于 2023-12-26 22:16:15 发布