Spring 专题
专题目录
您的位置:java > Spring专题 > Spring自动扫描组件
Spring自动扫描组件
作者:--    发布时间:2019-11-20
通常情况下,声明所有的bean类或组件的xml bean配置文件,这样spring容器可以检测并注册bean类或组件。 其实,spring是能够自动扫描,检测和预定义的项目包并实例化bean,不再有繁琐的bean类声明在xml文件中。
下面是一个简单的spring项目,包括客户服务和dao层。让我们来探讨手动申明组件和自动扫描组件之间的不同。

1、手动声明组件

看到在 spring 的一个正常方式来声明一个 bean。

一个正常的 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 + "]";
	}
		
}
bean配置文件(applicationcontext.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="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]

2. 自动组件扫描

现在,启用spring组件扫描功能。
使用@component注释来表示这是类是一个自动扫描组件。
package com.h3.customer.dao;

import org.springframework.stereotype.component;

@component
public class customerdao 
{
	@override
	public string tostring() {
		return "hello , this is customerdao";
	}	
}
dao层,添加@component,表明这也是一个自动扫描组件。
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]
这是 spring 中的自动扫描组件如何工作。

自定义自动扫描组件名称

默认情况下,spring 将小写部件的第一字符- 从'customerservice'到'customerservice'。可以检索该组件名称为“customerservice”。
customerservice cust = (customerservice)context.getbean("customerservice");

要创建组件的自定义名称,你可以这样自定义名称:

@service("aaa")
public class customerservice 
...
现在,可以用'aaa'这个名称进行检索。
customerservice cust = (customerservice)context.getbean("aaa");

自动组件扫描注释类型

在spring2.5中,有4种类型的组件自动扫描注释类型
  • @component – 指示自动扫描组件。
  • @repository – 表示在持久层dao组件。
  • @service – 表示在业务层服务组件。
  • @controller – 表示在表示层控制器组件。
因此,使用哪一个?其实并不那么重要。参见 @repository,@service 或 @controller 源代码。
@target({elementtype.type})
@retention(retentionpolicy.runtime)
@documented
@component
public @interface repository {

	string value() default "";

} 

你可能会发现,所有的 @repository, @service 或 @controller 被注解为 @component。因此,我们可以只使用 @component 对所有组件进行自动扫描?是的,spring会自动扫描所有组件的 @component 注解。

它工作正常,但不是一个好的做法,为便于阅读,应该始终声明@repository,@ service 或 @controller 在指定的层,使你的代码更易于阅读,如下:

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 + "]";
	}
		
}


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册