观看慕课moocer老师视频Spring Bean装配之Bean的定义及作用域的注解实现

Bean的定义及作用域例子:

package com.imooc.beanannotation;

import org.springframework.stereotype.Component;

@Component
public class BeanAnnotation {

	public void say(String arg){
		System.out.print("BeanAnnotation:"+arg);
	}
}
package com.imooc.test.beanannotation;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.imooc.beanannotation.BeanAnnotation;
import com.imooc.ioc.interfaces.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanAnnotation extends UnitTestBase{
	
	public TestBeanAnnotation(){
		super("classpath*:spring-beanannotation.xml");
	}

	@Test
	public void testSay(){
		BeanAnnotation bean=super.getBean("beanAnnotation");
		bean.say("This is a test.");
		
	}
}
<?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.imooc.beanannotation"></context:component-scan>
        
</beans>

运行结果:

注意

注意,如果出现无法解析XM了文件, 记得导入spring-aop.jar的包,我导入的是最新版本的,因为我也一直在用5.0.8的配置,如图下载包:

自定义bean名称:

package com.imooc.beanannotation;

import org.springframework.stereotype.Component;

@Component("bean")
public class BeanAnnotation {

	public void say(String arg){
		System.out.print("BeanAnnotation:"+arg);
	}
}
package com.imooc.test.beanannotation;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.imooc.beanannotation.BeanAnnotation;
import com.imooc.ioc.interfaces.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanAnnotation extends UnitTestBase{
	
	public TestBeanAnnotation(){
		super("classpath*:spring-beanannotation.xml");
	}

	@Test
	public void testSay(){
		BeanAnnotation bean=super.getBean("bean");
		bean.say("This is a test.");
		
	}
}

运行结果:

说明自定义bean名称成功。

bean的作用域:

@Scope("prototype"):

package com.imooc.beanannotation;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

//@Component("bean")
@Scope("prototype")
@Component
public class BeanAnnotation {

	public void say(String arg){
		System.out.print("BeanAnnotation:"+arg);
	}
	
	public void myHashCode(){
		System.out.println("BeanAnnotation:"+this.hashCode());
	}
}
package com.imooc.test.beanannotation;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.imooc.beanannotation.BeanAnnotation;
import com.imooc.ioc.interfaces.UnitTestBase;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanAnnotation extends UnitTestBase{
	
	public TestBeanAnnotation(){
		super("classpath*:spring-beanannotation.xml");
	}

	@Test
	public void testSay(){
		BeanAnnotation bean=super.getBean("beanAnnotation");
		bean.say("This is a test.");
		
		bean=super.getBean("bean");
		bean.say("This is a test.");
	}
		
	
	@Test
		public void testScope(){
		BeanAnnotation bean=super.getBean("beanAnnotation");
		bean.myHashCode();
		
		bean=super.getBean("beanAnnotation");
		bean.myHashCode();
		
	}
}

<?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.imooc.beanannotation"></context:component-scan>
        
</beans>

运行结果:

使用默认值singleton:

原文链接:加载失败,请重新获取