Spring入门——(九、Bean管理的注解实现)

Bean管理的注解实现及例子
>Classpath扫描与组件管理
》从spring3.0开始,Spring JavaConfig项目提供了很多特性,包括使用Java而不是XML定义 bean,比如@Configuration,@Bean,@Import,@DependsOn
》@Component是一个通用注解,可用于任何Bean
》@Repository,@Service,@Controller是根由针对性的注解
- @Repository通常用于注解DAO类,即持久层
- @Service通常用于注解Service类,即服务层
- @Controller通常用于Controller类,即控制层(MVC)
元注解(Meta - annotations)
>许多spring提供的注解可以作为自己的代码,即“元数据注解”,元注解是一个简单的注解,可以应用到另一个注解

>除了value(),元注解还可以有其他的属性,可以定制



>类的自动检测与注册bean

使用过滤器进行自定义扫描

官方文档:

><context:annotation-config/>

定义Bean:

作用域:

代理方式:

示例:

BeanAnnotation类:

package com.imooc.beanannotation;

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

//@Component("bean")
@Scope
@Component
public class BeanAnnotation {
	
	public void say(String arg) {
		System.out.println("BeanAnnotation : " + arg);
	}
	
	public void myHashCode() {
		System.out.println("BeanAnnotation : " + this.hashCode());
	}
	
}

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

测试类:

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.test.base.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 test.");
		
		bean = super.getBean("bean");
		bean.say("This is test.");
	}
	
	@Test
	public void testScpoe() {
		BeanAnnotation bean = super.getBean("beanAnnotation");
		bean.myHashCode();
		
		bean = super.getBean("beanAnnotation");
		bean.myHashCode();
	}
	
}

测试结果:

七月 10, 2018 10:51:53 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@5d22bbb7: startup date [Tue Jul 10 22:51:53 GMT+08:00 2018]; root of context hierarchy
JsrServie destroy.
七月 10, 2018 10:51:53 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4b5d6a01: startup date [Tue Jul 10 22:51:53 GMT+08:00 2018]; root of context hierarchy
七月 10, 2018 10:51:53 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from URL [file:/C:/Users/ghost/Desktop/545040d70001107900000000/Spring/classes/spring-beanannotation.xml]
七月 10, 2018 10:51:53 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [config.xml]
七月 10, 2018 10:51:53 下午 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
信息: Loading properties file from class path resource [config.properties]
七月 10, 2018 10:51:53 下午 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
信息: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
JsrServie init.
BeanAnnotation : 7967307
BeanAnnotation : 7967307
七月 10, 2018 10:51:53 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4b5d6a01: startup date [Tue Jul 10 22:51:53 GMT+08:00 2018]; root of context hierarchy
JsrServie destroy.


猜你喜欢

转载自blog.csdn.net/ghostxbh/article/details/80993307