首页 > 其他分享 >SpringBoot复习:(45)@Component定义的bean会被@Bean定义的同名的bean覆盖

SpringBoot复习:(45)@Component定义的bean会被@Bean定义的同名的bean覆盖

时间:2023-08-21 17:35:37浏览次数:30  
标签:定义 45 springframework Person bean org import public name


有同名的bean需要配置:
spring.main.allow-bean-definition-overriding=true
否则报错。

package cn.edu.tju.component;

import org.springframework.stereotype.Component;

@Component
public class Person {
    private String name;
    private int age;

    {
        this.name = "nameInComponent";
        this.age =33;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
package cn.edu.tju.config;

import cn.edu.tju.component.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PersonConfig {
    @Bean("person")
    Person getPerson(){
        Person p = new Person();
        p.setAge(23);
        p.setName("nameInBeanAnnotation");
        return  p;
    }
}
package cn.edu.tju;

import cn.edu.tju.component.Person;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class Start {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Start.class, args);
        Person person = context.getBean("person", Person.class);
        System.out.println(person.getName());

    }
}

运行结果

SpringBoot复习:(45)@Component定义的bean会被@Bean定义的同名的bean覆盖_spring


标签:定义,45,springframework,Person,bean,org,import,public,name
From: https://blog.51cto.com/amadeusliu/7177768

相关文章

  • SpringBoot复习(54)用于事务处理的InfrastructureAdvisorAutoProxyCreator BeanPostProc
    从类的继承关系看InfrastructureAdvisorAutoProxyCreator是一个BeanPostProcessor.@EnableTransactionManagement注解导入了TransactionManagementConfigurationSelector类,它的代码如下:这个ImportSelector的selectImports方法返回了一个AutoProxyRegistrar,AutoProxyRegistrar代码......
  • 14、P145
    1、”不包含底层const,都可以使用static_cast“目前不理解这句话(整数第四行)1、指针的类型是int*,double*,float*等1、inta=5;int*b=&a;这个时候是可以称呼b为指针。2、把指针放在void*中,其实是指的是把指针放在void这个类型的变量里,加了*,是为说明这是一个指针。3、这......
  • 解析BeanDefinitionRegistry与BeanDefinition合并
    本文分享自华为云社区《Spring高手之路12——BeanDefinitionRegistry与BeanDefinition合并解析》,作者:砖业洋__。1.什么是BeanDefinitionRegistry?BeanDefinitionRegistry是一个非常重要的接口,存在于Spring的org.springframework.beans.factory.support包中,它是Spring中注......
  • 计讯物联边缘计算网关TG452助力燃气柜智慧监测,共织用气安全网
    方案背景近年来,随着城市化进程不断推进,燃气已然成为城市现代化建设的基础设施之一。当前,全国各地利用数字化技术推进燃气的智慧化建设,持续提升燃气监管的信息化水平,实现了从管网、节点、设施设备到燃气柜用户全业务、全场景的智能监管和预警。  作为燃气柜智慧监测的专家企......
  • 前端项目实战壹佰柒拾陆react-admin+material ui-react-admin之Create之构建自定义字
    我是歌谣微信公众号关注前端小歌谣import{useRecordContext,Show,SimpleShowLayout}from'react-admin';constBookAuthor=()=>{constrecord=useRecordContext();if(!record)returnnull;return<span>{record.author}</span>;};......
  • 【算法】用c#实现自定义字符串编码及围栏解码方法
    编写一个函数/方法,它接受2个参数、一个字符串和轨道数,并返回ENCODED字符串。编写第二个函数/方法,它接受2个参数、一个编码字符串和轨道数,并返回DECODED字符串。然后使用围栏密码对其进行解码。这种密码用于通过将每个字符沿着一组“竖状轨道”依次放在对角线上来对字符串进行编......
  • C# .NET 使用HttpClient,以及自定义Header中存在特殊字符的处理方式
    平常我们在使用HttpClient时,只需要创建加上设置调用接口地址就可以使用了,比如:HttpClienthttpClient=httpClientFactory.CreateClient();httpClient.BaseAddress=newUri(接口地址);httpClient.PostAsync(接口方法,HttpContent);但是,在自定义Header......
  • P1345 [USACO5.4] 奶牛的电信Telecowmunication 题解
    P1345[USACO5.4]奶牛的电信Telecowmunication题目描述农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流。这些机器用如下的方式发送电邮:如果存在一个由\(c\)台电脑组成的序列\(a_1,a_2,\cdots,a_c\),且\(a_1\)与\(a_2\)相连,\(a_2\)与......
  • python字符串的定义和表示及使用语法
    Python中字符串是由字符组成的不可变序列。字符串可以用单引号(')或双引号(")括起来表示。例如:s1='HelloWorld's2="PythonProgramming"Python还支持原始字符串,原始字符串以前缀r或R开头,可以包含转义字符但不会被解析。例如:s3=r'C:\Users\name\Documents'在字符串中可......
  • 114514
    个人认为我的思路是比较自然的。首先,显然\(\gcd(a_i,a_j,a_k,a_l)=1\)是不好做的,考虑将其转换成总方案数减去\(\gcd(a_i,a_j,a_k,a_l)\neq1\)的方案数。记后半部分为\(num\),则原问题等价于求\(\binomn4-num\)。考虑怎么求\(num\),显然有\(\gcd(a_i,a_j,a_k,a_l)......