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");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章