1、Book.java
- @PropertySource(value="classpath:配置文件地址") 替代 <context:property-placeholder location="配置文件地址"/>
|-----------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| @Value("{book.bid}")** **@Value("{book.bname}") @Value("{book.price}")** | **\** **\ {book.bname}"/\>** **\ javapackage 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
javabook.bid=1 book.bname=Java入门经典 book.price=99
3、MySpringConfiguration.java
- @ComponentScan(basePackages={"包","包"}) 替代 <context:component-scan base-package="com.atguigu.ioc,等等">
javapackage com.atguigu.ioc; import org.springframework.context.annotation.ComponentScan; @ComponentScan public class MySpringConfiguration { }
4、BookTest.java
javapackage 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>
Spring-IOC-@Value和@PropertySource用法
丁总学Java2023-11-29 19:37
相关推荐
空の鱼4 小时前
java开发,IDEA转战VSCODE配置(mac)P7进阶路5 小时前
Tomcat异常日志中文乱码怎么解决小丁爱养花6 小时前
Spring MVC:HTTP 请求的参数传递2.0CodeClimb6 小时前
【华为OD-E卷 - 第k个排列 100分(python、java、c++、js、c)】等一场春雨6 小时前
Java设计模式 九 桥接模式 (Bridge Pattern)带刺的坐椅6 小时前
[Java] Solon 框架的三大核心组件之一插件扩展体系不惑_7 小时前
深度学习 · 手撕 DeepLearning4J ,用Java实现手写数字识别 (附UI效果展示)费曼乐园7 小时前
Kafka中bin目录下面kafka-run-class.sh脚本中的JAVA_HOMEfeilieren7 小时前
SpringBoot 搭建 SSE阿岳3168 小时前
Java导出通过Word模板导出docx文件并通过QQ邮箱发送