初學,為了能更快更方便的記錄和復習知識,在這里記錄一下。
1.Component注解,這個注解有兩種用法,一種是帶有name屬性值,即Component("xxxx"),一種是直接寫這個注解@Component,這兩個的區別是:
第一種的注解在getbean的時候取的bianID是@component("xxxx")這個name屬性的值,否則報錯,第二種使用Component注解,並且不指定其屬性name的值,則bena的ID默認為類的名稱的第一個字母小寫的字符串
2.Scope注解/,作用域 默認為singleton,即為單例。
第一、新建一個項目,可以為java項目,也可以為web項目
我建的是web項目,項目結構如圖
第二步,新建包
在test包下新建一個JUnitBaseUtil的工具類,用來做單元測試類的繼承類。
package com.moocer.test; import org.junit.After; import org.junit.Before; import org.springframework.beans.BeansException; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.util.StringUtils; public class JUnitBaseUtil { private ClassPathXmlApplicationContext context; private String springXmlpath; public JUnitBaseUtil() {} public JUnitBaseUtil(String springXmlpath) { this.springXmlpath = springXmlpath; } @Before public void before() { if (StringUtils.isEmpty(springXmlpath)) { springXmlpath = "classpath*:spring-*.xml"; } try { context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+")); context.start(); } catch (BeansException e) { e.printStackTrace(); } } @After public void after() { context.destroy(); } @SuppressWarnings("unchecked") protected <T extends Object> T getBean(String beanId) { try { return (T)context.getBean(beanId); } catch (BeansException e) { e.printStackTrace(); return null; } } protected <T extends Object> T getBean(Class<T> clazz) { try { return context.getBean(clazz); } catch (BeansException e) { e.printStackTrace(); return null; } } }
第三 新建一個測試類並繼承JUnitBaseUtil類
package com.moocer.test; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.BlockJUnit4ClassRunner; import com.moocer.annotation.BeanAnnotation; import com.moocer.annotation.mulbean.CoInvoker; @RunWith(BlockJUnit4ClassRunner.class) public class AnnotationTest extends JUnitBaseUtil{ public AnnotationTest(){ super("classpath*:config/spring-beanannotation.xml"); } //@Test public void testSay(){ BeanAnnotation beanAn = super.getBean("beanAnnotation"); //Component("beanID") //BeanAnnotation beanAn = super.getBean("beanID"); beanAn.say("this is component test."); } @Test public void testHash(){ //第一次從bean中get一個hashcode BeanAnnotation beanAn = super.getBean("beanAnnotation"); beanAn.scope(); //再次從bean中get一個hashcode beanAn = super.getBean("beanAnnotation"); beanAn.scope(); } }
第四 新建一個BeanAnnotation類
package com.moocer.annotation; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component//使用Component注解,並且不指定其屬性name的值,則bena的ID默認為類的名稱的第一個字母小寫的字符串 //@Component("beanID")//使用Component注解,並且指定其name屬性的值。則bean的ID就是指定的值,不再是默認的情況。 @Scope("prototype")//作用域 默認為singleton,即為單例 public class BeanAnnotation { public void say(String arg) { System.out.println("BeanAnnotation : print = "+arg); } //測試作用域用hashcode值來區分 public void scope(){ System.out.println("BeanAnnotation : = "+ this.hashCode()); } }
第五 新建一個spring-bean.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <context:component-scan base-package="com.moocer.annotation"></context:component-scan> </beans>
第六 執行測試類的testSay()方法,這個方法是測試@Component注解的,兩種方式,一種是name屬性有值,一種是name屬性沒值。
第七 執行testHash方法,這個方法是測試@Scope注解的作用域
可以看到兩個值得hashCode值不一樣。
所用到的jar包如圖