一個(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 + "]"; } }
<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]
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 + "]"; } }
將這個(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]
CustomerService cust = (CustomerService)context.getBean("customerService");
要?jiǎng)?chuàng)建組件的自定義名稱,你可以這樣自定義名稱:
@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 ""; }
你可能會(huì)發(fā)現(xiàn),所有的 @Repository, @Service 或 @Controller 被注解為 @Component。因此,我們可以只使用 @Component 對(duì)所有組件進(jìn)行自動(dòng)掃描?是的,Spring會(huì)自動(dòng)掃描所有組件的 @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 + "]"; } }