@Configuration+@ConfigurationProperties+@EnableCo

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了@Configuration+@ConfigurationProperties+@EnableCo相关的知识,希望对你有一定的参考价值。

@Configuration+@ConfigurationProperties+@EnableConfigurationProperties

技术图片

最佳设计方案(现在又改板了):

Bean上面直接设置@ConfigurationProperties

//需要依赖spring-boot-configuration-processor

@ConfigurationProperties(prefix = "stu")

public class Stu {

String name;

public Stu(String name) {

    this.name = name;

}

public Stu() {

}

public String getName() {

    return name;

}

public void setName(String name) {

    this.name = name;

}

@Override

public String toString() {

    return "Stu{" +

            "name=‘" + name + ‘‘‘ +

            ‘}‘;

}

}

方式一:configuration上面跟随@EnableConfigurationProperties【这种情况只有configuration里面一个Bean 】

@Configuration

@EnableConfigurationProperties

public class StuConfiguration {

@Bean

public Stu stu(){

    return  new Stu();

}

}

方式二:configuration上面跟随@EnableConfigurationProperties【这种情况只有configuration里面一个Bean 】

package com.example.demo11.configuration;

import com.example.demo11.entity.Stu;

import org.springframework.boot.context.properties.EnableConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration

//@EnableConfigurationProperties(Stu.class),如果携带class参数则有用注册Bean的能力,

//因为实现了ImportBeanDefinitionRegistrar接口,Import三插之一

//这里的Bean名称非常有意思【类型名称+全包名】

@EnableConfigurationProperties(Stu.class)

public class StuConfiguration {

@Bean

public Stu stu2(){

    return  new Stu();

}

}

启动查看

@SpringBootApplication

public class Demo11Application {

public static void main(String[] args) {

    ConfigurableApplicationContext context = SpringApplication.run(Demo11Application.class, args);

    Stu stu = context.getBean("stu-com.example.demo11.entity.Stu", Stu.class);

    System.out.println(context.getBeansOfType(Stu.class).size());

    System.out.println(stu.toString());

    context.close();

}

}

以上是关于@Configuration+@ConfigurationProperties+@EnableCo的主要内容,如果未能解决你的问题,请参考以下文章

面试官:@Configuration 和 @Component 注解的区别?大部分人都会答错!

node_modules/firebase-functions/lib/function-configuration.d.ts:4:64 - 错误 TS1005: ']' 预期

serverless knative实战

serverless knative实战

serverless knative实战

sh Configuraçãodocouchdb