Spring学习(6)---Bean定义及作用域的例子
(一)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定义及作用域的例子的更多相关文章
- spring 学习之 bean 的注入方式 property和constructor-arg的使用方式
spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...
- 品Spring:负责bean定义注册的两个“排头兵”
别看Spring现在玩的这么花,其实它的“筹码”就两个,“容器”和“bean定义”. 只有先把bean定义注册到容器里,后续的一切可能才有可能成为可能. 所以在进阶的路上如果要想走的顺畅些,彻底搞清楚 ...
- Spring 学习笔记 Bean的作用域
在配置文件中定义Bean时,用户不但可以配置Bean的属性值以及相互之间的依赖关系,还可以定义Bean的作用域.作用域将对Bean的生命周期和创建方式产生影响.在低版本的Spring中,仅有两个作用域 ...
- Spring 学习之bean的理解
前言:对于使用Spring框架的开发人员来说,我们主要做的主要有两件事情:①开发Bean;②配置Bean;而Spring帮我们做的就是根据配置文件来创建Bean实例,并调用Bean实例的方法来完成“依 ...
- Spring 学习笔记---Bean的生命周期
生命周期图解 由于Bean的生命周期经历的阶段比较多,我们将通过一个图形化的方式进行描述.下图描述了BeanFactory中Bean生命周期的完整过程: Bean 的生命周期从Spring容器着手实例 ...
- Spring(五)之Bean定义继承和依赖注入
一.Bean定义继承 bean定义可以包含许多配置信息,包括构造函数参数,属性值和特定于容器的信息,例如初始化方法,静态工厂方法名称等. 子bean定义从父定义继承配置数据.子定义可以根据需要覆盖某些 ...
- Spring学习十----------Bean的配置之Autowired注解实现
© 版权声明:本文为博主原创文章,转载请注明出处 @Required -@Required注解适用于bean属性的setter方法 -这个注解仅仅表示,受影响的bean属性必须在配置时被填充,通过在b ...
- Spring学习七----------Bean的配置之自动装配
© 版权声明:本文为博主原创文章,转载请注明出处 Bean的自动装配(Autowiring) no:不启用自动装配,此时需要手动注入.参考:Spring学习三----------注入方式 defaul ...
- 品Spring:实现bean定义时采用的“先进生产力”
前景回顾 当我们把写好的业务代码交给Spring之后,Spring都会做些什么呢? 仔细想象一下,再稍微抽象一下,Spring所做的几乎全部都是: “bean的实例化,bean的依赖装配,bean的初 ...
随机推荐
- TagHelper的一些个人学习体会(发现了微软官方文档的一个错误)
最近一直在学习.net core 上周六开始学习Taghelper的自定义,准备周日写个博客和大家分享一下学习体会,无奈周日去考科四了,回来之后就感冒了,现在还没好.可是我发现了微软官方文档的一个错误 ...
- DFB系列 之 Clear清空surface缓存
1. 函数原型解析 函数声明: DFBResult Clear ( IDirectFBSurface * thiz, u8 r, u8 g, ...
- IO流程中IO向量iovec
作者:Younger Liu,本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 未本地化版本许可协议进行许可. 为了提高从磁盘读取数据到内存的效率,引入了IO向量机制,IO向量即struct ...
- 【算法系列学习】[kuangbin带你飞]专题十二 基础DP1 G - 免费馅饼
https://vjudge.net/contest/68966#problem/G 正解一: http://www.clanfei.com/2012/04/646.html #include< ...
- 【算法系列学习】状压dp [kuangbin带你飞]专题十二 基础DP1 D - Doing Homework
https://vjudge.net/contest/68966#problem/D http://blog.csdn.net/u010489389/article/details/19218795 ...
- Linux学习之要点必备
在本节课中我将会带领大家秒懂linux命令 要点一:常见基本命令格式 命令名称+选项+参数 例如:#mkdir -r /var/ruofeng/001/ 我们通过逐级创建目录方式解释这个例子,mkdi ...
- lock invoke 死锁事例
代码如下: using System; using System.Collections.Generic; using System.Windows.Forms; using System.Threa ...
- Play初识
2015年11月21日,写下这篇<Play初识> Play是神马呢?不是Google Play,而是一个java的web框架,因为它抛弃了传统的servlet模式的做法,国内网络连接pla ...
- matlab笔记(1) 元胞结构cell2mat和num2cell
摘自于:https://zhidao.baidu.com/question/1987862234171281467.html https://www.zybang.com/question/dcb09 ...
- jQuery 一个你意想不到的代码神器!
jQuery 一个你意想不到的代码神器! jQuery 选择器.(最简单,最基本) jquery选择器的优势: (1) 简洁的写法,$()函数 (2)支持CSS1到CSS3选择器 (3)完善的处理机制 ...