Spring与SpringMVC注解的理解

https://www.cnblogs.com/fightingcoding/p/component-scan.html

@Service  告诉spring容器,这是一个Service类,标识持久层Bean组件,默认情况会自动加载它到spring容器中。 
@Autowried  告诉spring,这个字段需要自动注入 
@Scope  指定此spring bean的scope是单例 (默认单例singleton,不创建新的实例)
@Repository: 注解指定此类是一个容器类,是dao层类的实现。标识持久层Bean组件 
@Componet: 基本注解,标识一个受Spring管理的Bean组件 
@Controller:  标识表现层Bean组件

context.component-scan节点

base-package属性告诉spring要扫描的包 
use-default-filters=”false”表示不要使用默认的过滤器,自己在下面指定。此处的默认过滤器,会扫描包含Service,Component,Responsitory,Controller注释修饰类。

**context:component-scan**查找注解
context:component-scan查找使用构造型(stereotype)注解所标注的类,如@Component(组件),@Service(服务),@Controller(控制器),@Repository(数据仓库)

通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,
如果扫描到文件中带有@Service,@Component,@Repository,@Controller等这些注解的类,则把这些类注册为bean 。
注:在注解后加上例如@Component(value=”abc”)时,注册的这个类的bean的id就是adc。在别处通过abc就可以访问该类。

实例:

https://www.cnblogs.com/caoyc/p/5626365.html
https://www.cnblogs.com/exe19/p/5391712.html
https://blog.csdn.net/qwe5810658/article/details/74343228 也讲user-defaule-filter=“false”

在ssm整合时,使用注解方式对相关bean进行管理,此时出现一个问题:
spring ioc容器 和 spring mvc容器 两个容器对bean管理的问题,
一般情况下都是单单使用springmvc容器对@Controller注解标识的类进行管理,
其他的类如@Service、@Component等注解标识的类由spring来管理,
(spring mvc容器中的类可以引用spring ioc中的类,反过来则不行),

此时springMVC在配置扫描包时的配置如下:
<context:component-scan base-package=“com.ssm.user” use-default-filters=“false”>
<context:include-filter type=“annotation”
expression=“org.springframework.stereotype.Controller”/>
</context:component-scan>
以上配置指示扫描器单单扫描context:include-filter指定的类即@Controller注解指定的类,因为已经指定use-default-filters="false"不使用默认的filters,默认filters为全部的注解包括了@Controller、@Service等
https://blog.csdn.net/wwwffy/article/details/78869470

猜你喜欢

转载自blog.csdn.net/duanbaoke/article/details/88547295