検索
ホームページJava&#&チュートリアルインタビュアー: @Configuration と @Component の違い

昨日、友人が、アノテーション @Configuration@ について質問されたと私に報告してくれました。インタビュー. コンポーネントの違い。

一文でまとめると、@Configuration 内の @Bean アノテーションを持つすべてのメソッドは動的にプロキシされるため、このメソッドを呼び出すと次の結果が返されます。同じインスタンス。

理解: @Configuration クラスの @Bean アノテーション付きメソッドを呼び出すと、同じ例が返されますが、@Component クラスの @ メソッドBean アノテーションが付けられたは、新しいインスタンスを返します。

注: 上記の呼び出しは Spring コンテナーから取得されたものではありません! 以下の下部の例 1 と例 2

を参照してください。実装の詳細を確認してください。

@構成アノテーション

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
    String value() default "";
}

定義から、@構成アノテーションは本質的に @ Component であるため、<component-scan></component-scan> または @ComponentScan は、@Configuration のアノテーションが付けられたクラスを処理できます。

@構成マークされたクラスは次の要件を満たす必要があります:

  • 配置类必须以类的形式提供(不能是工厂方法返回的实例),允许通过生成子类在运行时增强(cglib 动态代理)。
  • 配置类不能是final 类(没法动态代理)。
  • 配置注解通常为了通过 @Bean注解生成 Spring 容器管理的类,
  • 配置类必须是非本地的(即不能在方法中声明,不能是 private)。
  • 任何嵌套配置类都必须声明为static。
  • @Bean方法可能不会反过来创建进一步的配置类(也就是返回的 bean 如果带有 @Configuration,也不会被特殊处理,只会作为普通的 bean)。

@Bean 注解方法执行策略

先给一个简单的示例代码:

@Configuration
public class MyBeanConfig {

    @Bean
    public Country country(){
        return new Country();
    }

    @Bean
    public UserInfo userInfo(){
        return new UserInfo(country());
    }

}

相信大多数人第一次看到上面 userInfo() 中调用 country()时,会认为这里的 Country和上面 @Bean方法返回的 Country 可能不是同一个对象,因此可能会通过下面的方式来替代这种方式:

  • @Autowired
  • private Country country;

实际上不需要这么做(后面会给出需要这样做的场景),直接调用country() 方法返回的是同一个实例。

@Component 注解

@Component注解并没有通过 cglib 来代理@Bean 方法的调用,因此像下面这样配置时,就是两个不同的 country

@Component
public class MyBeanConfig {

    @Bean
    public Country country(){
        return new Country();
    }

    @Bean
    public UserInfo userInfo(){
        return new UserInfo(country());
    }

}

有些特殊情况下,我们不希望 MyBeanConfig被代理(代理后会变成WebMvcConfig$$EnhancerBySpringCGLIB$$8bef3235293)时,就得用 @Component,这种情况下,上面的写法就需要改成下面这样:

@Component
public class MyBeanConfig {

    @Autowired
    private Country country;

    @Bean
    public Country country(){
        return new Country();
    }

    @Bean
    public UserInfo userInfo(){
        return new UserInfo(country);
    }

}

这种方式可以保证使用的同一个 Country 实例。

示例 1:调用@Configuration类中的@Bean注解的方法,返回的是同一个示例

第一个bean类

package com.xl.test.logtest.utils;

public class Child {
 private String name = "the child";

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
}

第二个bean类

package com.xl.test.logtest.utils;

public class Woman {
 
 private String name = "the woman";
 
 private Child child;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public Child getChild() {
  return child;
 }

 public void setChild(Child child) {
  this.child = child;
 }
}

@Configuration

package com.xl.test.logtest.utils;

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

@Configuration
//@Component
public class Human {
 
 @Bean
 public Woman getWomanBean() {
  Woman woman = new Woman();
  woman.setChild(getChildBean()); // 直接调用@Bean注解的方法方法getChildBean()
  return woman;
 }
 
 @Bean
 public Child getChildBean() {
  return new Child();
 }
}

测试类 I

本测试类为一个配置类,这样启动项目是就可以看到测试效果的,更加快捷;也可以使用其他方式测试见下面的测试类 II

package com.xl.test.logtest.utils;

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

@Configuration
public class Man {
 
 @Autowired
 public Man(Woman wn, Child child) {
  System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
  System.out.println(wn.getChild() == child ? "是同一个对象":"不是同一个对象");
 }
}

启动项目,查看输出结果:

インタビュアー: @Configuration と @Component の違い

测试类 II

package com.xl.test.logtest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xl.test.logtest.utils.Child;
import com.xl.test.logtest.utils.Woman;

@RestController
public class LogTestController {
 @Autowired
 Woman woman ;
 
 @Autowired
 Child child;
 
 @GetMapping("/log")
 public String log() {
  return woman.getChild() == child ? "是同一个对象":"不是同一个对象";
 }
}

浏览器访问项目,查看结果;输入localhost:8080/log

インタビュアー: @Configuration と @Component の違い

示例 2 :调用@Component类中的@Bean注解的方法,返回的是一个新的实例。

测试代码,只需要将@Configuration改为@Component即可!其他的均不变

package com.xl.test.logtest.utils;

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

//@Configuration
@Component
public class Human {
 
 @Bean
 public Woman getWomanBean() {
  Woman woman = new Woman();
  woman.setChild(getChildBean()); // 直接调用@Bean注解的方法方法getChildBean()
  return woman;
 }
 
 @Bean
 public Child getChildBean() {
  return new Child();
 }
}

测试 :

インタビュアー: @Configuration と @Component の違い

控制台和浏览器展示,均符合预期!

最后,如果你也需要修改简历,需要模拟面试的。

以上がインタビュアー: @Configuration と @Component の違いの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事はJava后端技术全栈で複製されています。侵害がある場合は、admin@php.cn までご連絡ください。
面试官:@Configuration 和 @Component 的区别面试官:@Configuration 和 @Component 的区别Aug 15, 2023 pm 04:29 PM

调用@Configuration类中的@Bean注解的方法,返回的是同一个示例;而调用@Component类中的@Bean注解的方法,返回的是一个新的实例。

vue3怎么使用defineAsyncComponent与component标签实现动态渲染组件vue3怎么使用defineAsyncComponent与component标签实现动态渲染组件May 12, 2023 pm 05:55 PM

一、基础的动态引入组件:简单的动态引入的意思是,前端知道要引入哪些组件,将多个组件引入到父组件中,但不渲染它,满足一定条件后,才去在某个位置渲染指定的组件。import{reactive,ref,shallowReactive,onActivated,defineAsyncComponent,}from&#39;vue&#39;;constcustomModal=defineAsyncComponent(()=>import(&#39;./modal/CustomM

Vue 中使用 mixin、extend、component 等 API 实现组件定制的技巧Vue 中使用 mixin、extend、component 等 API 实现组件定制的技巧Jun 25, 2023 pm 03:28 PM

Vue.js是一个流行的前端框架,它提供了许多API用于组件的定制。本文将介绍Vue中mixin、extend、component等API,帮助您掌握组件定制的技巧。mixinmixin是Vue中重用组件代码的一种方式。它允许我们将已经编写的代码复用到不同的组件中,从而减少重复代码的编写。例如,我们可以使用mixin帮助我们在多个组

Java中@Configuration的使用场景是什么?Java中@Configuration的使用场景是什么?Apr 21, 2023 am 10:37 AM

一、简单介绍@Configuration注解可以标注到类上,当标注到类上时,启动Spring就会自动扫描@Configuration注解标注的类,将其注册到IOC容器中,并被实例化成Bean对象。如果被@Configuration注解标注的类中存在使用@Bean注解标注的创建某个类对象的方法,那么,Spring也会自动执行使用@Bean注解标注的方法,将对应的Bean定义信息注册到IOC容器,并进行实例化。二、注解说明@Configuration注解是从Spring3.0版本开始加入的一个使Sp

react.component 报错怎么办react.component 报错怎么办Dec 20, 2022 am 10:49 AM

react.component报错的解决办法:1、打开相应的react文件,查找“class Counter extends Component {static propTypes = {...”语句,将等号改为冒号;2、修改“{ "presets": ["react", "es2015", "stage-0"]}”即可。

Springboot自动配置与@Configuration配置类实例分析Springboot自动配置与@Configuration配置类实例分析May 14, 2023 pm 06:25 PM

@Configuration注意点1配置类(@Configuration下的这个类)其实相当于一个工厂,标注@Bean注解的方法相当于工厂方法考虑有如下例子:@Configuration//注意点1:配置类其实相当于一个工厂,标注@Bean注解的方法相当于工厂方法staticclassMyConfig{@BeanpublicBean1bean1(){System.out.println("bean1()");returnnewBean1();}将来如果要生成一个bean1的实

react的component是什么react的component是什么Dec 05, 2022 pm 05:54 PM

在react中,component中文意思为“组件”,是封装起来的具有独立功能的UI部件;将要展示的内容,分成多个独立部分,每一个这样的部分,就是一个组件。组件有两个重要的东西,一个是属性,一个是状态。组件的属性是父组件给它的,存储的是父组件对子组件的要求,在组件内部可以对属性进行访问,但不可以修改它;组件的状态,由组件自行定义和使用,用来存储组件当前状态,组件的状态可以修改。

Springboot中@Configuration和@bean注解怎么用Springboot中@Configuration和@bean注解怎么用May 12, 2023 pm 02:46 PM

@Configuration注解可以达到在Spring中使用xml配置文件的作用@Bean就等同于xml配置文件中的在spring项目中我们集成第三方的框架如shiro会在spring.xml配置文件中进行配置,例如:/css/**=anon/js/**=anon/validatecode.jsp*=anon/images/**=anon/login.jsp=anon/service/**=anon/**=authc在springboot与shiro整合:@Configurationpublic

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

MantisBT

MantisBT

Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強力な PHP 統合開発環境

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

DVWA

DVWA

Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、