spring基础系列之JavaConfig配置详解

 更新时间:2017年07月15日 08:59:22   作者:唯一浩哥  
本篇文章主要介绍了spring基础系列之JavaConfig配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

早以前,Spring推荐使用XML的方式来定义Bean及Bean之间的装配规则,但是在Spring3之后,Spring提出的强大的JavaConfig这种类型安全的Bean装配方式,它基于Java代码的灵活性,使得装配的过程也变得及其灵活。

使用JavaConfig来装配Bean拥有其自己的一套规则,我们在这里来看一看:

1、规则

规则一:@Configuration注解

我们在定义JavaConfig类时,都会在其上加注@Configuration注解,来表明这是一个配置类,@Configuration注解底层是@Component注解,而且这个注解会被AnnotationConfigApplicationContext来进行加载,AnnotationConfigApplicationContext是ApplicationContext的一个具体实现,代表依据配置注解启动应用上下文。

规则二:@ComponentScan注解

我们使用JavaConfig的目的是为了实现以前XML配置实现的功能,首先就是组件扫描功能,将我们使用特定注解标注的类统一扫描加载到Spring容器,这一功能就是依靠@ComponentScan注解来实现的,我们可以为其指定位置参数来指定要扫描的包。

规则三:@Bean注解

使用@Bean注解我们可以实现XML配置中手动配置第三方Bean的功能,这里我们使用方法来定义Bean,并在方法前面加注@Bean注解,表示要将该方法返回的对象加载到Spring容器中,这样就对我们的方法定义带来了一些限制,这些限制包括方法的大概格式:

1-方法带返回值,且返回类型为你要加载的第三方类类型

2-方法的名称为默认的Bean的name,如果要自定义Bean的name,可以使用@Bean注解的name属性。

3-要实现注入只需要将要注入的Bean的类型作为参数,调用该类的带参数的构造器构建这个Bean,或者采用第二种方式:先创建这个类的对象,然后调用该对象的set方法进行注入,以被注入的Bean的方法为参数

规则验证:

首先我们创建几个测试类

针对第一种注入方式:

1-StudentService

import org.springframework.stereotype.Service;

@Service
public class StudentService {
  public void study(){
    System.out.println("学生学习Java");
  }
}

2-TeacherService

import org.springframework.stereotype.Service;

@Service
public class TeacherService {
  
  private StudentService studentService;
  
  public TeacherService(StudentService studentService){
    this.studentService=studentService;
  }
  
  public void teach(){
    studentService.study();
  }
}

3-Config:这是针对第一种注入方式而设,需要在TeacherService 中定义带参数的构造器

import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
//@ComponentScan
public class Config {
  
  @Bean(name="student")
  public StudentService studentService(){
    return new StudentService();
  }
  
  @Bean(name="teacher")
  public TeacherService teacherService(StudentService studentService){
    return new TeacherService(studentService);
  }
  
}

针对第二种注入方式:

1-StudentService

public class StudentService {
  
  public void study(){
    System.out.println("学生在学习Java");
  }
  
}

2-TeacherService

public class TeacherService {
    
  private StudentService studentService;

  public StudentService getStudentService() {
    return studentService;
  }

  public void setStudentService(StudentService studentService) {
    this.studentService = studentService;
  }
  
  public void teach(){
    studentService.study();
  }
  
}

3-Config:这是采用第二种注入方式:需要在TeacherService中提供set方法

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
  
  @Bean
  public StudentService student(){
    return new StudentService();
  }
  
  @Bean
  public TeacherService teacher(){
    TeacherService teacherService = new TeacherService();
    teacherService.setStudentService(student());
    return teacherService;
  }
  
}

4-测试类:TestMain

import java.util.Iterator;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestMain {

  public static void main(String[] args) {
    AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(Config.class);
    TeacherService teacher = acac.getBean(TeacherService.class);
    teacher.teach();
    Iterator<String> i = acac.getBeanFactory().getBeanNamesIterator();
    while(i.hasNext()){
      System.out.println(i.next());
    }
    acac.close();
  }

}

执行结果:

七月 14, 2017 4:10:56 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Fri Jul 14 16:10:56 CST 2017]; root of context hierarchy
学生学习Java
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessor
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.event.internalEventListenerProcessor
org.springframework.context.event.internalEventListenerFactory
config
student
teacher
environment
systemProperties
systemEnvironment
org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry
messageSource
applicationEventMulticaster
lifecycleProcessor
七月 14, 2017 4:10:59 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7e6cbb7a: startup date [Fri Jul 14 16:10:56 CST 2017]; root of context hierarchy

该测试结果中打印出的是Spring上下文中所有加载的Bean的名称(name)。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java可重入锁reentrantLock解析

    Java可重入锁reentrantLock解析

    这篇文章主要介绍了Java可重入锁reentrantLock解析,reentrantLock跟synchronized代码结构差不多,只是多了一个lock和unlock的过程,需要的朋友可以参考下
    2023-12-12
  • 浅谈springboot中tk.mapper代码生成器的用法说明

    浅谈springboot中tk.mapper代码生成器的用法说明

    这篇文章主要介绍了浅谈springboot中tk.mapper代码生成器的用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • SpringBoot整合MybatisPlus实现增删改查功能

    SpringBoot整合MybatisPlus实现增删改查功能

    MybatisPlus是国产的第三方插件, 它封装了许多常用的CURDapi,免去了我们写mapper.xml的重复劳动。本文将整合MybatisPlus实现增删改查功能,感兴趣的可以了解一下
    2022-05-05
  • MyBatis通用Mapper和PageHelper的过程详解

    MyBatis通用Mapper和PageHelper的过程详解

    这篇文章主要介绍了MyBatis通用Mapper和PageHelper的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • Spring Gateway动态路由的实现方案

    Spring Gateway动态路由的实现方案

    Spring Cloud Gateway (下文简称 Gateway)作为微服务网关,动态配置路由是刚需,毕竟没人想路由一改就要重启网关,本文结合源码,提供一种动态路由配置的思路,需要的朋友可以参考下
    2025-09-09
  • 使用Java的Lucene搜索工具对检索结果进行分组和分页

    使用Java的Lucene搜索工具对检索结果进行分组和分页

    这篇文章主要介绍了使用Java的搜索工具Lucene对检索结果进行分组和分页的方法,Luence是Java环境中的一个全文检索引擎工具包,需要的朋友可以参考下
    2016-03-03
  • 详解Spring工厂特性

    详解Spring工厂特性

    今天带大家学习Spring的特性-工厂特性,文中有非常详细的介绍及代码示例,对正在学习java的小伙伴们有很好地帮助,需要的朋友可以参考下
    2021-05-05
  • 一文详解SpringBoot Redis多数据源配置

    一文详解SpringBoot Redis多数据源配置

    Spring Boot默认只允许一种 Redis 连接池配置,且配置受限于 Lettuce 包,不够灵活,所以本文将为大家介绍如何自定义Redis配置方案实现多数据源支持,需要的可以参考下
    2024-11-11
  • SpringBoot中的Bean注入问题

    SpringBoot中的Bean注入问题

    SpringBoot开发中,Bean注入是关键,涉及构造函数注入、Setter注入和字段注入等方法,常见问题包括Bean未找到、循环依赖、多个实现注入等,推荐使用构造函数注入以增强代码测试性和维护性,并关注Bean的生命周期和作用域
    2024-09-09
  • 基于OpenID Connect及Token Relay实现Spring Cloud Gateway

    基于OpenID Connect及Token Relay实现Spring Cloud Gateway

    这篇文章主要介绍了基于OpenID Connect及Token Relay实现Spring Cloud Gateway,Spring Cloud Gateway旨在提供一种简单而有效的方式来路由到API,并为API提供跨领域的关注点,如:安全性、监控/指标和弹性
    2022-06-06

最新评论