首页 > 解决方案 > 基于 Spring 注解的自动装配内部工作

问题描述

我对基于注释的字段、setter 方法和构造函数的自动装配有些困惑。我没有遇到任何问题,只是为了澄清我的理解,我在这里发布了这个。

制作一个包含三个文件的小型 Maven 项目来测试自动装配注释。这是项目结构。

src
  main
    java
      com
        demo
          annotationsAutowiring
                  MainApp.java
                  SpellChecker.java
                  TextEditor.java
        autowiring-config.xml

autowiring-config.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config />
    <bean id="spellChecker" class="com.demo.annotationsAutowiring.SpellChecker"></bean> 
    <bean id="textEditor" class="com.demo.annotationsAutowiring.TextEditor"></bean>
</beans>

这就是 MainApp.java 和 SpellChecker.java 的样子

主应用程序.java

package com.demo.annotationsAutowiring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("autowiring-config.xml");
        TextEditor te = (TextEditor) context.getBean("textEditor");
        te.spellCheck();
    }
}

拼写检查器.java

package com.demo.annotationsAutowiring;

public class SpellChecker {
    public SpellChecker() {
        System.out.println("Inside SpellChecker constructor.");
    }

    public void checkSpelling() {
        System.out.println("Inside checkSpelling.");
    }
}

我使用了注释配置,这表明我可以使用注释。

我将 TextEditor.java 文件连同我的问题一起发布三个不同的版本。

版本 1:字段上的 @Autowired 注释。

package com.demo.annotationsAutowiring;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
    @Autowired      
    private SpellChecker spellChecker;

    public void spellCheck() {
        spellChecker.checkSpelling();
    }
}

版本 2:构造函数上的 @Autowired 注释。

package com.demo.annotationsAutowiring;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
    
    private SpellChecker spellChecker;
        
    @Autowired
    private TextEditor(SpellChecker spellchecker) {
        this.spellChecker = spellchecker;
    }

    public void spellCheck() {
        spellChecker.checkSpelling();
    }
}

版本 3:setter 方法上的 @Autowired 注释

package com.demo.annotationsAutowiring;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
    
    private SpellChecker spellChecker;   

    @Autowired 
    public void setSpellChecker(SpellChecker spellChecker) {
        this.spellChecker = spellChecker;
    }
    public SpellChecker getSpellChecker() {
        return spellChecker;
    }    

    public void spellCheck() {
        spellChecker.checkSpelling();
    }
}

所有版本的代码运行良好,输出如下:

Jul 08, 2021 6:43:21 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1646c37: startup date [Thu Jul 08 18:43:21 IST 2021]; root of context hierarchy
Jul 08, 2021 6:43:21 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [autowiring-config.xml]
Inside SpellChecker constructor.
Inside checkSpelling.

问题:

  1. 当我在 setter 上使用 @Autowired 注释时,内部会发生什么。我的意思是弹簧容器有什么作用?是否调用 SpellCheckers 默认构造函数。
  2. 我看到当我在字段上使用自动装配注释时,我不需要定义 setter 或 getter。因此,在现场自动装配时,spring 容器是否会自行调用 getter 和 setter,而无需用户担心。

标签: springannotationsautowired

解决方案


推荐阅读