當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring自动扫描组件
生活随笔
收集整理的這篇文章主要介紹了
Spring自动扫描组件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
通常情況下,聲明所有的Bean類或組件的XML?bean配置文件,這樣Spring容器可以檢測并注冊Bean類或組件。?其實,Spring是能夠自動掃描,檢測和預定義的項目包并實例化bean,不再有繁瑣的Bean類聲明在XML文件中。 下面是一個簡單的Spring項目,包括客戶服務和DAO層。讓我們來探討手動申明組件和自動掃描組件之間的不同。 1、手動聲明組件 看到在 Spring 的一個正常方式來聲明一個 bean。
一個正常的 bean.
package com.yiibai.customer.dao;public class CustomerDAO {@Overridepublic 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;}@Overridepublic 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/beanshttp://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>執行程序
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);} }輸出結果
CustomerService [customerDAO=Hello , This is CustomerDAO]2. 自動組件掃描
現在,啟用Spring組件掃描功能。 使用@Component注釋來表示這是類是一個自動掃描組件。 package com.yiibai.customer.dao;import org.springframework.stereotype.Component;@Component public class CustomerDAO {@Overridepublic String toString() {return "Hello , This is CustomerDAO";} } DAO層,添加@Component,表明這也是一個自動掃描組件。 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 {@AutowiredCustomerDAO customerDAO;@Overridepublic 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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:component-scan base-package="com.yiibai.customer" /></beans>執行它
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);} }輸出結果
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 被注解為 @Component。因此,我們可以只使用 @Component 對所有組件進行自動掃描?是的,Spring會自動掃描所有組件的 @Component 注解。
它工作正常,但不是一個好的做法,為便于閱讀,應該始終聲明@Repository,@?Service?或?@Controller?在指定的層,使你的代碼更易于閱讀,如下:DAO 層
package com.yiibai.customer.dao;import org.springframework.stereotype.Repository;@Repository public class CustomerDAO {@Overridepublic 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 {@AutowiredCustomerDAO customerDAO;@Overridepublic String toString() {return "CustomerService [customerDAO=" + customerDAO + "]";}} 下載代碼 –?http://pan.baidu.com/s/1o7hwcHW總結
以上是生活随笔為你收集整理的Spring自动扫描组件的全部內容,希望文章能夠幫你解決所遇到的問題。