一个正常的 bean.
package com.zyiz.customer.dao; public class CustomerDAO { @Override public String toString() { return "Hello , This is CustomerDAO"; } }
DAO 层.
package com.zyiz.customer.services; import com.zyiz.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.zyiz.customer.services.CustomerService"> <property name="customerDAO" ref="customerDAO" /> </bean> <bean id="customerDAO" class="com.zyiz.customer.dao.CustomerDAO" /> </beans>
执行程序
package com.zyiz.netmon; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zyiz.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.zyiz.customer.dao; import org.springframework.stereotype.Component; @Component public class CustomerDAO { @Override public String toString() { return "Hello , This is CustomerDAO"; } }
package com.zyiz.customer.services; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.zyiz.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.zyiz.customer" /> </beans>
执行它
package com.zyiz.netmon; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zyiz.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.zyiz.customer.dao; import org.springframework.stereotype.Repository; @Repository public class CustomerDAO { @Override public String toString() { return "Hello , This is CustomerDAO"; } }
Service 层
package com.zyiz.customer.services; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.zyiz.customer.dao.CustomerDAO; @Service public class CustomerService { @Autowired CustomerDAO customerDAO; @Override public String toString() { return "CustomerService [customerDAO=" + customerDAO + "]"; } }