Spring入门——Bean管理的注解实现及例子
来源:互联网 发布:手机克隆软件 编辑:程序博客网 时间:2024/05/17 03:02
- Classpath扫描与组件管理
从Spring3.0开始,Spring javaConfig项目提供了很多特性,包括使用Java而不是xml定义bean,比如@Configuration,@Bean,@Import,@DependsOn
@Component是一个通用注解,可以用于任何bean
@Repository,@Service,@Controller是更有针对性的注解
-@Repository通常用于注解DAO类,即持久层
-@Service通常用于注解Service类,即服务层
-@Controller通常用于Controller类,即控制层(MVC)
- 类的自动检测与注册Bean
Spring可以自动检测类并注册Bean到ApplicationContext中
- <context:annotation-config/>
当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如:
使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean:
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>
使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean:
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
类似地,使用@Resource、@PostConstruct、@PreDestroy等注解就必须声明 CommonAnnotationBeanPostProcessor;使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。
这样的声明未免太不优雅,而Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即使用<context:annotation- config/>隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。
通过在基于XML的Spring配置如下标签(请注意包含上下文命名空间)
<context:annotation-config/>仅会查找到同一个applicationContext中的bean注解为了能够检测这些类并注册相应的Bean,需要下面内容
<context:component-scan>包含<context:annotation-config/>,通常在使用前者后,不再使用后者
使用过滤器进行自定义扫描
默认情况下,类被自动发现并注册bean的条件是:使用@Component,@Repository,@Service,@Controller注解或者使用@component的自定义注解
可以通过过滤器修改上面的行为,如:下面例子的xml配置忽略所有的@Repository注解并用“stub”代替
include表示包含下面的内容,regex是以通配符的形式,exclude表示排除,排除的形式是annotation也就是注解,而这个注解是所有以Repository进行的注解
还可以使用user-default-filters="false"禁用自动发现与注册
定义bean
扫描过程中组件被自动检测,那么Bean名称是由BeanNameGenerator生成的(@Component,@Repository,@Service,@Controller都会有个name属性用于显示设置Bean Name)当没有指定名称是通常生成的名称为首字母小写的类名称,一般我们都会自定义名称
可以定义bean命名策略,实现BeanNameGenerator接口,并一定要包含一个无参数构造函器
作用域(Scope)
通常情况下自动查找的Spring组件,其scope是singleton,Spring2.5提供了一个标识scope的注解@Scope
也可以自定义Scope策略,实现ScopeMetadataResolver接口并提供一个无参构造器
代理方式
可以使用scoped-proxy属性指定代理,有三个值可选:no,interfaces,targetClass默认情况下是no
Bean定义及作用域例子:
配置文件:
<?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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"><context:component-scan base-package="com.txr.beanannotation"/></beans>BeanAnnotation类
package com.txr.beanannotation;import org.springframework.stereotype.Component;@Component("bean")public class BeanAnnotation {public void say(String word){System.out.println("BeanAnnotation :"+word);}}测试
@Testpublic void testAnnotationSay(){BeanAnnotation annotation=(BeanAnnotation)context.getBean("bean");annotation.say("hello txr!!");}测试结果
BeanAnnotationhello txr!!
Scope测试
在BeanAnnotation中添加Scope注解
package com.txr.beanannotation;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;@Scope("prototype")@Component("bean")public class BeanAnnotation {public void say(String word){System.out.println("BeanAnnotation :"+word);}}
测试
@Testpublic void testAnnotationScope(){BeanAnnotation annotation=(BeanAnnotation)context.getBean("bean");BeanAnnotation annotation1=(BeanAnnotation)context.getBean("bean");System.out.println(annotation==annotation1);}
测试结果
false
- 类的自动检测与注册Bean
- <context:annotation-config/>
- @Component,@Repository,@Service,@Controller
- @Required
- @Autowired
- @Qualifier
- @Resource
0 0
- Spring入门——Bean管理的注解实现及例子
- Spring的bean管理——注解注入属性
- Spring的bean管理(注解)
- Spring入门(Bean的定义及作用域的注解实现)
- spring入门(注解实现Bean的定义)
- Spring学习笔记(10.Spring Bean装配之Bean的定义域及注解实现)
- Spring的bean管理(注解)
- Spring的bean管理(注解方式)
- Spring的bean管理-注解创建对象
- Spring Bean装配之Bean的定义及作用域的注解实现
- Spring入门——Bean装配之Autowired注解
- Spring-注解实现Bean
- Spring4 Bean管理的注解实现
- Spring入门——基于Java的容器注解说明之@bean
- Spring bean管理【基于注解】
- Spring 注解方式管理Bean
- spring之bean管理-----注解
- Spring--7.bean管理(注解)
- Handler的正确使用,防止内存泄漏
- android 6.0权限全面详细分析和解决方案
- USB 3.0 / SATA 3 速度对比
- 集成Tinker后的坑 , noclassdeffounderror异常
- jsp 乱码处理
- Spring入门——Bean管理的注解实现及例子
- JS实现图片翻书效果
- 为什么做AI的都选Python?
- 剑指offer题目汇总(三)
- WebRTC详解
- Android Studio入门到精通
- eclipse
- setTimeout参数传递obj对象时,循环调用解决办法
- 100亿数据1万属性数据架构设计