(一)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. iOS·UIKit & Foundation框架—Annotations & Category注解工具类

  2. 作死上CODEVS,青铜题

    题面:输入一列整数,输出它们的总和.最大值.最小值.并从大到小输出. 先上一波伪代码,认真地做一波数组排序题. #include<stdio.h> #include<math.h&g ...

  3. Python库的安装方法

    Python库的安装方法 Python的解释器CPython是开源的,我们可以下载查看其源代码,同时,Python语言的各种库也都是开源的.利用Python语言编程,可用的库有很多,在Python官方 ...

  4. js修改样式表规则

    <div>adasfsfs</div> <div id="div">adasfsfs</div> <div>adasfs ...

  5. Thinkphp与CI的区别

    深入学习一门新技术的最好方法就是看官方文档. ThinkPHP5.0文档: http://www.kancloud.cn/manual/thinkphp5/118003 官方的说辞是: 主要特性 : ...

  6. js原型链部分详细使用说明案例

    1. 'index.html'文件 ```html <!DOCTYPE html> <html lang="en"> <head> <me ...

  7. 随笔-关于公网IP无法访问服务器的解决办法

    笔者的环境: windows server 2008 r2 .IIS,php,MySql. 理论上来讲,服务器,其实就是一个大型计算机,我们通过访问服务器的某个端口请求某个资源. 正常情况下,如果没有 ...

  8. java反射 顺序输出类中的方法

    java反射可以获取一个类中的所有方法,但是这些方法的输出顺序,并非代码的编写顺序. 我们可以通过自定义一个注解来实现顺序输出类中的方法. 首先,先写一个类,定义增删改查4个方法 public cla ...

  9. 基于Babylonjs自制WebGL3D模型编辑器

    一.总述 当代WebGL编程所使用的3D模型大多是从3DsMax模型或Blender模型转化而来,这种工作模式比较适合3D设计师和3D程序员分工配合的场景.但对于单兵作战的WebGL爱好者来讲这种模式 ...

  10. html学习笔记 - 特殊字符