您的位置:首页 > 编程语言 > Java开发

Spring学习笔记之通过注解配置Bean(1)

2017-11-01 23:17 435 查看
1、配置形式

①、基于XML文件的方式;

②、基于注解配置Bean

2、Bean的配置方式

①通过全类名(反射)

②通过工厂方法(静态方法&实例工厂方法)

③FactoryBean

3、特定组件

@Component:基本注解,标识了一个受Spring管理的组件

@Repository:标识持久层组件

@Service:标识服务层(业务层组件)

@Controller:标识表现层组件

当在组件类上使用了特定的注解之后,还需要再Spring的配置文件中声明 context:component-scan

-base-package属性指定一个需要扫描的基类包,Spring容器将会扫描这个基类包里及其所有子包中的所有类,

-当扫描多个包时,可以使用逗号分隔

-如果仅希望扫描特定的类而非所有类,可使用resource-pattern属性过滤特定的类

context:include-filter子节点表示要包含的目标类

context:exclude-fileter子节点表示要排除在外的目标类

context:component-scan下可以拥有若干个context:include-filter和context:exclude-fileter子节点

<!-- 指定Spring IOC 容器扫描的包 -->
<!-- 可以通过resource-pattern扫描指定的资源 -->
<!--  <context:component-scan
base-package="com.jhh.spring.beans.annotation"
resource-pattern="repository/*.class"></context:component-scan>
-->
<!--
use-default-filters true 扫描默认的注解
-->
<context:component-scan base-package="com.jhh.spring.beans.annotation" use-default-filters="false">
<!-- 排除哪些指定表达式的组件
annotation 通过注解去指定包含哪些注解排除哪些注解
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
assignable 通过具体的类去指定包含哪些 排除哪些
<context:exclude-filter type="assignable" expression="com.jhh.spring.beans.annotation.repository.UserRepositoryImpl"/>
-->
<!--
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
-->
<context:include-filter type="assignable" expression="com.jhh.spring.beans.annotation.repository.UserRepositoryImpl"/>
</context:component-scan>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring