鍍金池/ 教程/ Java/ Spring自動(dòng)掃描組件
Spring EL hello world實(shí)例
Spring使用@Autowired注解自動(dòng)裝配
MapFactoryBean實(shí)例
Bean InitializingBean和DisposableBean實(shí)例
Spring使用@Required注解依賴檢查
安裝Spring工具套件到Eclipse
CGLIB is required to process @Configuration classes
Spring EL Arrays, Lists, Maps實(shí)例
Spring由類(lèi)型(Type)自動(dòng)裝配
Spring在bean配置文件中定義電子郵件模板
Spring發(fā)送帶附件郵件
ListFactoryBean實(shí)例
Struts 2 + Spring + Hibernate集成實(shí)例
Spring bean引用實(shí)例
Spring JdbcTemplate batchUpdate()實(shí)例
Spring SetFactoryBean實(shí)例
Spring通過(guò)setter方法注入
Spring EL hello world實(shí)例
在bean配置文件中的電子郵件模板
Spring發(fā)送帶附件的Email
Spring EL 方法調(diào)用實(shí)例
Spring自動(dòng)裝配Beans
Spring由構(gòu)造方法自動(dòng)裝配
Spring使用@Required注解依賴檢查
Spring SimpleJdbcTemplate查詢示例
自定義@Required-style注解
Spring+JDBC實(shí)例
Spring EL三元運(yùn)算(if-then-else)實(shí)例
JdbcTemplate+JdbcDaoSupport實(shí)例
Spring自動(dòng)掃描組件
Spring EL方法調(diào)用實(shí)例
Spring自動(dòng)裝配Beans
Spring EL bean引用實(shí)例
Spring依賴注入(DI)
Spring EL Lists,Maps實(shí)例
Spring通過(guò)構(gòu)造方法依賴注入
通過(guò)MailSender發(fā)送電子郵件
Spring AOP實(shí)例(Pointcut, Advisor)
Spring EL運(yùn)算符實(shí)例
Spring JavaConfig實(shí)例
Spring依賴注入 (DI)
Spring EL bean引用實(shí)例
Spring由AutoDetect自動(dòng)裝配
Spring由構(gòu)造方法自動(dòng)裝配
Spring JdbcTemplate查詢實(shí)例
Spring SimpleJdbcTemplate batchUpdate()實(shí)例
Spring+JDBC實(shí)例
如何注入值到Spring bean屬性
Spring SimpleJdbcTemplate類(lèi)命名參數(shù)實(shí)例
Spring PropertyPlaceholderConfigurer實(shí)例
Spring JdbcTemplate batchUpdate() 實(shí)例
Spring EL與ExpressionParser測(cè)試
Spring內(nèi)部Bean實(shí)例
加載多個(gè)Spring bean配置文件
Spring+Hibernate+MySql實(shí)例(注解)
Spring AOP+AspectJ 在XML配置實(shí)例
Spring自動(dòng)代理創(chuàng)建者實(shí)例
Spring EL正則表達(dá)式實(shí)例
測(cè)試 Spring EL與ExpressionParser
java.lang.ClassNotFoundException: org.exolab.castor.xml.XMLExcep
Spring PropertyPlaceholderConfigurer實(shí)例
Spring bean配置繼承
Spring使用Setter依賴注入
Spring自動(dòng)裝配@Qualifier實(shí)例
Spring依賴檢查
Spring hello world實(shí)例
Spring依賴注入servlet會(huì)話監(jiān)聽(tīng)器
Spring自定義@Required-style注解
Spring bean作用域
Spring AOP攔截器的序列
注入值到Spring Bean的屬性
Spring JdbcTemplate+JdbcDaoSupport實(shí)例
Spring教程
Spring自動(dòng)裝配@Qualifier實(shí)例
Spring MapFactoryBean例子
Spring Bean作用域?qū)嵗?/span>
Cannot proxy target class because CGLIB2 is not available
Spring AOP攔截器的順序
Spring集合(List, Set, Map, and Properties)實(shí)例
Spring SimpleJdbcTemplate類(lèi)命名參數(shù)實(shí)例
Spring通過(guò)自動(dòng)檢測(cè)自動(dòng)裝配
Spring+Hibernate+MySql實(shí)例
Spring JavaConfig @Import實(shí)例
訪問(wèn)MessageSource的bean(MessageSourceAware)
Spring JdbcTemplate查詢實(shí)例
Spring JavaConfig @Import實(shí)例
Spring Object/XML映射實(shí)例
Spring Bean引用例子
Spring AOP+AspectJ注解實(shí)例
java.lang.ClassNotFoundException: org.apache.xml.serialize.XMLSe
Spring依賴檢查
Spring JavaConfig實(shí)例
Spring AOP+AspectJ注解實(shí)例
Spring bean配置繼承
Spring自動(dòng)掃描組件
自動(dòng)代理創(chuàng)建者實(shí)例
Spring Bean init-method 和 destroy-method實(shí)例
Spring通過(guò)構(gòu)造方法注入
Spring過(guò)濾器組件自動(dòng)掃描
Spring構(gòu)造方法注入類(lèi)型歧義
Spring+JDBC實(shí)例
Spring EL正則表達(dá)式實(shí)例
Spring通過(guò)Gmail SMTP服務(wù)器MailSender發(fā)送電子郵件
Spring AOP+AspectJ在XML配置實(shí)例
SetFactoryBean實(shí)例
Spring注入日期到bean屬性-CustomDateEditor
Spring AOP通知實(shí)例 – Advice
Spring使用@Autowired注解自動(dòng)裝配
Spring AOP實(shí)例(Pointcut,Advisor)
Spring AOP在Hibernate的事務(wù)管理
Spring初學(xué)快速入門(mén)
Spring內(nèi)部bean實(shí)例
Spring bean加載多個(gè)配置文件
Spring + Hibernate+ MySql注解實(shí)例
Spring @PostConstruct和@PreDestroy實(shí)例
Spring依賴注入servlet會(huì)話監(jiān)聽(tīng)器
Spring ListFactoryBean實(shí)例
Spring松耦合的實(shí)例
Spring過(guò)濾器組件自動(dòng)掃描
Spring按名稱(Name)自動(dòng)裝配
Spring AOP通知實(shí)例(Advice)
Spring AOP在Hibernate事務(wù)管理
Bean init-method和destroy-method實(shí)例
Spring EL操作符實(shí)例
Spring注入日期到bean屬性(CustomDateEditor)
資源加載程序的getResource()示例
Bean @PostConstruct和@PreDestroy實(shí)例
Struts2+Spring集成實(shí)例
Spring集合 (List,Set,Map,Properties) 實(shí)例
Spring按類(lèi)型(Type)自動(dòng)裝配
Spring由名稱(Name)自動(dòng)裝配
Spring SimpleJdbcTemplate batchUpdate()實(shí)例
Spring hello world實(shí)例
Spring Bean InitializingBean和DisposableBean實(shí)例
Spring構(gòu)造方法注入類(lèi)型歧義
Spring EL三元操作符(if-then-else)實(shí)例
Spring+Hibernate+MySql實(shí)例
Spring松耦合實(shí)例

Spring自動(dòng)掃描組件

通常情況下,聲明所有的Bean類(lèi)或組件的XML bean配置文件,這樣Spring容器可以檢測(cè)并注冊(cè)Bean類(lèi)或組件。 其實(shí),Spring是能夠自動(dòng)掃描,檢測(cè)和預(yù)定義的項(xiàng)目包并實(shí)例化bean,不再有繁瑣的Bean類(lèi)聲明在XML文件中。
下面是一個(gè)簡(jiǎn)單的Spring項(xiàng)目,包括客戶服務(wù)和DAO層。讓我們來(lái)探討手動(dòng)申明組件和自動(dòng)掃描組件之間的不同。

1、手動(dòng)聲明組件

看到在 Spring 的一個(gè)正常方式來(lái)聲明一個(gè) bean。

一個(gè)正常的 bean.

package com.yiibai.customer.dao;

public class CustomerDAO 
{
	@Override
	public String toString() {
		return "Hello , This is CustomerDAO";
	}	
}

DAO 層.

package com.yiibai.customer.services;

import com.yiibai.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中的一個(gè)普通 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.yiibai.customer.services.CustomerService">
		<property name="customerDAO" ref="customerDAO" />
	</bean>

	<bean id="customerDAO" class="com.yiibai.customer.dao.CustomerDAO" />

</beans>

執(zhí)行程序

package com.yiibai.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yiibai.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);
    	
    }
}

輸出結(jié)果

CustomerService [customerDAO=Hello , This is CustomerDAO]

2. 自動(dòng)組件掃描

現(xiàn)在,啟用Spring組件掃描功能。
使用@Component注釋來(lái)表示這是類(lèi)是一個(gè)自動(dòng)掃描組件。
package com.yiibai.customer.dao;

import org.springframework.stereotype.Component;

@Component
public class CustomerDAO 
{
	@Override
	public String toString() {
		return "Hello , This is CustomerDAO";
	}	
}
DAO層,添加@Component,表明這也是一個(gè)自動(dòng)掃描組件。
package com.yiibai.customer.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.yiibai.customer.dao.CustomerDAO;

@Component
public class CustomerService 
{
	@Autowired
	CustomerDAO customerDAO;

	@Override
	public String toString() {
		return "CustomerService [customerDAO=" + customerDAO + "]";
	}
} 

將這個(gè)“context:component”在bean配置文件,這意味著,在 Spring 中啟用自動(dòng)掃描功能。base-package 是指明存儲(chǔ)組件,Spring將掃描該文件夾,并找出Bean(注解為@Component)并注冊(cè)到 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.yiibai.customer" />

</beans>

執(zhí)行它

package com.yiibai.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yiibai.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);
    	
    }
}

輸出結(jié)果

CustomerService [customerDAO=Hello , This is CustomerDAO]
這是 Spring 中的自動(dòng)掃描組件如何工作。

自定義自動(dòng)掃描組件名稱

默認(rèn)情況下,Spring 將小寫(xiě)部件的第一字符- 從'CustomerService'到'CustomerService'。可以檢索該組件名稱為“CustomerService”。
CustomerService cust = (CustomerService)context.getBean("customerService");

要?jiǎng)?chuàng)建組件的自定義名稱,你可以這樣自定義名稱:

@Service("AAA")
public class CustomerService 
...
現(xiàn)在,可以用'AAA'這個(gè)名稱進(jìn)行檢索。
CustomerService cust = (CustomerService)context.getBean("AAA");

自動(dòng)組件掃描注釋類(lèi)型

在Spring2.5中,有4種類(lèi)型的組件自動(dòng)掃描注釋類(lèi)型
  • @Component – 指示自動(dòng)掃描組件。
  • @Repository – 表示在持久層DAO組件。
  • @Service – 表示在業(yè)務(wù)層服務(wù)組件。
  • @Controller – 表示在表示層控制器組件。
因此,使用哪一個(gè)?其實(shí)并不那么重要。參見(jiàn) @Repository,@Service 或 @Controller 源代碼。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {

	String value() default "";

} 

你可能會(huì)發(fā)現(xiàn),所有的 @Repository, @Service 或 @Controller 被注解為 @Component。因此,我們可以只使用 @Component 對(duì)所有組件進(jìn)行自動(dòng)掃描?是的,Spring會(huì)自動(dòng)掃描所有組件的 @Component 注解。

它工作正常,但不是一個(gè)好的做法,為便于閱讀,應(yīng)該始終聲明@Repository,@ Service 或 @Controller 在指定的層,使你的代碼更易于閱讀,如下:

DAO 層

package com.yiibai.customer.dao;

import org.springframework.stereotype.Repository;

@Repository
public class CustomerDAO 
{
	@Override
	public String toString() {
		return "Hello , This is CustomerDAO";
	}	
}

Service 層

package com.yiibai.customer.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yiibai.customer.dao.CustomerDAO;

@Service
public class CustomerService 
{
	@Autowired
	CustomerDAO customerDAO;

	@Override
	public String toString() {
		return "CustomerService [customerDAO=" + customerDAO + "]";
	}
		
}