一个正常的 bean.
package com.h3.customer.dao;
public class customerdao
{
@override
public string tostring() {
return "hello , this is customerdao";
}
}
dao 层.
package com.h3.customer.services;
import com.h3.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.h3.customer.services.customerservice"> <property name="customerdao" ref="customerdao" /> </bean> <bean id="customerdao" class="com.h3.customer.dao.customerdao" /> </beans>
执行程序
package com.h3.common;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
import com.h3.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.h3.customer.dao;
import org.springframework.stereotype.component;
@component
public class customerdao
{
@override
public string tostring() {
return "hello , this is customerdao";
}
}
package com.h3.customer.services;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.component;
import com.h3.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.h3.customer" /> </beans>
执行它
package com.h3.common;
import org.springframework.context.applicationcontext;
import org.springframework.context.support.classpathxmlapplicationcontext;
import com.h3.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.h3.customer.dao;
import org.springframework.stereotype.repository;
@repository
public class customerdao
{
@override
public string tostring() {
return "hello , this is customerdao";
}
}
service 层
package com.h3.customer.services;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.service;
import com.h3.customer.dao.customerdao;
@service
public class customerservice
{
@autowired
customerdao customerdao;
@override
public string tostring() {
return "customerservice [customerdao=" + customerdao + "]";
}
}