Spring3系列7- 自動掃描組件或Bean


Spring3系列7- 自動掃描組件或Bean

 

一、      Spring Auto Scanning Components —— 自動掃描組件    

    1.      Declares Components Manually——手動配置component

    2.      Auto Components Scanning——自動掃描組件

    3.      Custom auto scan component name——自定義掃描組件名稱

    4.      Auto Components Scan Antotation Types——自動掃描組件的注釋類型

二、      Spring Filter Components In Auto Scanning —— 在自動掃描中過濾組件

    1.      Filter Component——include

    2.      Filter Component——exclude

 

 

一、      Spring Auto Scanning Components —— 自動掃描組件

通常你可以在xml配置文件中,聲明一個bean或者component,然后Spring容器會檢查和注冊你的bean或component。實際上,Spring支持自動掃描bean或component,你可以不必再在xml文件中繁瑣的聲明bean,Spring會自動掃描、檢查你指定包的bean或component。

以下列舉一個簡單的Spring Project,包含了Customer、Service、DAO層,讓我們來看一下手動配置和自動掃描的不同。

1.      Declares Components Manually——手動配置component

先看一下正常手動配置一個bean

DAO層,CustomerDAO.java如下:

package com.lei.customer.dao;
 
public class CustomerDAO 
{
    @Override
    public String toString() {
        return "Hello , This is CustomerDAO";
    }    
}

 

Service層,CustomerService.java如下:

package com.lei.customer.services;
 
import com.lei.customer.dao.CustomerDAO;
 
public class CustomerService 
{
    CustomerDAO customerDAO;
 
    public void setCustomerDAO(CustomerDAO customerDAO) {
        this.customerDAO = customerDAO;
    }
 
    @Override
    public String toString() {
        return "CustomerService [customerDAO=" + customerDAO + "]";
    }
 
}

 

 

配置文件,Spring-Customer.xml文件如下:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
    <bean id="customerService" class="com.lei.customer.services.CustomerService">
        <property name="customerDAO" ref="customerDAO" />
    </bean>
 
    <bean id="customerDAO" class="com.lei.customer.dao.CustomerDAO" />
 
</beans>

 

運行如下代碼,App.java如下:

package com.lei.common;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.lei.customer.services.CustomerService;
 
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = 
          new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
 
        CustomerService cust = (CustomerService)context.getBean("customerService");
        System.out.println(cust);
 
    }
}

 

輸出結果:CustomerService [customerDAO=Hello , This is CustomerDAO]

 

2.      Auto Components Scanning——自動掃描組件

現在,看一下怎樣運用Spring的自動掃描。

用注釋@Component來表示這個Class是一個自動掃描組件。

Customer.java如下:

package com.lei.customer.dao;
 
import org.springframework.stereotype.Component;
 
@Component
public class CustomerDAO 
{
    @Override
    public String toString() {
        return "Hello , This is CustomerDAO";
    }    
}

 

CustomerService.java如下:

package com.lei.customer.services;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import com.lei.customer.dao.CustomerDAO;
 
@Component
public class CustomerService 
{
    @Autowired
    CustomerDAO customerDAO;
 
    @Override
    public String toString() {
        return "CustomerService [customerDAO=" + customerDAO + "]";
    }
}

 

 

配置文件Spring-customer.xml如下

 

<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-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
    <context:component-scan base-package="com.lei.customer" />
 
</beans>

 

 

 

注意:

以上xml文件中,加入了“context:component-scan”標簽,這樣就將Spring的自動掃描特性引入,base-package表示你的組件的存放位置,Spring將掃描對應文件夾下的bean(用@Component注釋過的),將這些bean注冊到容器中。

       最后運行結果與手動配置的結果一致。

3.      Custom auto scan component name——自定義掃描組件名稱

上例中,默認情況下,Spring將把組件Class的第一個字母變成小寫,來作為自動掃描組件的名稱,例如將“CustomerService”轉變為“customerservice”,你可以用“customerService”這個名字調用組件,如下:

CustomerService cust = (CustomerService)context.getBean("customerService");

 

你可以像下邊這樣,創建自定義的組件名稱:

 

@Service("AAA")
public class CustomerService 
...

 

現在,可以調用自己定義的組件了,如下:

CustomerService cust = (CustomerService)context.getBean("AAA");

 

4.      Auto Components Scan Antotation Types——自動掃描組件的注釋類型

有4種注釋類型,分別是:

@Component      ——表示一個自動掃描component

@Repository              ——表示持久化層的DAO component

@Service             ——表示業務邏輯層的Service component

@Controller        ——表示表示層的Controller component

 

以上4種,在應用時,我們應該用哪一種?讓我們先看一下@Repository、@Service、@Controller的源代碼

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
 
    String value() default "";
 
}

 

你還可以看一下@Service、@Controller的源代碼,發現它們都用@Component注釋過了,所以,在項目中,我們可以將所有自動掃描組件都用@Component注釋,Spring將會掃描所有用@Component注釋過得組件。

       實際上,@Repository、@Service、@Controller三種注釋是為了加強代碼的閱讀性而創造的,你可以在不同的應用層中,用不同的注釋,就像下邊這樣。

DAO層:

package com.lei.customer.dao;
 
import org.springframework.stereotype.Repository;
 
@Repository
public class CustomerDAO 
{
    @Override
    public String toString() {
        return "Hello , This is CustomerDAO";
    }    
}

 

 

Service層:

package com.lei.customer.services;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.lei.customer.dao.CustomerDAO;
 
@Service
public class CustomerService 
{
    @Autowired
    CustomerDAO customerDAO;
 
    @Override
    public String toString() {
        return "CustomerService [customerDAO=" + customerDAO + "]";
    }
 
}

 

 

二、      Spring Filter Components In Auto Scanning —— 在自動掃描中過濾組件

1.      Filter Component——include

下例演示了用“filter”自動掃描注冊組件,這些組件只要匹配定義的“regex”的命名規則,Clasee前就不需要用@Component進行注釋。

DAO層,CustomerDAO.java如下:

package com.lei.customer.dao;
 
public class CustomerDAO 
{
    @Override
    public String toString() {
        return "Hello , This is CustomerDAO";
    }    
}

 

Service層,CustomerService.java如下:

package com.lei.customer.services;
 
import org.springframework.beans.factory.annotation.Autowired;
import com.lei.customer.dao.CustomerDAO;
 
public class CustomerService 
{
    @Autowired
    CustomerDAO customerDAO;
 
    @Override
    public String toString() {
        return "CustomerService [customerDAO=" + customerDAO + "]";
    }
 
}

 

Spring Filtering,xml配置如下:

<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-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 
    <context:component-scan base-package="com.lei" >
 
        <context:include-filter type="regex" 
                       expression="com.lei.customer.dao.*DAO.*" />
 
        <context:include-filter type="regex" 
                       expression="com.lei.customer.services.*Service.*" />
 
    </context:component-scan>
 
</beans>

 

注意:

以上xml文件中,所有文件名字,只要包含DAO和Service(*DAO.*,*Service.*)關鍵字的,都將被檢查注冊到Spring容器中。

 

運行以下代碼:

package com.lei.common;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.lei.customer.services.CustomerService;
 
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = 
        new ClassPathXmlApplicationContext(new String[] {"Spring-AutoScan.xml"});
 
        CustomerService cust = (CustomerService)context.getBean("customerService");
        System.out.println(cust);
 
    }
}

 

運行結果:CustomerService [customerDAO=Hello , This is CustomerDAO]

 

2.      Filter Component——exclude

你也可以用exclude,制定組件避免被Spring發現並被注冊到容器中。

以下配置排除用@Service注釋過的組件

<context:component-scan base-package="com.lei.customer" >
        <context:exclude-filter type="annotation" 
            expression="org.springframework.stereotype.Service" />        
</context:component-scan>

 

以下配置排除包含“DAO”關鍵字的組件

<context:component-scan base-package="com.lei" >
        <context:exclude-filter type="regex" 
            expression="com.lei.customer.dao.*DAO.*" />        
</context:component-scan>

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM