一個正常的 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 + "]"; } }
<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>
執行程式
package com.tw511.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]
package com.yiibai.customer.dao; import org.springframework.stereotype.Component; @Component public class CustomerDAO { @Override public String toString() { return "Hello , This is CustomerDAO"; } }
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 + "]"; } }
將這個「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.yiibai.customer" /> </beans>
執行它
package com.tw511.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]
CustomerService cust = (CustomerService)context.getBean("customerService");
要建立元件的自定義名稱,你可以這樣自定義名稱:
@Service("AAA") public class CustomerService ...
CustomerService cust = (CustomerService)context.getBean("AAA");
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Repository { String value() default ""; }
你可能會發現,所有的 @Repository, @Service 或 @Controller 被註解為 @Component。因此,我們可以只使用 @Component 對所有元件進行自動掃描?是的,Spring會自動掃描所有元件的 @Component 註解。
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 + "]"; } }