Spring可以自動掃描,檢測,連線和實例化Java bean。
以下部分顯示xml配置與自動掃描自動連線之間的區(qū)別組態(tài)。
這里是一個普通的Java bean。
package com.www.o2fo.common; public class Printer { }
另一個Java bean引用上面的代碼。
package com.www.o2fo.common; 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.www.o2fo.common.PrinterHelper"> <property name="printer" ref="myPrinter" /> </bean> <bean id="myPrinter" class="com.www.o2fo.common.Printer" /> </beans>
這里是要運行的代碼。
package com.www.o2fo.common; 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自動組件掃描功能,向類添加注釋@Component。
package com.www.o2fo.common; import org.springframework.stereotype.Component; @Component public class Printer { }
以下Java bean不僅使用@Component來指示這一點是自動掃描組件,它還將屬性標記為 Autowired
。
package com.www.o2fo.common; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class PrinterHelper { @Autowired Printer myPrinter; }
以下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.www.o2fo.common" /> </beans>
默認情況下,Spring將小寫組件的簡單類名的第一個字符作為bean id。
以下代碼顯示如何使用自動生成的ID以檢索組件。
PrinterHelper cust = (PrinterHelper)context.getBean("printerHelper");
我們也可以在聲明它時命名一個組件。
@Component("myService") public class PrinterHelper { }
一旦我們在@Component中定義了名稱,我們就可以使用這個名稱“myService"來檢索它。
PrinterHelper cust = (PrinterHelper)context.getBean("myService");
下面的代碼顯示了如何使用Spring過濾器來掃描和注冊組件通過正則表達式,甚至類不用@Component注釋。
package com.www.o2fo.common; public class Printer { }
助手類
package com.www.o2fo.common; import org.springframework.beans.factory.annotation.Autowired; public class PrinterHelper { @Autowired Printer myPrinter; }
以下xml配置使用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.java2s" > <context:include-filter type="regex" expression="com.www.o2fo.common.*DAO.*" /> <context:include-filter type="regex" expression="com.www.o2fo.common.*Service.*" /> </context:component-scan> </beans>
以下代碼包含com.java2s.customer.dao包下的任何類DAO在類名中。
<context:include-filter type="regex" expression="com.www.o2fo.common.*DAO.*" />
運行
package com.www.o2fo.common; 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注釋的類自動注冊。
<context:component-scan base-package="com.www.o2fo.common" > <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
在類名中排除具有DAO字母的類。
<context:component-scan base-package="com.java2s" > <context:exclude-filter type="regex" expression="com.java2s.customer.dao.*DAO.*" /> </context:component-scan>
更多建議: