(一)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[email protected]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[email protected]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[email protected]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
说明得到的是同一个对象。
时间: 2024-10-23 07:25:32