Bean管理的注解实现

  • Classpath扫描与组件管理
  • 类的自动检测与注册Bean
  • <context:annotation-config/>
  • @Component,@Repository,@Service,@Controller

以下单独讲解:

  • @Required
  • @Autowired
  • @Qualifier
  • @Resource

(一) Classpath扫描与组件管理

  • 从Spring3.0开始,Spring javaConfig项目提供了很多特性,包括使用java而不是xml定义bean,比如:@Configuration,@Bean,@Import,@DependsOn
  • @Component是一个通用的注解,可用于任何的bean
  • @Repository,@Service,@Controller是更有针对性的注解

- @Repository 通常用于注解DAO类,即持久层

- @Service 通常用于注解 Service类,即服务层

- @Controller 通常用于注解Controller类,即控制层

  • @Component是所有受Spring管理组件的通用形式,Spring还提供了更加细化的注解形式:@Repository、@Service、@Controller,它们分别对应持久层Bean,服务层Bean,和控制层Bean。这些注解与@Component的语义是一样的,完全通用,为了给它们追加更多的语义。所以,推荐使用@Repository、@Service、@Controller来替代@Component。

(二) 类的自动检测及Bean的注册

Spring可以自动检测类并注册Bean到ApplicationContext中

(三) <context:annotation-config/>

  • 通常在基于XML的Spring配置如下标签(请注意包含上下文命名空间)
  • <context:annotation-config/>仅会查找在同一个applicationContext中的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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:annotation-config/>
<beans/>

(四)类的自动检测及Bean的注册(补充)

为了能够检测这些类并注册相应的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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:component-scan base-package="com.mypackage"></context:component-scan>
</beans>

该配置隐式注册了多个对注解进行解析的处理器,如:
 AutowiredAnnotationBeanPostProcessor    
 CommonAnnotationBeanPostProcessor
 PersistenceAnnotationBeanPostProcessor  
 RequiredAnnotationBeanPostProcessor
 其实,注解本身做不了任何事情,和XML一样,只起到配置的作用,主要在于背后强大的处理器,其中就包括了<context:annotation-config/>配置项里面的注解所使用的处理器,所以配置了<context:component-scan  base-package="">之后,便无需再配置<context:annotation-config>

(五)使用过滤器进行自定义扫描

  • 默认情况下,类被自动发现并注册bean的条件是:使用@Component,@Repository,@Service,@Controller注解或者使用@Component的自定义注解
  • 可以通过过滤器修改上面的行为,如:下面例子的xml配置忽略所有的@Repository注解并用“Stub”代替
<?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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <context:component-scan base-package="com.mypackage">
<!-- 包含的过滤器 -->
<context:include-filter type="regex" expression=".*Stub.*Repository"/>
<!-- 排除的过滤器 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan> </beans>
  • 还可以使用use-default-filters="false" 禁用自动发现并注册

(六)定义Bean

扫描过程中组件被自动检测,那么Bean名称事由BeanNameGenerator生成的(@Component,@Repository,@Service,@Controller都会有个name属性用于显式设置Bean Name)

@Component("exampleBean")
public class ExampleBean{
//........
}
------------------------------
@Service("exampleBean")
public class ExampleBean{
//........
}
------------------------------
//虽然并未显式声明,但会根据BeanNameGenerator自动生成,规则:以类名为基础,类名第一个字母小写
@Repository
public class ExampleBean{
//........
}

可自定义bean命名策略,实现BeanNameGenerator接口,并一定要包含一个无参构造器

<!-- 自定义的命名规则,例如: 类名全小写,全大写.....;按照自己的方式实现-->
<context:component-scan base-package="com.mypackage" name-generator="com.mypackage.MyBeanNameGenerator"> </context:component-scan>

(七)作用域

通常情况下自动查找的Spring组件,其Scope是singleton,Spring提供了一个标识scope的注解@Scope

@Scope("prototype")
@Repository
public class OneInterfaceImpl extends OneInterface {
//..........
}

也可以自定义scope策略,实现ScopeMetadataResolver接口,并提供一个无参的构造器

<context:component-scan base-package="com.mypackage" scope-resolver="com.mypackage.MyScopeMetadataResolver">
</context:component-scan>

(八)代理方式

可以使用scope-proxy属性指定代理,有三个值可选:no,interfaces,targetClass

    <context:component-scan base-package="com.mypackage" scoped-proxy="interfaces">

</context:component-scan>

Spring学习(5)---Bean的定义及作用域的注解实现的更多相关文章

  1. Spring学习九----------Bean的配置之Bean的定义及作用域的注解实现

    © 版权声明:本文为博主原创文章,转载请注明出处 Spring Bean常用注解 @Component:通常注解,可用于任何Bean @Repository:通常用于注解DAO层,即持久层 @Serv ...

  2. Bean的定义及作用域的注解实现

    1. Classpath扫描与组件管理 从Spring3.0开始,Spring JavaConfig项目提供了很多特性,包括使用java而不是XML定义bean. 比如@configuration, ...

  3. spring 学习之 bean 的注入方式 property和constructor-arg的使用方式

    spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...

  4. 学习 Spring (八) 注解之 Bean 的定义及作用域

    Spring入门篇 学习笔记 Classpath 扫描与组件管理 从 Spring 3.0 开始,Spring JavaConfig 项目提供了很多特性,包括使用 java 而不是 XML 定义 be ...

  5. Spring Bean的定义及作用域

    目录: 了解Spring的基本概念 Spring简单的示例 Bean的定义 简单地说Bean是被Spring容器管理的Java对象,Spring容器会自动完成对Bean的实例化. 那么什么是容器呢?如 ...

  6. Spring 学习笔记 Bean的作用域

    在配置文件中定义Bean时,用户不但可以配置Bean的属性值以及相互之间的依赖关系,还可以定义Bean的作用域.作用域将对Bean的生命周期和创建方式产生影响.在低版本的Spring中,仅有两个作用域 ...

  7. Spring 学习之bean的理解

    前言:对于使用Spring框架的开发人员来说,我们主要做的主要有两件事情:①开发Bean;②配置Bean;而Spring帮我们做的就是根据配置文件来创建Bean实例,并调用Bean实例的方法来完成“依 ...

  8. 理解Spring框架中Bean的5个作用域

    当通过spring容器创建一个Bean实例时,不仅可以完成Bean实例的实例化,还可以为Bean指定特定的作用域.Spring支持如下5种作用域: singleton:单例模式,在整个Spring I ...

  9. Spring-Context之四:Spring容器及bean的定义

    Spring框架的核心功能之一就是控制反转(Inversion of Control, IoC),也叫做依赖注入(dependency injection, DI).关于依赖注入的具体内容可以参见Ma ...

随机推荐

  1. glup/grunt,browserify / webpack等的比较

    gulp/grunt的比较: Gulp / Grunt 是一种工具,能够优化前端工作流程.比如自动刷新页面.combo.压缩css.js.编译less等等.简单来说,就是使用Gulp/Grunt,然后 ...

  2. JS设计模式---缓存代理

    缓存代理可以为一些开销大的运算结果提供暂时的存储,在下次运算的时候,传进来的参数跟上次是一致, 则可以直接返回前面存储的结果. 运行上面的代码我们发现,当第二次再调用proxyMult(1,2,3)的 ...

  3. GitHub 入门不完全指南(未完待续)

    我一直认为 GitHub 是一座宝藏,想让更多人的知道它.加入到这个社区中.本人能力有限,如果文中出现不对的地方,欢迎指正交流. 一.前言 大家好,我是削微寒(xuē wēi hán),一个走在进阶路 ...

  4. seajs的那些坑

    seajs是what? 先看段代码: var loder = {}; var define = loder.define = function(id,deps,factory){ loader[id] ...

  5. 推荐免费的svn空间

    可能大部分人跟我一样办公和其他环境的代码同步选择用github,但github免费用户的代码是公开的.如果想做商业项目,用免费的github账号就不合适了,这里推荐的免费svn是个不错的选择. 1.h ...

  6. js图片轮播

    html部分 <a href="" id="pta"><div id="picture" class="pt&q ...

  7. 读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10的100次幂。 输出格式:在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格。 输入样例: 1234567890987654321123456789 输出样例: yi san wu

    这是PAT中的一道练习题 刚开始的时候我想着直接定义正整数n,结果走了很大的弯路,因为题目中要求n小于10的100次幂,即最大的正整数n有100位,而C语言中整型数字最大占8个字节的存储空间,如果按无 ...

  8. 在Visual Studio中入门F#

    写在前面的话 个人由某方面的兴趣需要学习 F#,网络上有关F#的中文资料很少,微软官方有很不错的文档,但是很可惜的是绝大部分的章节都是英文的.个人是一位.NET爱好者,想自己将 F# 的官方文档翻译出 ...

  9. 使用Dotfuscator加密混淆程序以及如何脱壳反编译

    混淆演示 首先介绍如何使用Dotfuscator对.net程序加密码混淆/加壳 C#或vb.net编写的应用程序或DLL. 这里随便创建了一个C#的命令行控制台程序.程序很简单,对当前的时间进行了AE ...

  10. poj2253 Frogger Dijkstra变形

    题目链接:http://poj.org/problem?id=2253 就是求所有路径的最大边权值的最小值 处理时每次找出距离当前的已选的节点的最短距离,然后更新每个未选节点的值 代码: #inclu ...