• Spring-IOC-@Value和@PropertySource用法


    1、Book.java

    • @PropertySource(value="classpath:配置文件地址") 替代 
    • @Value("${book.bid}")

      @Value("${book.bname}")

      @Value("${book.price}")

                 

                 

                 

    1. package com.atguigu.ioc;
    2. import lombok.Data;
    3. import org.springframework.beans.factory.annotation.Value;
    4. import org.springframework.context.annotation.PropertySource;
    5. import org.springframework.stereotype.Component;
    6. @Data
    7. @Component
    8. @PropertySource(value = "classpath:book.properties", encoding = "UTF-8")
    9. public class Book {
    10. @Value("${book.bid}")
    11. private Integer bid;
    12. @Value("${book.bname}")
    13. private String bname;
    14. @Value("${book.price}")
    15. private Integer price;
    16. }

    2、book.properties

    1. book.bid=1
    2. book.bname=Java入门经典
    3. book.price=99

    3、MySpringConfiguration.java

    • @ComponentScan(basePackages={"包","包"}) 替代
    1. package com.atguigu.ioc;
    2. import org.springframework.context.annotation.ComponentScan;
    3. @ComponentScan
    4. public class MySpringConfiguration {
    5. }

    4、BookTest.java

    1. package com.atguigu.ioc;
    2. import org.junit.jupiter.api.BeforeEach;
    3. import org.junit.jupiter.api.Test;
    4. import org.springframework.beans.factory.BeanFactory;
    5. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    6. public class BookTest {
    7. private BeanFactory beanFactory;
    8. @BeforeEach
    9. public void setup() {
    10. beanFactory = new AnnotationConfigApplicationContext(MySpringConfiguration.class);
    11. }
    12. @Test
    13. public void test() {
    14. System.out.println(beanFactory.getBean(Book.class));
    15. }
    16. }
    17. //Book(bid=1, bname=Java入门经典, price=99)

     5、父工程pom.xml

    1. <packaging>pompackaging>
    2. <modules>
    3. <module>pro04-spring-ioc-xmlmodule>
    4. <module>pro00-spring-handwritemodule>
    5. <module>pro05-spring-ioc-annotationmodule>
    6. modules>
    7. <properties>
    8. <maven.compiler.source>17maven.compiler.source>
    9. <maven.compiler.target>17maven.compiler.target>
    10. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    11. <spring.version>6.0.6spring.version>
    12. <junit.version>5.3.1junit.version>
    13. <lombok.version>1.18.20lombok.version>
    14. properties>
    15. <dependencyManagement>
    16. <dependencies>
    17. <dependency>
    18. <groupId>org.springframeworkgroupId>
    19. <artifactId>spring-contextartifactId>
    20. <version>${spring.version}version>
    21. dependency>
    22. <dependency>
    23. <groupId>org.junit.jupitergroupId>
    24. <artifactId>junit-jupiter-apiartifactId>
    25. <version>${junit.version}version>
    26. <scope>testscope>
    27. dependency>
    28. <dependency>
    29. <groupId>org.projectlombokgroupId>
    30. <artifactId>lombokartifactId>
    31. <version>${lombok.version}version>
    32. dependency>
    33. dependencies>
    34. dependencyManagement>

    6、子工程pom.xml 

    1. <parent>
    2. <groupId>com.atguigugroupId>
    3. <artifactId>pro-ssmartifactId>
    4. <version>1.0-SNAPSHOTversion>
    5. parent>
    6. <artifactId>pro05-spring-ioc-annotationartifactId>
    7. <dependencies>
    8. <dependency>
    9. <groupId>org.springframeworkgroupId>
    10. <artifactId>spring-contextartifactId>
    11. dependency>
    12. <dependency>
    13. <groupId>org.junit.jupitergroupId>
    14. <artifactId>junit-jupiter-apiartifactId>
    15. dependency>
    16. <dependency>
    17. <groupId>org.projectlombokgroupId>
    18. <artifactId>lombokartifactId>
    19. dependency>
    20. dependencies>
  • 相关阅读:
    中尺度混凝土二维有限元求解——运行弯曲、运行光盘、运行比较、运行半圆形(Matlab代码实现)
    漫谈计算机网络:物理层 ----- 双绞线&光纤?,从最底层开始了解计算机网络
    GoLang之channel数据结构及阻塞、非阻塞操作、多路select
    2022年经典散文:滚烫的石板
    按照指定条件对数据进行分组并对每个分组内的全部数据应用自定义函数进行聚合计算groupby().apply()
    驱动day8
    Unity --- 视频播放与角色控制器
    弘辽科技:淘宝提升销量后销量清零是为什么?提升销量要注意什么
    【杭电多校第四场 B题】最短路图+缩点dp
    数据库设计详细教程上篇(数据库设计流程)
  • 原文地址:https://blog.csdn.net/m0_65152767/article/details/134538306