通常,您在XML bean配置文件中声明所有bean或组件,以便Spring容器可以检测和注册您的bean或组件。 实际上,Spring能够从预定义的项目包中自动扫描,检测和实例化bean,而XML文件中不再有乏味的bean声明。
以下是一个简单的Spring项目,包括客户服务和dao层。 让我们探讨一下在Spring中手动声明组件和自动扫描组件之间的区别。
1.手动声明组件
查看在Spring中声明bean的正常方法。
普通豆。
package com.mkyong.customer.dao;
public class CustomerDAO
{
@Override
public String toString() {
return "Hello , This is CustomerDAO";
}
}
DAO层。
package com.mkyong.customer.services;
import com.mkyong.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配置文件(Spring-Customer.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.mkyong.customer.services.CustomerService">
<property name="customerDAO" ref="customerDAO" />
</bean>
<bean id="customerDAO" class="com.mkyong.customer.dao.CustomerDAO" />
</beans>
运行
package com.mkyong.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.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.mkyong.customer.dao;
import org.springframework.stereotype.Component;
@Component
public class CustomerDAO
{
@Override
public String toString() {
return "Hello , This is CustomerDAO";
}
}
在DAO层中,添加@Component表示这也是一个自动扫描组件。
package com.mkyong.customer.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.mkyong.customer.dao.CustomerDAO;
@Component
public class CustomerService
{
@Autowired
CustomerDAO customerDAO;
@Override
public String toString() {
return "CustomerService [customerDAO=" + customerDAO + "]";
}
}
将这个“ context:component
”放在bean配置文件中,这意味着在Spring中启用自动扫描功能。 基本包指示组件的存储位置,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.mkyong.customer" />
</beans>
运行
package com.mkyong.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.mkyong.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");
汽车零部件扫描注释类型
在Spring 2.5中,有4种类型的自动组件扫描注释类型
- @Component –指示自动扫描组件。
- @Repository –表示持久层中的DAO组件。
- @Service –表示业务层中的Service组件。
- @Controller –表示表示层中的控制器组件。
那么,该使用哪一个呢? 真的没关系。 让我们看看@Repository
, @Service
@Controller
或@Controller
的源代码。
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository {
String value() default "";
}
您会注意到,所有@Repository
, @Service
@Controller
或@Controller
都使用@Component
进行了注释。 那么,我们可以仅对所有自动扫描组件使用@Component吗? 是的,可以,Spring会自动扫描所有带注解的组件。
出于可读性的考虑,它工作正常,但不是一个好习惯,您应该始终为指定的层声明@ Repository,@ Service或@Controller,以使代码更易于阅读,如下所示:
DAO层
package com.mkyong.customer.dao;
import org.springframework.stereotype.Repository;
@Repository
public class CustomerDAO
{
@Override
public String toString() {
return "Hello , This is CustomerDAO";
}
}
服务层
package com.mkyong.customer.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mkyong.customer.dao.CustomerDAO;
@Service
public class CustomerService
{
@Autowired
CustomerDAO customerDAO;
@Override
public String toString() {
return "CustomerService [customerDAO=" + customerDAO + "]";
}
}
下载源代码
翻译自: https://mkyong.com/spring/spring-auto-scanning-components/