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>
相关推荐
咖啡教室3 小时前
java日常开发笔记和开发问题记录
java
咖啡教室3 小时前
java练习项目记录笔记
java
鱼樱前端3 小时前
maven的基础安装和使用--mac/window版本
java·后端
RainbowSea4 小时前
6. RabbitMQ 死信队列的详细操作编写
java·消息队列·rabbitmq
RainbowSea4 小时前
5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明
java·消息队列·rabbitmq
李少兄6 小时前
Unirest:优雅的Java HTTP客户端库
java·开发语言·http
此木|西贝6 小时前
【设计模式】原型模式
java·设计模式·原型模式
可乐加.糖6 小时前
一篇关于Netty相关的梳理总结
java·后端·网络协议·netty·信息与通信
s9123601016 小时前
rust 同时处理多个异步任务
java·数据库·rust
9号达人6 小时前
java9新特性详解与实践
java·后端·面试