1、Book.java
- @PropertySource(value="classpath:配置文件地址") 替代
@Value("${book.bid}")
@Value("${book.bname}")
@Value("${book.price}")
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
book.bid=1 book.bname=Java入门经典 book.price=993、MySpringConfiguration.java
- @ComponentScan(basePackages={"包","包"}) 替代
package com.atguigu.ioc; import org.springframework.context.annotation.ComponentScan; @ComponentScan public class MySpringConfiguration { }4、BookTest.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
<packaging>pompackaging> <modules> <module>pro04-spring-ioc-xmlmodule> <module>pro00-spring-handwritemodule> <module>pro05-spring-ioc-annotationmodule> modules> <properties> <maven.compiler.source>17maven.compiler.source> <maven.compiler.target>17maven.compiler.target> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <spring.version>6.0.6spring.version> <junit.version>5.3.1junit.version> <lombok.version>1.18.20lombok.version> properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.junit.jupitergroupId> <artifactId>junit-jupiter-apiartifactId> <version>${junit.version}version> <scope>testscope> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> <version>${lombok.version}version> dependency> dependencies> dependencyManagement>6、子工程pom.xml
<parent> <groupId>com.atguigugroupId> <artifactId>pro-ssmartifactId> <version>1.0-SNAPSHOTversion> parent> <artifactId>pro05-spring-ioc-annotationartifactId> <dependencies> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-contextartifactId> dependency> <dependency> <groupId>org.junit.jupitergroupId> <artifactId>junit-jupiter-apiartifactId> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> dependency> dependencies>