IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

SpringBoot:获取值和配置文件(@ConfigurationProperties、@Value、@PropertySource、@ImportResource和@Bean)

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

1、@ConfigurationProperties(prefix = “student”)方式

(1)定义两个实体类,其中student实体类的属性包括Course类:

@Data
@Component
@ConfigurationProperties(prefix = "student")//告诉springboot将本类中的所有属性和配置文件的相关配置进行绑定
public class Student {       //prefix:配置文件中哪一个名称下面的属性进行一一映射
    private String sname;
    private int age;
    private Map<String,Object> maps;
    private List<Object> list;
    private Course course; }
@Data
public class Course {
    private String courseno;
    private String coursename;
}

(2)创建yaml配置文件:

student:
  sname: zhai
  age: 12

  maps: {k1: 12,k2: 13}
  list:
    - zhai
    - zhang
  course:
    courseno: 202007 coursename: javaweb

(3)创建properties文件:

#配置student
student.age=12
student.sname=zhai
student.maps.k1=1
student.maps.k2=2
student.list=a,b,c student.course.courseno=202007 student.course.coursename=java

(4)测试类:

//可以在测试期间很方便地类似编码一样进行自动注入等容器的功能
@SpringBootTestclass Springboot03ApplicationTests {
    @Autowired
    Student student;
    @Test
    void contextLoads() {
        System.out.println(student);
    }
}

(5)需要导入的依赖:配置文件处理器,配置文件进行绑定会有提示

   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.2.1.RELEASE</version>
   </dependency>

110_1.png

2、@Value方式

(1)书写配置文件

#配置student
student.sname=zhai
student.age=12
student.maps.k1=1
student.maps.k2=2
student.list=a,b,c
student.course.courseno=202007
student.course.coursename=java

(2)获取值:

@Data
@Component
public class Student {
    @Value("${student.sname}")
    private String sname;
    @Value("#{2*9}")
    private int age;
    private Map<String,Object> maps;
    private List<Object> list;
    private Course course;
}

(3)@ConfigurationProperties(prefix = “”)方式与@Value方式的比较

@ConfigurationProperties(prefix = “”)方式支持批量注入配置文件的属性,@Value方式需要一个个指定

@ConfigurationProperties(prefix = “”)方式支持松散绑定,@Value方式不支持

@Value方式支持JSR303校验

@Data
@Component
@Validated
public class Student {
    @NonNull
    private String sname;
    private int age;
    private Map<String,Object> maps;
    private List<Object> list;
    private Course course;
}

@Value方式支持SpEl

如果我们只是在某一个业务逻辑中需要获取配置文件的某一项值,可以使用@Value,如果是一个javaBean来和配置文件进行映射,则要使用@ConfigurationProperties(prefix = “”)方式

@RestController
public class HelloController {
    @Value("${student.sname}")
    private String sname;
    @RequestMapping("/hello")
    public String hello(){
        return "hello"+sname;
    }
}

配置文件:

#配置student
student.sname=zhai
student.age=12
student.maps.k1=1
student.maps.k2=2
student.list=a,b,c
student.course.courseno=202007
student.course.coursename=java

3、@PropertySource

(1)配置文件(student.properties)

#配置student
student.sname=zhai
student.age=12
student.maps.k1=1
student.maps.k2=2
student.list=a,b,c
student.course.courseno=202007
student.course.coursename=java

(2)实体类获取值

@Data
@Component
@PropertySource(value = {"classpath:student.properties"})
public class Student {
    private String sname;
    private int age;
    private Map<String,Object> maps;
    private List<Object> list;
    private Course course;
}

@PropertySource是从指定路径下获取数据,默认是从application.properties下获取数据

4、@ImportResource和@Bean

(1)指定spring的配置文件

@SpringBootApplication(scanBasePackages = "com")
@ImportResource(locations = {"classpath:beans.xml"})
public class Springboot02Application {
    public static void main(String[] args) {
        SpringApplication.run(Springboot02Application.class, args);
    }
}

(2)书写spring的配置文件:beans.xml

(3)书写如下配置,可以省略配置文件的书写,用注解来代替

@Configuration
public class MyAppConfig {
    @Bean
    public HelloService helloService(){
        return new HellService();
    }
}

@Configuration说明这是一个配置类,就是在替代之前的配置文件

@Bean标记在方法上,该方式将方法的返回值添加到容器中,容器中组件的ID默认是方法名

文章永久链接:https://tech.souyunku.com/?p=23211


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(72) 打赏



未经允许不得转载:搜云库技术团队 » SpringBoot:获取值和配置文件(@ConfigurationProperties、@Value、@PropertySource、@ImportResource和@Bean)

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367