首页 > 其他分享 >SpringBoot的@Configuration注解

SpringBoot的@Configuration注解

时间:2023-05-09 12:22:22浏览次数:38  
标签:return SpringBoot age name User 注解 Configuration public String

  本文主要讲述SpringBoot的@Configuration注解。

一.POJO类的声明

  例如有两个pojo类,分别是User和Pet

  User类的声明如下:

public class User {

    private String name;
    private Integer age;

    public User(){

    }

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

  Pet类的声明如下:

public class Pet {

    private String name;
    private String clasz;

    public Pet() {
    }

    public Pet(String name, String clasz) {
        this.name = name;
        this.clasz = clasz;
    }

    public String getName() {
        return name;
    }

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

    public String getClasz() {
        return clasz;
    }

    public void setClasz(String clasz) {
        this.clasz = clasz;
    }

    @Override
    public String toString() {
        return "Pet{" +
                "name='" + name + '\'' +
                ", clasz='" + clasz + '\'' +
                '}';
    }
}

二.在Spring的xml中配置组件

  1.pojo无依赖关系,bean.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="user01" class="com.atguigu.boot.pojo.User">
        <property name="name" value="tom"></property>
        <property name="age" value="18"></property>
    </bean>

    <bean id="pet01" class="com.atguigu.boot.pojo.Pet">
        <property name="name" value="maomi"></property>
        <property name="clasz" value="cat"></property>
    </bean>
</beans>

  2.pojo有依赖关系,user类依赖pet类

  User类声明如下:

public class User {

    private String name;
    private Integer age;
    private Pet pet;

    public User(){

    }

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Pet getPet() {
        return pet;
    }

    public void setPet(Pet pet) {
        this.pet = pet;
    }

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", pet=" + pet +
                '}';
    }
}

  bean.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="user01" class="com.atguigu.boot.pojo.User">
        <property name="name" value="tom"></property>
        <property name="age" value="18"></property>
        <property name="pet" ref="pet01"></property>
    </bean>

    <bean id="pet01" class="com.atguigu.boot.pojo.Pet">
        <property name="name" value="maomi"></property>
        <property name="clasz" value="cat"></property>
    </bean>
</beans>

三.SpringBoot的@Configuration配置组件

  @Configuration注解作用在类上,声明该类是配置组件类;

  @Bean注解作用在配置组件类的方法上,声明该方法是ioc容器的组件。

  POJOConfig组件配置类声明如下:

/**
 * @Configuration 标识配置组件的类
 * @Bean 标识配置对象的方法
 */
@Configuration
public class POJOConfig {
    
    @Bean
    public User user01(){
        return new User("张三",19);
    }

    @Bean
    public User user02(){
        return new User("张三",19);
    }

    @Bean
    public Pet pet01(){
        return new Pet("tom","cat");
    }
}

  在MainApp启动类中验证注册的组件是否唯一。

@SpringBootApplication
public class MainApp {

    public static void main(String[] args) {
        // 1.获取ioc容器【应用程序的上下文】
        ConfigurableApplicationContext run = SpringApplication.run(MainApp.class, args);

        // 2.获取上下文所有的bean的name
        String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }

        // 3.获取指定类型的bean的对象
        // 只注册了一个User类的对象,ioc容器获取的也只有一个对象
        // 注册了同一个类的多个对象
        User user01 = run.getBean("user01", User.class);
        User user02 = run.getBean("user01", User.class);
        System.out.println(user01 == user02);  // true

        // 4.获取配置组件类的对象
        POJOConfig bean = run.getBean(POJOConfig.class);
        System.out.println(bean);

        User user1 = bean.user01();
        User user2 = bean.user01();
        System.out.println(user1 == user2);

    }
}

  证明user01和user02对象,都是从ioc容器中取出的 id=user01的对象。

  @Configuration中有一个参数proxyBeanMethod,默认为 true,

  这个注解主要有以下两个作用:

  1. 声明一个类为 Spring 配置类。@Configuration 注解告诉 Spring 这是一个配置类,需要在应用程序上下文中注册 bean。通常情况下,配置类中包含了多个 @Bean 方法,这些方法都会返回一个对象,供其他 bean 使用。

  2. 控制 Spring 是否会为 @Bean 方法创建代理对象。当 proxyBeanMethods 设置为 true 时,Spring 会为每个 @Bean 方法创建一个代理对象,这个代理对象会缓存方法的调用结果,从而提高应用程序的性能。当 proxyBeanMethods 设置为 false 时,Spring 不会为 @Bean 方法创建代理对象,而是每次调用 @Bean 方法时都会创建一个新的对象。

  需要注意的是,如果使用了 @Configuration(proxyBeanMethods = true),那么 @Bean 方法必须是非 final 的,因为 Spring 使用 CGLIB 生成子类来实现代理。如果 @Bean 方法是 final 的,那么就无法生成子类,从而导致代理失败。因此,如果你的 @Bean 方法必须是 final 的,就需要将 proxyBeanMethods 设置为 false。

  

 

标签:return,SpringBoot,age,name,User,注解,Configuration,public,String
From: https://www.cnblogs.com/zwgitOne123/p/17384531.html

相关文章

  • Spring注解开发报错
    今天学习Spring注解开发时,又报错了报错代码Exceptioninthread“main”org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:Line6inXMLdocumentfromclasspathresource[1.xml]isinvalid;nestedexceptionisorg.xml.sax.SAXParseExceptio......
  • Springboot 项目配置 HTTPS
    生成证书输入命令keytool-genkeypair-alias"boot"-keyalg"RSA"-keystore"boot.keystore"生成完成后会提示Warning:JKS密钥库使用专用格式。建议使用"keytool-importkeystore-srckeystoreboot.keystore-destkeystoreboot.keystore-deststoretypepkc......
  • SpringBoot项目如何打包成exe应用程序?
    前言近期做了一个前后端合并的springboot项目,但是要求打包城exe文件,提供给不懂电脑的小白安装使用,就去研究了半天,踩了很多坑,写这篇文章,是想看到这篇文章的人,按照我的步骤走,能少踩坑。准备准备工作:一个jar包,没有bug能正常启动的jar包exe4j,一个将jar转换成exe的工具,链接:h......
  • @Accessors 注解参数
    @Accessors注解参数经常会在实体类上看到,记录一下,方便以后复习@Accessors注解的作用:当属性字段在生成getter和setter方法时,做一些相关的设置。@Accessors共有三个属性,分别是fluent,chain,prefixfluent属性不写默认为false,当该值为true时,对应字段的getter方法前面......
  • 使用IDEA创建第一个SpringBoot项目并进行一些基础配置的详细教程
    1.打开IDEA,新建newproject,填写项目信息。 2.如上图所示,设置serverURL为阿里云服务器为:https://start.aliyun.com/下面的Java版本选择必须和ProjectSDK版本相对应,不然不能进行下一步。3.选择springboot版本和开发会使用到的组件,最后点finish即可。 4.等待IDEA创建并......
  • @RequestParam注解参数
    做业务的时候经常忘记@RequestParam注解参数,记录一下首先,我们要清楚@RequestParam是干什么的@RequestParam:将请求参数绑定到你控制器的方法参数上,路径上有个参数+?@RequestParam注解参数:语法:@RequestParam(value=”参数名”,required=”true/false”,defaultValue=””)......
  • java netty socket实例:报文长度+报文内容,springboot
    前言说实话,javanetty方面的资料不算多,尤其是自定义报文格式的,少之又少自己写了个简单的收发:报文长度+报文内容发送的话,没有写自动组装格式,自己看需求吧,需要的话,自己完善服务端启动可以直接用类文件启动,也可以通过springboot。我这里写的是用springboot启动的,可以自己按照需求自......
  • SpringBoot全局异常处理
    @ControllerAdvice:使用该注解表示开启了全局异常的捕获; 参考链接[1]https://www.cnblogs.com/xuwujing/p/10933082.html[2]https://gitee.com/bruce6213/global-exception-handler......
  • SpringBoot常用注解
    @SpringBootApplication@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@Configuration@EnableAutoConfiguration@ComponentScanpublic@interfaceSpringBootApplication{ /** *Excludespecificauto-configurationclas......
  • 《java接力》springboot篇——注解
    注解后续补充常用注解aspect参考链接:https://zhuanlan.zhihu.com/p/351468451需要依赖:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId></dependency>......