@Component注解和@Scope作用域

 

初学,为了能更快更方便的记录和复习知识,在这里记录一下。

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

 

转载于:https://www.cnblogs.com/AaOoSmile/p/7808015.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值