spring中bean的注解装配(8)

2016/1/16 14:28:46


1.classpath扫描与组件管理

  • 从Spring3.0开始,Spring JavaConfig项目提供了很多特性,包括使用java而不是XML定义bean,例如@Configuration,@Bean,@Import,@DependsOn
  • @Configuration是一个通用注解,可用于任何bean
  • @Repository,@Service,@Controller是更有针对性的注解
    • @Repository通常用于注解DAO类,即持久层
    • @Service通常用于注解Service类,即服务层
    • @Controller通常用于Controller类,即控制层(MVC)

2.元注解

  • 许多Spring提供的注解可以作为自己的代码,即”元数据注解”,元注解是一个简单的注解,可以应用到另一个注解
  • 除了value(),元注解还可以有其它属性,允许订制

3.类的自动检测及bean的注册

  • spring可以自动检测类并注册bean到ApplicationContext中
  • 类加@Service@Controller等

4.

  • 通过在基于XML的Spring配置如下标签
  • 仅会查找在同一个applicationContext中的bean注解
    <context:annotation-config></context:annotation-config>
  • 为了检测到这些类并注册相应的bean,需要

    <context:component-scan base-package="com.zjx"></context:component-scan>

  • 包含,通常在使用前者后,不再使用后者

  • AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor也会被包含进来

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

  • 默认情况下,类被自动发现并注册bean的条件是:使用@Component,@Repository,@Service,@Controller注解或者使用自定义注解
  • 可以通过过滤器修改上面的行为,例如下面的例子忽略所有的@Service

    <context:component-scan base-package="com.lovo">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
  • 还可以使用use-default-filters=”false”禁用自动发现注册

6.定义bean

  • 扫描过程中组件被自动检测,那么bean名称是由BeanNameGenerator生成的(@Component,@Repository,@Service,@Controller都会有个name属性用于显式设置Bean Name)
  • 可自定义bean命名策略,实现BeanNameGenerator接口,并一定要包含一个无参构造器

例如

@Service
public class OneInterfaceImpl implements IOneInterface {

    @Override
    public String hello(String words) {
        return "Word from interface\"IOneInterface\":"+words;
    }

}

得到bean如果不显式设置BeanName,那么该bean就会由BeanNameGenerator生成类名小写的名字

7.作用域Scope

  • 通常情况下自动查找的Spring组件,其scope是singleton,Spring2.5提供了一个标识scope的注解@scope
  • 也可以自定义scope策略,实现ScopeMetadataResolver接口,并提供一个无参构造器

8.代理方式

  • 可以用scoped-porxy属性指定代理,有三个值可选:no(默认),interface,tagetClass

    <context:component-scan base-package="com.zjx"scoped-proxy="interfaces"></context:component-scan>

9.一个自动扫描例子

XML: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:p="http://www.springframework.org/schema/p" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd 
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

        <context:annotation-config></context:annotation-config>
        <context:component-scan base-package="com.zjx.bean"></context:component-scan>

</beans>

bean类注解及内容

package com.zjx.bean;

import org.springframework.stereotype.Component;

@Component//这里不写name属性,默认BeanNameGenerator生成策略默认生成BeanName
public class BeanAnnotation {
    public void say(String str){
        System.out.println("BeanAnnotation say:"+str);
    }
}

测试:

package com.zjx.interfaces.test;

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

import com.zjx.bean.BeanAnnotation;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanAnnotation extends UnitTestBase{
    public TestBeanAnnotation() {
        super("classpath:spring-beanannotation.xml");
    }
    @Test
    public void test(){
        BeanAnnotation bean = super.getBean("beanAnnotation");
        bean.say("this is me");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章