Spring可以自動(dòng)掃描,檢測(cè),連線和實(shí)例化Java bean。
以下部分顯示xml配置與自動(dòng)掃描自動(dòng)連線之間的區(qū)別組態(tài)。
這里是一個(gè)普通的Java bean。
package com.hgci.cnmon; public class Printer { }
另一個(gè)Java bean引用上面的代碼。
package com.hgci.cnmon; public class PrinterHelper { Printer printer; public void setPrinter(Printer myPrinter) { this.printer = myPrinter; } @Override public String toString() { return "PrinterHelper [myPrinter=" + printer + "]"; } }
XML Bean配置文件Spring-Customer.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="printerHelper" class="com.hgci.cnmon.PrinterHelper"> <property name="printer" ref="myPrinter" /> </bean> <bean id="myPrinter" class="com.hgci.cnmon.Printer" /> </beans>
這里是要運(yùn)行的代碼。
package com.hgci.cnmon; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"}); PrinterHelper cust = (PrinterHelper)context.getBean("printerHelper"); System.out.println(cust); } }
要啟用Spring自動(dòng)組件掃描功能,向類(lèi)添加注釋@Component。
package com.hgci.cnmon; import org.springframework.stereotype.Component; @Component public class Printer { }
以下Java bean不僅使用@Component來(lái)指示這一點(diǎn)是自動(dòng)掃描組件,它還將屬性標(biāo)記為 Autowired
。
package com.hgci.cnmon; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class PrinterHelper { @Autowired Printer myPrinter; }
以下xml配置文件最后添加包以啟用自動(dòng)連線自動(dòng)掃描上下文。
<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.hgci.cnmon" /> </beans>
默認(rèn)情況下,Spring將小寫(xiě)組件的簡(jiǎn)單類(lèi)名的第一個(gè)字符作為bean id。
以下代碼顯示如何使用自動(dòng)生成的ID以檢索組件。
PrinterHelper cust = (PrinterHelper)context.getBean("printerHelper");
我們也可以在聲明它時(shí)命名一個(gè)組件。
@Component("myService") public class PrinterHelper { }
一旦我們?cè)贎Component中定義了名稱(chēng),我們就可以使用這個(gè)名稱(chēng)“myService"來(lái)檢索它。
PrinterHelper cust = (PrinterHelper)context.getBean("myService");
下面的代碼顯示了如何使用Spring過(guò)濾器來(lái)掃描和注冊(cè)組件通過(guò)正則表達(dá)式,甚至類(lèi)不用@Component注釋。
package com.hgci.cnmon; public class Printer { }
助手類(lèi)
package com.hgci.cnmon; import org.springframework.beans.factory.annotation.Autowired; public class PrinterHelper { @Autowired Printer myPrinter; }
以下xml配置使用Spring過(guò)濾器來(lái)包含類(lèi)。
<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.java2s" > <context:include-filter type="regex" expression="com.hgci.cnmon.*DAO.*" /> <context:include-filter type="regex" expression="com.hgci.cnmon.*Service.*" /> </context:component-scan> </beans>
以下代碼包含com.java2s.customer.dao包下的任何類(lèi)DAO在類(lèi)名中。
<context:include-filter type="regex" expression="com.hgci.cnmon.*DAO.*" />
運(yùn)行
package com.hgci.cnmon; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.java2s.customer.services.PrinterHelper; public class App { public static void main( String[] args ) { ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"}); PrinterHelper cust = (PrinterHelper)context.getBean("printerHelper"); System.out.println(cust); } }
以下代碼顯示如何排除指定的組件停止Spring從用@Service注釋的類(lèi)自動(dòng)注冊(cè)。
<context:component-scan base-package="com.hgci.cnmon" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
在類(lèi)名中排除具有DAO字母的類(lèi)。
<context:component-scan base-package="com.java2s" > <context:exclude-filter type="regex" expression="com.java2s.customer.dao.*DAO.*" /> </context:component-scan>
更多建議: