(一)Bean的定义

先定义一个BeanAnnotation

package com.mypackage;

import org.springframework.stereotype.Component;

@Component
public class BeanAnnotation { public void say(String args){
System.out.println("BeanAnnotation:"+args);
}
}

XML配置:

<?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>

测试:

package com.mypackage;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class UnitTest { @Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-beansnnotation.xml");
BeanAnnotation bean=(BeanAnnotation)context.getBean("beanAnnotation");
bean.say("测试");
}
}

测试结果:

2015-7-6 11:36:44 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1342a80d: startup date [Mon Jul 06 11:36:44 CST 2015]; root of context hierarchy
2015-7-6 11:36:44 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-beansnnotation.xml]
BeanAnnotation:测试

(二)作用域实例

指定作用域:

package com.mypackage;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; @Scope("prototype")
@Component
public class BeanAnnotation { public void say(String args){
System.out.println("BeanAnnotation:"+args);
} public void myhashcode(){
System.out.println("BeanAnnotation:"+this.hashCode());
}
}

配置XML:

<?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>

单元测试:

package com.mypackage;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class UnitTest { @Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-beansnnotation.xml");
BeanAnnotation bean=(BeanAnnotation)context.getBean("beanAnnotation");
bean.myhashcode(); BeanAnnotation bean11=(BeanAnnotation)context.getBean("beanAnnotation");
bean11.myhashcode(); }
}

结果:

2015-7-6 12:54:03 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1342a80d: startup date [Mon Jul 06 12:54:03 CST 2015]; root of context hierarchy
2015-7-6 12:54:03 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-beansnnotation.xml]
BeanAnnotation:1617829356
BeanAnnotation:1567531625

bean的hashcode不一样说明是两个不同的对象。

把上述的@Scope("prototype")注解,该成@Scope,即使用默认值

得到的结果:

2015-7-6 13:00:30 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1342a80d: startup date [Mon Jul 06 13:00:30 CST 2015]; root of context hierarchy
2015-7-6 13:00:30 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-beansnnotation.xml]
BeanAnnotation:1617829356
BeanAnnotation:1617829356

说明得到的是同一个对象。

Spring学习(6)---Bean定义及作用域的例子的更多相关文章

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

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

  2. 品Spring:负责bean定义注册的两个“排头兵”

    别看Spring现在玩的这么花,其实它的“筹码”就两个,“容器”和“bean定义”. 只有先把bean定义注册到容器里,后续的一切可能才有可能成为可能. 所以在进阶的路上如果要想走的顺畅些,彻底搞清楚 ...

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

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

  4. Spring 学习之bean的理解

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

  5. Spring 学习笔记---Bean的生命周期

    生命周期图解 由于Bean的生命周期经历的阶段比较多,我们将通过一个图形化的方式进行描述.下图描述了BeanFactory中Bean生命周期的完整过程: Bean 的生命周期从Spring容器着手实例 ...

  6. Spring(五)之Bean定义继承和依赖注入

    一.Bean定义继承 bean定义可以包含许多配置信息,包括构造函数参数,属性值和特定于容器的信息,例如初始化方法,静态工厂方法名称等. 子bean定义从父定义继承配置数据.子定义可以根据需要覆盖某些 ...

  7. Spring学习十----------Bean的配置之Autowired注解实现

    © 版权声明:本文为博主原创文章,转载请注明出处 @Required -@Required注解适用于bean属性的setter方法 -这个注解仅仅表示,受影响的bean属性必须在配置时被填充,通过在b ...

  8. Spring学习七----------Bean的配置之自动装配

    © 版权声明:本文为博主原创文章,转载请注明出处 Bean的自动装配(Autowiring) no:不启用自动装配,此时需要手动注入.参考:Spring学习三----------注入方式 defaul ...

  9. 品Spring:实现bean定义时采用的“先进生产力”

    前景回顾 当我们把写好的业务代码交给Spring之后,Spring都会做些什么呢? 仔细想象一下,再稍微抽象一下,Spring所做的几乎全部都是: “bean的实例化,bean的依赖装配,bean的初 ...

随机推荐

  1. SELECT中(非常)常用的子查询操作

    MySQL中的子查询 是在MySQL中经常使用到的一个操作,不仅仅是用在DQL语句中,在DDL语句.DML语句中也都会常用到子查询. 子查询的定义: 子查询是将一个查询语句嵌套在另一个查询语句中: 在 ...

  2. 远程SSH连接服务与基本排错

    为什么要远程连接Linux系统?? 在实际的工作场景中,虚拟机界面或物理服务器本地的窗口都是很少能够接触到的,因为服务器装完系统后,都要拉到IDC机房托管,如果是购买了云主机,更碰不到服务器本地显示器 ...

  3. PHP array_filter() 函数

    定义和用法 array_filter() 函数用回调函数过滤数组中的元素,如果自定义过滤函数返回 true,则被操作的数组的当前值就会被包含在返回的结果数组中, 并将结果组成一个新的数组.如果原数组是 ...

  4. ORA-01034: ORACLE not available ORA-27101: shared memory realm does not exist

    Oracle 设置默认数据库 如果我们的服务器上或者电脑上安装了多个数据库,当我们使用sqlplus时如果为指定数据库时登录到的是哪一个数据库呢?今天遇到了一个老问题: ORA-01034: ORAC ...

  5. 【iOS UI】UINavigationController

    1.UINavigationController介绍 1.1简介 UINavigationController可以翻译为导航控制器,在iOS里经常用到. 下面的图显示了导航控制器的流程.最左侧是根视图 ...

  6. POJ1850-Code 递推数学

    题目链接:http://poj.org/problem?id=1850 题目大意: 按照字典序对升序排列组成的字母进行编号,给出一个长度不超过10的串,求出它的编号是多少?如果无法进行编号则输出0. ...

  7. 增强for循环 -- foreach循环

    1  作用 简化迭代器的书写格式.(注意:foreach循环的底层还是使用了迭代器遍历.) 2  适用范围 如果是实现了Iterable接口的对象或者是数组对象都可以使用foreach循环. 3  格 ...

  8. redis-key相关命令

    本篇主要介绍和redis的key进行操作的相关命令. 命令 描述 复杂度 返回值 DEL key [key ...] 删除给定的一个或多个 key .不存在的 key 会被忽略.删除单个列表.集合.有 ...

  9. php对文件操作(读、写、)的基础知识(详细)

    文件位置如下图所示: 1.判断是文件还是目录 var_dump(filetype("./aa/bb/cc.txt")); 输出: string(4) "file" ...

  10. servlet的过滤器的doFilter()

    doFilter中的chain.doFilter(res,req);//Filter 只是链式处理,请求依然转发到目的地址 意思就说过滤器只是一个关口,如果符合条件的请求会被过滤器拦截下来,然后进行处 ...