今天学习spring的bean组件装载功能,个人不太喜欢xml文件一个个配置bean的方式,所以主要学习测试注解式的自动装载方式。下面将简单说明下@Component的用法,简单入门示例献给大家。

  实现主要步骤说明:

1、ApplicationContext.xml(spring上下文环境配置)文件先配置好需要自动扫描的包位置。注册完成后,在spring初始化上下文环境时,会自动扫描声明的包以及子包下面有注解(@Component,@Repository,@Service,@Controller)的类型,并缓存起来。这里需要提一下@Component是一个基础的注解,不对代码功能分类进行区分,@Repository一般用于业务逻辑层的类注解,@Service用于dao层注解,@Controller用于控制器注解。

<?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">
<!-- 定义 bean 的 id,以及对应的 java 类 -->
<context:component-scan base-package="com.spring.simm" use-default-filters="true"/>
<!--<bean id="helloWorld" class="com.spring.simm.impls.HelloWorld">
<property name="message" value="Hello World!" />
</bean>-->
</beans>

2、添加一个HelloWorld组件。需要注意的是@Component注解的组件,默认都是单例模式,即缺省的@Scope("singleton")。通过spring去getBean实例时,其实都是同一个对象。我这里将改组件设置成多例模式,即添加注解@Scope("prototype")。

@Scope的模式有:singleton、prototype、request、session、global session,其他模式各位可以查阅网上资料

public interface IHelloWorld {
void say(String name);
}
@Scope("prototype")
//@Scope("singleton")
@Component("hello")
public class HelloWorld implements IHelloWorld {
private static int no = 0;
public HelloWorld(){
no++;
System.out.println("IHelloWorld[" + no +"]运行");
}
/**
* 说话
*/
public void say(String name) {
// TODO Auto-generated method stub
System.out.println(name +" : hello world!");
}
}

3、再添加一个HelloWorldService组件,注解成@Service,单例模式(后面,我们针对单例、多例两个组件的表现进行测试)。在服务内声明了一个自动装载的HelloWorld组件(@AutoWired),通过getHelloWorld方法返回,由于服务类是单例模式,因此这个helloworld对象也是唯一的,在服务的整个生命周期中不变。getHelloWorld2方法则每次都创建一个新的HelloWorld对象。

public interface IHelloWorldService {
HelloWorld getHelloWorld();
HelloWorld getHelloWorld2();
}
/**
* Hello world 服务,默认情况下就是单例的
* @author wh-simm
*
*/
@Service("helloservice")
public class HelloWorldService implements IHelloWorldService {
private static int no = 0;
public HelloWorldService(){
no++;
System.out.println("HelloWorldService["+no+"]启动");
}
/**
* 自动装载
*/
@Autowired
private HelloWorld helloWorld;
/**
* 获取自动装载的实例
*/
public HelloWorld getHelloWorld(){
return helloWorld;
}
/**
* 新建实例
*/
public HelloWorld getHelloWorld2(){
return new HelloWorld();
}
}

4、添加测试代码

public class app {
public static void main(String[] args) {
// 获取 classpath 中的 Beans.xml 到上下文 context
//1.装载spring上下文配置文件,完成spring容器的初始化
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
//2.从spring容器中获取bean-helloworld 实例1
IHelloWorld obj1 = (IHelloWorld) context.getBean("hello");
obj1.say("simm1");
//3.从spring容器中获取bean-helloworld 实例2
IHelloWorld obj2 = (IHelloWorld) context.getBean("hello");
obj2.say("simm2");
//3.从spring容器中获取bean-HelloWorldService 实例1(单例)
IHelloWorldService service = (IHelloWorldService)context.getBean("helloservice");
service.getHelloWorld().say("simm3");
service.getHelloWorld().say("simm4");
//3.从spring容器中获取bean-HelloWorldService 实例2(单例)
IHelloWorldService service2 = (IHelloWorldService)context.getBean("helloservice");
service2.getHelloWorld2().say("simm5");
service2.getHelloWorld2().say("simm6");
// 关闭上下文,防止内存泄漏
((ClassPathXmlApplicationContext) context).close();
}
}

5、测试结果显示,从spring容器中两次获取HelloWorldService服务,确实只运行了一次,说明默认情况下Component确实是单例的,而HelloWorld组件每次获取都是创建新的对象,属于多例模式。服务中自动装载的HelloWorld组件(@AutoWired)在单例的组件中使用时,要考虑清楚对象的生命周期。

参考资料

https://www.cnblogs.com/lonecloud/p/5745902.html

http://blog.csdn.net/qiuhan/article/details/45581371

spring 组件自动装载示例(@ComponentScan,@Component,@Scope)的更多相关文章

  1. spring 组件@Scope(request,session)示例

    上回说到, spring组件的注解Scope大约有singleton.prototype.request.session.global session 这么几种常用的场景.这里需要特别说明一下,根据源 ...

  2. 【译】Spring 4 自动装配、自动检测、组件扫描示例

    前言 译文链接:http://websystique.com/spring/spring-auto-detection-autowire-component-scanning-example-with ...

  3. Spring Boot 自动配置的原理、核心注解以及利用自动配置实现了自定义 Starter 组件

    本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 Starter 组件 摘录:读书是读完这些文字还要好好用心去想想,写书也一样,做任何事也一样 图 2 第二章目录结构图 第 2 章 Spr ...

  4. Spring组件扫描<context:component-scan/>使用详解

    1.如果不想在xml文件中配置bean,我们可以给我们的类加上spring组件注解,只需再配置下spring的扫描器就可以实现bean的自动载入. <!-- 注解注入 --> <co ...

  5. 转 Spring 组件 <context:component-scan base-pakage="">用法

    1.如果不想在xml文件中配置bean,我们可以给我们的类加上spring组件注解,只需再配置下spring的扫描器就可以实现bean的自动载入. <!-- 注解注入 --> <co ...

  6. spring Bean类自动装载实现

    先贴spring的开发文档,有助于大家学习http://shouce.jb51.net/spring/beans.html#beans-factory-class 一直想研究一下spring bean ...

  7. Spring核心技术(八)——Spring自动装载的注解

    本文针对自动装载的一些注解进行描述. 基于注解的容器配置 @Required注解 @Required注解需要应用到Bean的属性的setter方法上面,如下面的例子: public class Sim ...

  8. Spring深入浅出(二)IOC的单例 ,继承,依赖,JDBC,工厂模式以及自动装载

    IOC的单例模式--Bean Spring中的bean是根据scope来决定的. scope有4种类型: 1.singleton:单例模型,表示通过Spring容器获取的该对象是唯一的.常用并且默认. ...

  9. Spring框架——IOC 自动装载

    IOC自动装载有两种形式 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

随机推荐

  1. linux 磁盘加密和tpm搭配使用1

    一.基本名称,容易混淆 1.dm-crypt是linux的2.6内核开始集成的一种磁盘加密功能.十几年来,连sche调度算法都被改了N次,但dm-crypt一直稳定在内核中,稳定性还是很好的. 2.c ...

  2. docker 安装NexusRepository Manager

    今天学习了一下docker 感觉这东西要学习好多的命令,但是自己又是喜欢这种命令,感觉linux总是高一个等级的东西,这几天学习使用docker安装各种东西,下面记录一些我安装nexus的步骤,还是不 ...

  3. Spring-AOP标签scoped-proxy

    <aop:scoped-proxy/>介绍: Spring的Bean是有scope属性的,表示bean的生存周期.scope的值有prototype.singleton.session.r ...

  4. python_缩进_格式化代码

    pycharm如何格式化代码? ctrl + alt + l pycharm如何缩进代码? tab  向右缩进4格 shift + tab 向左缩进4格

  5. PHP7.1 报错 Warning Illegal string offset

    报错如下: Warning: Illegal string offset '阿根廷' in F:\wnmp\www\test.php on line 24 Warning: Illegal strin ...

  6. 计算机改名引发的ORA

    近期上班时,由于开机时老是提示" 局域网出现计算机重名冲突",于是把计算机名字给改了,从PC2010081312zeo改为了CXBIKKKKKKK,结果第二天来的时候,用PL/SQ ...

  7. opencv学习手稿(01开篇-显示一张图片)

    使用python36 源码: #-*- coding:utf-8 -*- import cv2 from PIL import Image, ImageTk import numpy as np # ...

  8. 文件无法删除java.io.IOException: Unable to delete

    疑问:1.为什么调用file.delete()方法时,返回值为false. 2.为什么调用Guava工具jar包中的Files.move(from,to) ,报异常:java.io.IOExcepti ...

  9. linux下安装phpunit简单方法

    现在安装phpunit相当简单,只需要下载phar压缩格式的phpunit文件,给个执行权限,就可以执行了 以下是一段官方安装文档 wget https://phar.phpunit.de/phpun ...

  10. Objective-C Runtime 文档翻译

    前言   Objective-C语言尽可能多的将许多决定从编译连接推迟到运行时.无论何时,它都尽可能的动态处理事件.这就意味着OC语言不仅仅需要编译器,还需要一个运行时系统来执行编译完成的代码.对于O ...