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>
相关推荐
StayInLove1 分钟前
G1垃圾回收器日志详解
java·开发语言
对许5 分钟前
SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“
java·log4j
鹿屿二向箔6 分钟前
基于SSM(Spring + Spring MVC + MyBatis)框架的咖啡馆管理系统
spring·mvc·mybatis
无尽的大道9 分钟前
Java字符串深度解析:String的实现、常量池与性能优化
java·开发语言·性能优化
小鑫记得努力18 分钟前
Java类和对象(下篇)
java
binishuaio22 分钟前
Java 第11天 (git版本控制器基础用法)
java·开发语言·git
zz.YE24 分钟前
【Java SE】StringBuffer
java·开发语言
老友@24 分钟前
aspose如何获取PPT放映页“切换”的“持续时间”值
java·powerpoint·aspose
wrx繁星点点39 分钟前
状态模式(State Pattern)详解
java·开发语言·ui·设计模式·状态模式
Upaaui42 分钟前
Aop+自定义注解实现数据字典映射
java