前言

译文链接: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

工程结构目录

如下是本工程的目录结构

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

步骤一:往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...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的装配方式(XML、注解(Annotation)、自动装配)

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

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

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

  8. Spring的自动装配和注解

    Bean的自动装配 自动装配说明 自动装配是使用spring满足bean依赖的一种方法 spring会在应用上下文中为某个bean寻找其依赖的bean. Spring的自动装配需要从两个角度来实现,或 ...

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

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

随机推荐

  1. 最大子段和(c++)

    // 最大子段和.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namesp ...

  2. Java基础知识(壹)

    写在前面的话 这篇博客,是很早之前自己的学习Java基础知识的,所记录的内容,仅仅是当时学习的一个总结随笔.现在分享出来,希望能帮助大家,如有不足的,希望大家支出. 后续会继续分享基础知识手记.希望能 ...

  3. JavaScript 闭包深入浅出

    闭包是什么? 闭包是内部函数可以访问外部函数的变量.它可以访问三个作用域:首先可以访问自己的作用域(也就是定义在大括号内的变量),它也能访问外部函数的变量,和它能访问全局变量. 内部函数不仅可以访问外 ...

  4. 【MSP是什么】MSP认证之项目群管理学习总结

    首先要说的是,我这篇体会是针对一定的背景的,不能算是一种通用的管理方式,只能是我自己的经验总结,能给大家平常的管理提供一点思路,我就很满足了.先说说背景,我所在公司做的是大型桌面应用软件,简单点说就是 ...

  5. ecshop 模板开发总结

    ecshop 模板开发总结 模板标签 1.{$articleTitle|escape:"html"} 描述:用于html转码,url转码,在没有转码的变量上转换单引号,十六进制转码 ...

  6. CentOS 6.6 升级GCC G++ (当前最新版本为v6.1.0) (完整)

    ---恢复内容开始--- CentOS 6.6 升级GCC G++ (当前最新GCC/G++版本为v6.1.0) 没有便捷方式, yum update....   yum install 或者 添加y ...

  7. 【Win10应用开发】签名与验证

    对数据进行签名和验证,是为了防止数据被“盗版”.比较常规的做法是通过公钥进行验证. 算法上多用SAH_xxxx,就是哈希算法.由于MD5后来被发现存在“非唯一性”,你会发现现在很多一些下载的文件验证都 ...

  8. JavaScript语言精粹--执行环境及作用域,this

    1.执行环境定义了变量或函数有权访问的其他数据,决定了他们各自的行为. 每个执行环境都有一个与之关联的变量对象,环境中定义的所有变量和函数都保存在这个对象中. 虽然我们无法访问,但是解析器在处理数据时 ...

  9. 高性能JavaScript--数据存储(简要学习笔记二)

    1.JavaScript中四种基本数据存取位置:字面量,本地变量,数组元素,对象成员. 一般来说:[字面量,局部变量]运行速度>[数组,对象成员]   2.内部属性包含了一个函数被创建的作用域中 ...

  10. C#中的Session

    一: 网站开发中,为了保存用户信息我们就会用到session. Session具有以下特点:(1)Session中的数据保存在服务器端:(2)Session中可以保存任意类型的数据:(2)Sessio ...