@Component註解和@Scope做用域

 

初學,爲了能更快更方便的記錄和複習知識,在這裏記錄一下。java

1.Component註解,這個註解有兩種用法,一種是帶有name屬性值,即Component("xxxx"),一種是直接寫這個註解@Component,這兩個的區別是:web

第一種的註解在getbean的時候取的bianID是@component("xxxx")這個name屬性的值,不然報錯,第二種使用Component註解,而且不指定其屬性name的值,則bena的ID默認爲類的名稱的第一個字母小寫的字符串spring

2.Scope註解/,做用域  默認爲singleton,即爲單例。工具

第1、新建一個項目,能夠爲java項目,也能夠爲web項目單元測試

我建的是web項目,項目結構如圖測試

第二步,新建包this

在test包下新建一個JUnitBaseUtil的工具類,用來作單元測試類的繼承類。spa

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類prototype

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類 3d

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包如圖

相關文章
相關標籤/搜索