Spring学习(6)---Bean定义及作用域的例子
本文内容纲要: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定义及作用域的例子
原文链接:https://www.cnblogs.com/JsonShare/p/4624129.html
以上是 Spring学习(6)---Bean定义及作用域的例子 的全部内容, 来源链接: utcz.com/z/362370.html