Spring入门篇 学习笔记

Classpath 扫描与组件管理

从 Spring 3.0 开始,Spring JavaConfig 项目提供了很多特性,包括使用 java 而不是 XML 定义 bean,比如 @Configuration, @Bean, @Import, @DependsOn

@Component 是一个通用注解,可用于任何 bean;@Repository, @Service, @Controller 是更具有针对性的注解:

  • @Repository 通常用于注解 DAO 类,即持久层
  • @Service 通常用于注解 Service 类,即服务层
  • @Controller 通常用于 Controller 类,即控制层 (MVC)

元注解 (Meta-annotations)

许多 Spring 提供的注解可以作为自己的代码,即“元数据注解“,元注解是一个简单的注解,可以应用到另一个注解(即注解的注解)

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component // 定义 @Service 注解时继承了 @Component 的所有特性
public @interface Service{
// ...
}

除了 value(), 元注解还可以有其他的属性,允许定制:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Scope("session")
public @interface SessionScope{
ScopedProxyMode proxyMode() default ScopedProxyMode.DEFAULT
}

类的自动检测与 Bean 的注册

Spring 可以自动检测类并注册 Bean 到 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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.example"/> </beans>

context:component-scan 包含 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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.example">
<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):

@Service("myMovieLister")
public class SimpleMovieLister{
// ...
} @Repository // 没有设置 name 将使用默认 name(类名首字母小写)
public class MovieFinderImpl implements MovieFinder{
// ...
}

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

<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.example"
name-generator="org.example.MyNameGenerrator"/> </beans>

作用域

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

@Scope("prototype")
@Repository
public class MovieFinderImpl implements MovieFinder{
// ...
}

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

<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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.example"
scope-resolver="org.example.MyScopeResolver"/> </beans>

代理方式

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

<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="org.example"
scoped-proxy="interfaces"/> </beans>

示例

新建类:

@Component
// @Component("bean")
@Scope
// @Scope("prototype")
public class BeanAnnotation { public void say(String arg) {
System.out.println("BeanAnnotation : " + arg);
} public void myHashCode() {
System.out.println("BeanAnnotation : " + this.hashCode());
} }

添加配置文件:

<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" > <context:component-scan base-package="com.karonda.beanannotation"></context:component-scan> </beans>

添加测试类:

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanAnnotation extends UnitTestBase { public TestBeanAnnotation() {
super("classpath*:spring-beanannotation.xml");
} @Test
public void testSay() {
// BeanAnnotation bean = super.getBean("bean");
BeanAnnotation bean = super.getBean("beanAnnotation");
bean.say("This is test.");
} @Test
public void testScpoe() {
BeanAnnotation bean = super.getBean("beanAnnotation");
bean.myHashCode(); bean = super.getBean("beanAnnotation");
bean.myHashCode();
} }

源码:learning-spring

学习 Spring (八) 注解之 Bean 的定义及作用域的更多相关文章

  1. 学习 Spring (十) 注解之 @Bean, @ImportResource, @Value

    Spring入门篇 学习笔记 @Bean @Bean 标识一个用于配置和初始化一个由 Spring IoC 容器管理的新对象的方法,类似于 XML 配置文件的 可以在 Spring 的 @Config ...

  2. (转)Spring注解完成Bean的定义

    使用Spring注解完成Bean的定义 2010-04-21 16:48:54|  分类: spring|举报|字号 订阅     下载LOFTER客户端     通过@Autowired或@Reso ...

  3. spring 通过注解装配Bean

    使用注解的方式可以减少XML的配置,注解功能更为强大,它既能实现XML的功能,也提供了自动装配的功能,采用了自动装配后,程序员所需要做的决断就少了,更加有利于对程序的开发,这就是“约定优于配置”的开发 ...

  4. Spring Bean的定义及作用域

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

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

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

  6. mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类

    相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...

  7. 学习 Spring (九) 注解之 @Required, @Autowired, @Qualifier

    Spring入门篇 学习笔记 @Required @Required 注解适用于 bean 属性的 setter 方法 这个注解仅仅表示,受影响的 bean 属性必须在配置时被填充,通过在 bean ...

  8. Spring通过注解装配Bean

    通过注解实现ServiceImpl业务 一.使用@Component装配Bean 1. 定义类:User 在类上面加@Component注解,在属性上面加@Value值 package com.wbg ...

  9. spring通过注解注册bean的方式+spring生命周期

    spring容器通过注解注册bean的方式 @ComponentScan + 组件标注注解 (@Component/@Service...) @ComponentScan(value = " ...

随机推荐

  1. 机器学习三剑客之Matplotlib基本操作

    Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形  . 通过 Matplotlib,可以仅需要几行代码,便可以生成绘图,线型图, ...

  2. Android Studio 2.2新增布局——ConstraintLayout完全解析

    ,但是Button并没有紧贴到布局的最右侧,这是为什么呢?实际上,Android Studio给控件的每个方向上的约束都默认添加了一个16dp的间距,从Inspector上面也可以明显地看出来这些间距 ...

  3. 关于GitHub的Hello Word

    最近GitHub一直是最火的配置库技术之一,各个技术大牛也都纷纷入驻GitHub 我每天都打交道的DITA-OT开源项目也宣布迁入GitHub. 那么GitHub到底有什么过人之处呢?给各位先扫个盲. ...

  4. Linux每天一个命令:iostat

    iostat用于输出CPU和磁盘I/O相关的统计信息 安装Sysstat工具包 centos: yum install sysstat ubuntu: sudo apt-get install sys ...

  5. JasperReport子报表参数传递

    子报表参数传递 下图的参数名称可以自定义 再子报表新增一个同名称的参数即可

  6. ASP.NET Core 和 ASP.NET Framework 共享 Identity 身份验证

    .NET Core 已经热了好一阵子,1.1版本发布后其可用性也越来越高,开源.组件化.跨平台.性能优秀.社区活跃等等标签再加上"微软爸爸"主推和大力支持,尽管现阶段对比.net ...

  7. Go+Python双剑合璧

    目的 Python调用Go的方法,Python有很多功能强悍又使用简洁的库.而新生军Go的多核心利用率也是非常强悍的.当然这是明面上的优点.反正你有很多理由想要让Python能够调用Go的方法. 实验 ...

  8. 领域驱动设计(DDD:Domain-Driven Design) 介绍

    Eric Evans的“Domain-Driven Design领域驱动设计”简称DDD,Evans DDD是一套综合软件系统分析和设计的面向对象建模方法,本站Jdon.com是国内公开最早讨论DDD ...

  9. 常见的web攻击手段总结

    xxs攻击(跨站脚本攻击) 攻击者在网页中嵌入恶意脚本程序,当用户打开该网页时脚本程序便在浏览器上执行,盗取客户端的cookie.用户名密码.下载执行病毒木马程 序 解决: 我们可以对用户输入的数据进 ...

  10. Java面试题详解一:面向对象三大特性

    一,多态:1.面向对象四大基本特性:抽象,封装,继承,多态抽象,封装,继承是多态的基础.多态是抽象,封装,继承的表现.2.什么是多态不同类的对象对同一消息作出不同的响应叫做多态3.多态的作用简单来说: ...