Spring-IOC-@Value和@PropertySource用法

1、Book.java

  • @PropertySource(value="classpath:配置文件地址") 替代 <context:property-placeholder location="配置文件地址"/>

|-----------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @Value("{book.bid}")** **@Value("{book.bname}") @Value("{book.price}")** | **\** **\{book.bname}"/\>** **\

java 复制代码
package com.atguigu.ioc;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Data
@Component
@PropertySource(value = "classpath:book.properties", encoding = "UTF-8")
public class Book {
    @Value("${book.bid}")
    private Integer bid;
    @Value("${book.bname}")
    private String bname;
    @Value("${book.price}")
    private Integer price;
}

2、book.properties

java 复制代码
book.bid=1
book.bname=Java入门经典
book.price=99

3、MySpringConfiguration.java

  • @ComponentScan(basePackages={"包","包"}) 替代 <context:component-scan base-package="com.atguigu.ioc,等等">
java 复制代码
package com.atguigu.ioc;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan
public class MySpringConfiguration {
}

4、BookTest.java

java 复制代码
package com.atguigu.ioc;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class BookTest {

    private BeanFactory beanFactory;
    @BeforeEach
    public void setup() {
        beanFactory = new AnnotationConfigApplicationContext(MySpringConfiguration.class);
    }
    @Test
    public void test() {
        System.out.println(beanFactory.getBean(Book.class));
    }
}
//Book(bid=1, bname=Java入门经典, price=99)

5、父工程pom.xml

XML 复制代码
    <packaging>pom</packaging>
    <modules>
        <module>pro04-spring-ioc-xml</module>
        <module>pro00-spring-handwrite</module>
        <module>pro05-spring-ioc-annotation</module>
    </modules>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>6.0.6</spring.version>
        <junit.version>5.3.1</junit.version>
        <lombok.version>1.18.20</lombok.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>

            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

6、子工程pom.xml

XML 复制代码
    <parent>
        <groupId>com.atguigu</groupId>
        <artifactId>pro-ssm</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>pro05-spring-ioc-annotation</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
相关推荐
张国荣家的弟弟1 分钟前
【Yonghong 企业日常问题 06】上传的文件不在白名单,修改allow.jar.digest属性添加允许上传的文件SH256值?
java·jar·bi
ZSYP-S12 分钟前
Day 15:Spring 框架基础
java·开发语言·数据结构·后端·spring
yuanbenshidiaos19 分钟前
C++----------函数的调用机制
java·c++·算法
是小崔啊37 分钟前
开源轮子 - EasyExcel01(核心api)
java·开发语言·开源·excel·阿里巴巴
黄公子学安全1 小时前
Java的基础概念(一)
java·开发语言·python
liwulin05061 小时前
【JAVA】Tesseract-OCR截图屏幕指定区域识别0.4.2
java·开发语言·ocr
jackiendsc1 小时前
Java的垃圾回收机制介绍、工作原理、算法及分析调优
java·开发语言·算法
qxlxi1 小时前
【Spring事务】深入浅出Spring事务从原理到源码
spring
Yuan_o_1 小时前
Linux 基本使用和程序部署
java·linux·运维·服务器·数据库·后端
Oneforlove_twoforjob1 小时前
【Java基础面试题027】Java的StringBuilder是怎么实现的?
java·开发语言