【译】Spring 4 自动装配、自动检测、组件扫描示例

时间:2022-09-15 10:27:48

前言

译文链接:http://websystique.com/spring/spring-auto-detection-autowire-component-scanning-example-with-annotations/

在本篇文章我们会看到Spring是如何通过component-scanning配置,在没有使用@Bean和@Configuration声明bean,也没有使用XML配置声明bean的情况下,自动检测到程序中配置的bean,并且自动装配这些bean。

对于component-scanning的配置,本文将使用@ComponentScan注解,当然,我们也会提供一份对应的XML配置来作为比较。

我们将创建一个典型的企业级应用示例,涉及不同的层(Service、DAO)。

涉及的技术及开发工具

  • Spring 4.0.6.RELEASE
  • Joda-time 2.3
  • Maven 3
  • JDK 1.6
  • Eclipse JUNO Service Release 2

工程结构目录

如下是本工程的目录结构

【译】Spring 4 自动装配、自动检测、组件扫描示例

接下来开始往上面添加具体内容。

步骤一:往pom.xml添加Spring依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.websystique.spring</groupId>
<artifactId>Spring4AutoScanning</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging> <name>Spring4AutoScanning</name>
<properties>
<springframework.version>4.0.6.RELEASE</springframework.version>
<joda-time.version>2.3</joda-time.version>
</properties> <dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${springframework.version}</version>
</dependency> <!-- Joda-Time -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency> </dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build> </project>

这个示例,我们使用了Spring-core和Spring-context依赖,另外,还使用了JodaTime的LocalDate类来做一些日期计算,所以引入了joda-time依赖。

步骤二:创建Spring配置类

Spring配置类是用@Configuration注解标注的,这些类包含了用@Bean注解标注的方法,这些方法生成bean会交给Spring容器来管理。

package com.websystique.spring.configuration;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan(basePackages = "com.websystique.spring")
public class AppConfig { }

你可能注意到上面的类是空的,没有使用@Bean标注的方法,那么bean从哪里产生呢?

事实上,我们使用了@ComponentScan注解,来帮助我们自动检测bean

@ComponentScan(basePackages = "com.websystique.spring")

@ComponentScan注解的basePackages属性是一个包名,配置好后,将会在该包下查找所有使用特定注解标注的类,作为bean。

如下是一些常见的注解,被这些注解标注的类是一个bean,将会被自动检测

@Repository - 作为持久层的DAO组件.
@Service - 作为业务层的Service组件.
@Controller - 作为展现层的Controller组件.
@Configuration - Configuration组件.
@Component - 通用注解, 可以作为以上注解的替代.

注意上面的注解内部都是用@Component标注的,所以实际上你可以在任何地方使用@Component, 但是为了表达更加清晰的设计意图,强烈建议根据不同情况使用不同的注解。

注意:在我们这里例子,你甚至可以直接删除配置类因为它并没有包含任何@Bean注解标注的方法,在后面的main方法里我们将会看到在这种情况下是如何扫描这些Bean。

另外,看下使用XML配置的情况,结果如下(命名为app-config.xml)

<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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="com.websystique.spring" /> </beans>

步骤三:创建DAO层的类

package com.websystique.spring.dao;

import com.websystique.spring.model.Employee;

public interface EmployeeDao {

    void saveInDatabase(Employee employee);
}
package com.websystique.spring.dao;

import org.springframework.stereotype.Repository;

import com.websystique.spring.model.Employee;

@Repository("employeeDao")
public class EmployeeDaoImpl implements EmployeeDao{ public void saveInDatabase(Employee employee) { /*
* Logic to save in DB goes here
*/
System.out.println("Employee "+employee.getName()+" is registered for assessment on "+ employee.getAssessmentDate()); } }

@Repository注解标注该类作为一个持久层自动检测的bean,参数employeeDao为bean提供了一个名字,我们将会在主服务Bean里注入该bean。

步骤四:创建Service层类

package com.websystique.spring.service;

import org.joda.time.LocalDate;

public interface DateService {

    LocalDate getNextAssessmentDate();
}
package com.websystique.spring.service;

import org.joda.time.LocalDate;
import org.springframework.stereotype.Service; @Service("dateService")
public class DateServiceImpl implements DateService{ public LocalDate getNextAssessmentDate() {
return new LocalDate(2015,10,10);
} }

@Service注解标注这个类为业务层自动检测的bean,后续我们会将其注入到主服务bean中。

package com.websystique.spring.service;

import com.websystique.spring.model.Employee;

public interface EmployeeService {

    void registerEmployee(Employee employee);
}
package com.websystique.spring.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.websystique.spring.dao.EmployeeDao;
import com.websystique.spring.model.Employee; @Service("employeeService")
public class EmployeeServiceImpl implements EmployeeService{ @Autowired
private DateService dateService; @Autowired
private EmployeeDao employeeDao; public void registerEmployee(Employee employee) {
employee.setAssessmentDate(dateService.getNextAssessmentDate());
employeeDao.saveInDatabase(employee);
} }

EmployeeService是我们的主服务类,可以看到,我们往这个类注入了DateService和EmployeeDao。被@Autowired注解标注的dateService属性,会被Spring的依赖注入自动装配合适的Bean,由于我们已经使用@Service声明了一个DateService Bean,所以该Bean将会被注入到这里。类似的,被@Repository标注的EmployeeDao也会被注入到employeeDao属性中。

如下是我们的实体类Employee

package com.websystique.spring.model;

import org.joda.time.LocalDate;

public class Employee {

    private int id;

    private String name;

    private LocalDate assessmentDate;

    public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public LocalDate getAssessmentDate() {
return assessmentDate;
} public void setAssessmentDate(LocalDate assessmentDate) {
this.assessmentDate = assessmentDate;
} @Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", assessmentDate="
+ assessmentDate + "]";
} }

步骤五:创建main方法运行该程序

package com.websystique.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext; import com.websystique.spring.configuration.AppConfig;
import com.websystique.spring.model.Employee;
import com.websystique.spring.service.EmployeeService; public class AppMain { public static void main(String args[]){
AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); EmployeeService service = (EmployeeService) context.getBean("employeeService"); /*
* Register employee using service
*/
Employee employee = new Employee();
employee.setName("Danny Theys");
service.registerEmployee(employee); context.close();
}
}

运行上面的程序,会看到如下结果:

Employee Danny Theys is registered for assessment on 2016-12-22

另外,假如你想不使用配置类AppConfig,那么还可以这样做:

package com.websystique.spring;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.websystique.spring.model.Employee;
import com.websystique.spring.service.EmployeeService; public class AppMain { public static void main(String args[]){
//AbstractApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.websystique.spring");
context.refresh(); EmployeeService service = (EmployeeService) context.getBean("employeeService"); /*
* Register employee using service
*/
Employee employee = new Employee();
employee.setName("Danny Theys");
service.registerEmployee(employee); context.close();
}
}

AnnotationConfigApplicationContext.scan方法会扫描指定包下的所有类,注册所有被@Component标注的bean(实际上@configuration本身内部也是使用@component注解)到应用的上下文环境中;

另外要注意,在完成扫描操作后,refresh方法必须被调用,能保证完整的处理这些注册类。

运行以上程序,你会看到同样的输出。

最后,如果使用XML配置的话,在main方法里替换

AbstractApplicationContext  context = new AnnotationConfigApplicationContext(AppConfig.class);

AbstractApplicationContext context = new ClassPathXmlApplicationContext("app-config.xml");

会看到同样的输出。

本例源码

http://websystique.com/?smd_process_download=1&download_id=793

【译】Spring 4 自动装配、自动检测、组件扫描示例的更多相关文章

  1. Spring装配Bean之组件扫描和自动装配

    Spring从两个角度来实现自动化装配: 组件扫描:Spring会自动发现应用上下文中所创建的bean. 自动装配:Spring自动满足bean之间的依赖. 案例:音响系统的组件.首先为CD创建Com ...

  2. Spring随笔-bean装配-自动装配

    Spring提供了三种装配方式 1.XML文件进行显式装配 2.java中进行显示装配 3.自动化装配 1.自动化装配的两种实现方式 1.组件扫描:Spring会自动发现应用上下文中创建的bean 2 ...

  3. 8 -- 深入使用Spring -- 7&period;&period;&period;4 使用自动装配

    8.7.4 使用自动装配 在自动装配策略下,Action还是由Spring插件创建,Spring 插件在创建Action实例时,利用Spring的自动装配策略,将对应的业务逻辑组件注入Action实例 ...

  4. Spring 自动装配及其注解

    一.属性自动装配 首先,准备三个类,分别是User,Cat,Dog.其中User属性拥有Cat和Dog对象. package com.hdu.autowire; public class User { ...

  5. SpringBoot核心特性之组件自动装配

    写在前面 spring boot能够根据依赖的jar包自动配置spring boot的应用,例如: 如果类路径中存在DispatcherServlet类,就会自动配置springMvc相关的Bean. ...

  6. 【Spring】Spring中的Bean - 5、Bean的装配方式&lpar;XML、注解(Annotation)、自动装配&rpar;

    Bean的装配方式 简单记录-Java EE企业级应用开发教程(Spring+Spring MVC+MyBatis)-Spring中的Bean 文章目录 Bean的装配方式 基于XML的装配 基于注解 ...

  7. 【spring 注解驱动开发】spring自动装配

    尚学堂spring 注解驱动开发学习笔记之 - 自动装配 自动装配 1.自动装配-@Autowired&@Qualifier&@Primary 2.自动装配-@Resource&amp ...

  8. Spring自动装配与扫描注解

    1 javabean的自动装配 自动注入,减少xml文件的配置信息. <?xml version="1.0" encoding="UTF-8"?> ...

  9. spring实战二之Bean的自动装配&lpar;非注解方式&rpar;

    Bean的自动装配 自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bea ...

随机推荐

  1. 第六篇T语言实例开发,多点找色应用

    ---恢复内容开始--- 多点找色应用 文字,图形特征的获取 多点找色 功能原型 窗口多点找色(窗口句柄,x1,y1,x2,y2,颜色值,色点组,相似度,方向,返回x,返回y) 功能说明 根据指定的多 ...

  2. READONLY、、cursor、、VARYING

    针对 Transact-SQL 过程的准则:所有 Transact-SQL 数据类型都可以用作参数.您可以使用用户定义的表类型创建表值参数.表值参数只能是 INPUT 参数,并且这些参数必须带有 RE ...

  3. Android 模拟器genymotion安装&comma;eclipse 插件

    genymotion是一款号称速度最快性能最好的android模拟器,它基于Oracle VM VirtualBox.支持GPS.重力感应.光.温度等诸多传感器:支持OpenGL 3D加速:电池电量模 ...

  4. C&num;解leetcode 16&period; 3Sum Closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  5. locate linux文件查找命令

    locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 di ...

  6. php数组排序

    sort() - 以升序对数组排序rsort() - 以降序对数组排序asort() - 根据值,以升序对关联数组进行排序ksort() - 根据键,以升序对关联数组进行排序arsort() - 根据 ...

  7. 【BZOJ2157】旅游(树链剖分,Link-Cut Tree)

    [BZOJ2157]旅游(树链剖分,Link-Cut Tree) 题面 Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥 ...

  8. SQL Server系统视图sys&period;master&lowbar;files不能正确显示数据库脱机状态

    最近发现在SQL Server数据库(目前测试过SQL Server 2008, 2012,2014,2016各个版本)中,即使数据库处于脱机(OFFLINE)状态,但是sys.master_file ...

  9. gitbook 入门教程之常用命令详解

    不论是 gitbook-cli 命令行还是 gitbook editor 编辑器都离不开 gitbook 命令的操作使用,所以再次了解下常用命令. 注意 gitbook-cli 是 gitbook 的 ...

  10. &period;NET常用功能

    1.判断对象判不为null或不为""或不为"undefined" public static bool isNotNullOrBlank(Object obj) ...