SpringBoot_1注解

本文探讨了Spring的JavaConfig项目,它提供了一种基于Java代码和注解的依赖关系绑定方式,替代传统的XML配置。文章通过示例展示了@Configuration和@Bean注解的使用,并解释了Spring如何通过方法拦截确保相同类型bean的单例性,从而保证依赖注入的一致性。
摘要由CSDN通过智能技术生成

Spring JavaConfig

   Java 5 的推出,加上当年基于纯 Java Annotation 的依赖注入框架 Guice 的出现,推出并持续完善了基于 Java 代码和 Annotation 元信息的依赖关系绑定描述方式,即 JavaConfig 项目。
   基于 JavaConfig 方式的依赖关系绑定描述基本上映射了最早的基于 XML 的配置方式,比如:

   1)表达形式层面

   基于 XML 的配置方式是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
  <bean id="stubean" class="com.wangxing.javaconfigdemo1.StudentBean"></bean>
</beans>

   而基于 JavaConfig 的配置方式是这样的:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class StudentConfig {
    @Bean
    public StudentBean stubean(){
        return new StudentBean();
    }
}

   任何一个标注了 @Configuration 的 Java 类定义都是一个 JavaConfig 配置类。

   任何一个标注了 @Bean 的方法,其返回值将作为一个 bean 定义注册到 Spring 的 IoC 容器,方法名将默认成为该 bean 定义的 id。

package com.wangxing.javaconfigdemo1;

import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppTest {
    @Test
    public void test1() {
        ApplicationContext context = new AnnotationConfigApplicationContext(StudentConfig.class);
        StudentBean studentBean=(StudentBean)context.getBean("stubean");
        studentBean.testStudent();
    }
}

   完整代码如下,测试set方法注入PersonBean对象

   1.导入依赖

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.1.5.RELEASE</version>
    </dependency>

  2.创建PersonBean

package com.wangxing.javaconfig;

public class PersonBean {
    public void testPerson() {
        System.out.println("PersonBean类的testPerson方法");
    }
}

   3.创建PersonConfig[Person配置类]

package com.wangxing.javaconfig;

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

@Configuration
public class PersonConfig {
    @Bean
    public PersonBean perBean(){
        return new PersonBean();
    }
}

   4.创建StudentBean类

package com.wangxing.javaconfig;

public class StudentBean {
    //定义依赖对象
    private PersonBean personBean;
    //set方法注入依赖对象
    public void setPersonBean(PersonBean personBean){
        this.personBean = personBean;
    }

    public void testStudent(){
        System.out.println("Student类的testStudent方法");
    }

    public void methodStudent(){
        personBean.testPerson();
        System.out.println("Student类的methodStudent方法");
    }
}

   5.创建StudentConfig[StudentBean配置类]

package com.wangxing.javaconfig;

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

@Import(PersonConfig.class)
@Configuration
public class StudentConfig {
    @Bean
    public StudentBean stuBean(){
        StudentBean studentBean = new StudentBean();
        //通过调用StudentBean类的setPersonBean方法将PersonBean对象注入到StudentBean中[set注入]
        studentBean.setPersonBean(new PersonConfig().perBean());
        return studentBean;
    }
}
/**
 * <?xml version="1.0" encoding="UTF-8"?>
 * <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.xsd">
 *  <bean id="stubean" class="com.wangxing.javaconfigdemo1.StudentBean"></bean>
 * </beans>
 */

   6.测试方法

package com.wangxing.javaconfig;

import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class AppTest {
    @Test
    public void shouldAnswerWithTrue() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(StudentConfig.class);
        //参数传入创建实例化bean的方法名
        StudentBean studentBean = (StudentBean) applicationContext.getBean("stuBean");
        //studentBean.testStudent();
        studentBean.methodStudent();
    }
}

   7.测试结果

测试构造方法注入PersonBean对象

package com.wangxing.javaconfig;

public class StudentBean {
    //定义依赖对象
    private PersonBean personBean;
    //set方法注入依赖对象
//    public void setPersonBean(PersonBean personBean){
//        this.personBean = personBean;
//    }
    //测试构造方法注入依赖对象
    public StudentBean(PersonBean personBean){
        this.personBean = personBean;
    }

    public void testStudent(){
        System.out.println("Student类的testStudent方法");
    }

    public void methodStudent(){
        personBean.testPerson();
        System.out.println("Student类的methodStudent方法");
    }
}

测试方法

package com.wangxing.javaconfig;

import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class AppTest {
    @Test
    public void shouldAnswerWithTrue() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(StudentConfig.class);
        //参数传入创建实例化bean的方法名
        StudentBean studentBean = (StudentBean) applicationContext.getBean("stuBean");
        //studentBean.testStudent();
        studentBean.methodStudent();
    }
}

测试结果

   如果一个 bean 的定义依赖其他 bean,则直接调用对应 JavaConfig 类中依赖 bean 的创建方法就可以了。
   在 JavaConfig 形式的依赖注入过程中,我们使用方法调用的形式注入依赖,如果这个方法返回的对象实例只被一个 bean 依赖注入,那也还好,如果多于一个 bean 需要依赖这个方法调用返回的对象实例,那是不是意味着我们就会创建多个同一类型的对象实例?
   从代码表述的逻辑来看,直觉上应该是会创建多个同一类型的对象实例,但实际上最终结果却不是这样,依赖注入的都是同一个 Singleton 的对象实例,那这是如何做到的?
   一开始以为 Spring 框架会通过解析 JavaConfig 的代码结构,然后通过解析器转换加上反射等方式完成这一目的,但实际上 Spring 框架的设计和实现者采用了另一种更通用的方式,这在 Spring 的参考文档中有说明。即通过拦截配置类的方法调用来避免多次初始化同一类型对象的问题,一旦拥有拦截逻辑的子类发现当前方法没有对应的类型实例时才会去请求父类的同一方法来初始化对象实例,否则直接返回之前的对象实例。
   所以,原来 Spring IoC 容器中有的特性(features)在 JavaConfig 中都可以表述,只是换了一种形式而已,而且,通过声明相应的 Java Annotation 反而“内聚”一处,变得更加简洁明了了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
4S店客户管理小程序-毕业设计,基于微信小程序+SSM+MySql开发,源码+数据库+论文答辩+毕业论文+视频演示 社会的发展和科学技术的进步,互联网技术越来越受欢迎。手机也逐渐受到广大人民群众的喜爱,也逐渐进入了每个用户的使用。手机具有便利性,速度快,效率高,成本低等优点。 因此,构建符合自己要求的操作系统是非常有意义的。 本文从管理员、用户的功能要求出发,4S店客户管理系统中的功能模块主要是实现管理员服务端;首页、个人中心、用户管理、门店管理、车展管理、汽车品牌管理、新闻头条管理、预约试驾管理、我的收藏管理、系统管理,用户客户端:首页、车展、新闻头条、我的。门店客户端:首页、车展、新闻头条、我的经过认真细致的研究,精心准备和规划,最后测试成功,系统可以正常使用。分析功能调整与4S店客户管理系统实现的实际需求相结合,讨论了微信开发者技术与后台结合java语言和MySQL数据库开发4S店客户管理系统的使用。 关键字:4S店客户管理系统小程序 微信开发者 Java技术 MySQL数据库 软件的功能: 1、开发实现4S店客户管理系统的整个系统程序; 2、管理员服务端;首页、个人中心、用户管理、门店管理、车展管理、汽车品牌管理、新闻头条管理、预约试驾管理、我的收藏管理、系统管理等。 3、用户客户端:首页、车展、新闻头条、我的 4、门店客户端:首页、车展、新闻头条、我的等相应操作; 5、基础数据管理:实现系统基本信息的添加、修改及删除等操作,并且根据需求进行交流信息的查看及回复相应操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值