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子节点
①、基于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学习笔记三: 通过注解配置Bean
- spring学习笔记----通过注解配置Bean(2)
- Spring学习笔记之通过注解配置Bean
- Spring4学习笔记-通过注解配置bean
- Spring学习笔记三: 通过注解配置Bean
- spring学习笔记----通过注解配置Bean(1)
- Spring4学习笔记-通过注解配置bean
- Spring学习笔记之通过注解配置Bean(2)
- Spring学习--通过注解配置 Bean (一)
- spring学习笔记九 通过工厂方法配置bean
- Spring学习笔记----注解方式配置bean
- spring笔记11---通过注解的方式配置bean
- [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
- Spring学习笔记之通过FactoryBean配置Bean
- spring学习笔记(12)——使用注解方式配置bean
- spring学习笔记02_注解配置Bean_AOP_
- spring学习十一 通过注解配置bean
- spring学习笔记十 通过factoryBean配置bean
- Spring学习--通过注解配置 Bean (二)
- 13.Spring学习笔记_通过注解配置Bean(by尚硅谷_佟刚)