通常情況下,聲明所有的Bean類或組件的XML bean配置檔,這樣Spring容器可以檢測並註冊Bean類或組件。 其實,Spring是能夠自動掃描,檢測和預定義的專案包並實例化bean,不再有繁瑣的Bean類聲明在XML檔中。
	
		下麵是一個簡單的Spring專案,包括客戶服務和DAO層。讓我們來探討手動申明組件和自動掃描組件之間的不同。
	
	
		
			1、手動聲明組件
		
	
	
		看到在 Spring 的一個正常方式來聲明一個 bean。
	
	一個正常的 bean.
package com.zaixian.customer.dao;
public class CustomerDAO
{
	@Override
	public String toString() {
		return "Hello , This is CustomerDAO";
	}
}
	DAO 層.
package com.zaixian.customer.services;
import com.zaixian.customer.dao.CustomerDAO;
public class CustomerService
{
	CustomerDAO customerDAO;
	public void setCustomerDAO(CustomerDAO customerDAO) {
		this.customerDAO = customerDAO;
	}
	@Override
	public String toString() {
		return "CustomerService [customerDAO=" + customerDAO + "]";
	}
}
	
		bean配置檔(applicationContext.xml),在Spring中的一個普通 bean 配置。
	
<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.zaixian.customer.services.CustomerService"> <property name="customerDAO" ref="customerDAO" /> </bean> <bean id="customerDAO" class="com.zaixian.customer.dao.CustomerDAO" /> </beans>
執行程式
package com.xuhuhu.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zaixian.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. 自動組件掃描
		現在,啟用Spring組件掃描功能。
	
	
		使用@Component注釋來表示這是類是一個自動掃描組件。
	
package com.zaixian.customer.dao;
import org.springframework.stereotype.Component;
@Component
public class CustomerDAO
{
	@Override
	public String toString() {
		return "Hello , This is CustomerDAO";
	}
}
	
		DAO層,添加@Component,表明這也是一個自動掃描組件。
	
package com.zaixian.customer.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.zaixian.customer.dao.CustomerDAO;
@Component
public class CustomerService
{
	@Autowired
	CustomerDAO customerDAO;
	@Override
	public String toString() {
		return "CustomerService [customerDAO=" + customerDAO + "]";
	}
} 
	將這個“context:component”在bean配置檔,這意味著,在 Spring 中啟用自動掃描功能。base-package 是指明存儲組件,Spring將掃描該檔夾,並找出Bean(注解為@Component)並註冊到 Spring 容器。
<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.zaixian.customer" /> </beans>
執行它
package com.xuhuhu.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zaixian.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]
		這是 Spring 中的自動掃描組件如何工作。
	
	
		
			自定義自動掃描組件名稱
		
	
	
		默認情況下,Spring 將小寫部件的第一字元- 從'CustomerService'到'CustomerService'。可以檢索該組件名稱為“CustomerService”。
	
CustomerService cust = (CustomerService)context.getBean("customerService");
	要創建組件的自定義名稱,你可以這樣自定義名稱:
@Service("AAA")
public class CustomerService
...
	
		現在,可以用'AAA'這個名稱進行檢索。
	
CustomerService cust = (CustomerService)context.getBean("AAA");
	
		
			自動組件掃描注釋類型
		
	
	
		在Spring2.5中,有4種類型的組件自動掃描注釋類型
	
	- @Component – 指示自動掃描組件。
- @Repository – 表示在持久層DAO組件。
- @Service – 表示在業務層服務組件。
- @Controller – 表示在表示層控制器組件。
		因此,使用哪一個?其實並不那麼重要。參見 @Repository,@Service 或 @Controller 源代碼。
	
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
	String value() default "";
} 
	你可能會發現,所有的 @Repository, @Service 或 @Controller 被注解為 @Component。因此,我們可以只使用 @Component 對所有組件進行自動掃描?是的,Spring會自動掃描所有組件的 @Component 注解。
		它工作正常,但不是一個好的做法,為便於閱讀,應該始終聲明@Repository,@ Service 或 @Controller 在指定的層,使你的代碼更易於閱讀,如下:
	
	DAO 層
package com.zaixian.customer.dao;
import org.springframework.stereotype.Repository;
@Repository
public class CustomerDAO
{
	@Override
	public String toString() {
		return "Hello , This is CustomerDAO";
	}
}
	Service 層
package com.zaixian.customer.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zaixian.customer.dao.CustomerDAO;
@Service
public class CustomerService
{
	@Autowired
	CustomerDAO customerDAO;
	@Override
	public String toString() {
		return "CustomerService [customerDAO=" + customerDAO + "]";
	}
}
	