SpringBoot 01: JavaConfig + @ImportResource + @PropertyResource
- 2022 年 11 月 9 日
- 筆記
- springboot
springboot的前置知識:通過註解創建對象和讀取配置文件
1. JavaConfig
設計思想
- 使用java類作為xml配置文件的替代,是配置spring容器的純java的方式
- 可以創建java對象並把對象注入到spring容器中
註解實現
- @Configuration : 放在一個類的上面,表示這個類是作為配置文件使用的
- @Bean:放在返回值是對象的方法上,容器啟動時,聲明對象,並把對象注入到容器中
- 上面兩個註解配套使用
程式碼實現
package com.example.springboot.configuration;
import com.example.springboot.model.Student;
import org.springframework.context.annotation.*;
@Configuration
public class SpringConfig {
@Bean
public Student getStudent(){
Student student = new Student();
student.setName("橘子");
student.setAge(18);
return student;
}
@Bean(name = "student")
public Student getStudentByBeanName(){
Student student = new Student();
student.setName("餃子");
student.setAge(21);
return student;
}
}
測試程式碼
package com.example.springboot.testspringconfig;
import com.example.springboot.configuration.SpringConfig;
import com.example.springboot.model.Student;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestSpringConfig {
@Test
public void testSpringConfig(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
Student student = (Student) applicationContext.getBean("student");
//未在@Bean中指定對象名稱時,從方法名(小駝峰命名規範)來獲取對象
//Student student = (Student) applicationContext.getBean("getStudent");
System.out.println("獲取到的對象: " + student);
}
}
2. @ImportResource
設計思想
- 導入其他的xml配置文件, 等於在xml 使用如下import標籤
<import resources="其他配置文件"/>
程式碼實現
- SpringConfig類
package com.example.springboot.configuration;
import org.springframework.context.annotation.*;
@ImportResource(value = "classpath:applicationContext.xml")
public class SpringConfig {
}
- applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="//www.springframework.org/schema/beans"
xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="//www.springframework.org/schema/beans //www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="cat" class="com.example.springboot.model.Cat">
<property name="catCard" value="0010"/>
<property name="catName" value="tomcat"/>
</bean>
</beans>
測試程式碼
package com.example.springboot.testspringconfig;
import com.example.springboot.configuration.SpringConfig;
import com.example.springboot.model.Cat;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestSpringConfig {
@Test
public void testImportResource(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
Cat cat = (Cat) applicationContext.getBean("cat");
System.out.println("獲取到的對象: " + cat);
}
}
3. @PropertyResource
設計思想
- 讀取properties屬性配置文件,使用屬性配置文件可以實現外部化配置
使用步驟
- 在resources目錄下,創建properties文件, 使用 key=value 的格式提供數據
- 在@PropertyResource 指定properties文件的位置
- 使用在待注入值的變數上使用@Value(value=”${key}”)
需要用的其他註解
- @Component:用在實體類上
- @ComponentScan:SpringConfig類上
- @Value:待注入值的屬性上
程式碼實現
- SpringConfig類
package com.example.springboot.configuration;
import com.example.springboot.model.Student;
import org.springframework.context.annotation.*;
@PropertySource(value = "classpath:food.properties")
@ComponentScan(basePackages = "com.example.springboot.model")
public class SpringConfig {
}
- food.properties
food.name=餃子
food.price=13
- JiaoZi類
package com.example.springboot.model;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("jiaozi")
public class JiaoZi {
@Value("${food.name}")
private String name;
@Value("${food.price}")
private double price;
@Override
public String toString() {
return "JiaoZi{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public JiaoZi(String name, double price) {
this.name = name;
this.price = price;
}
public JiaoZi() {
}
}
測試程式碼
package com.example.springboot.testspringconfig;
import com.example.springboot.configuration.SpringConfig;
import com.example.springboot.model.JiaoZi;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestSpringConfig {
@Test
public void testPropertiesSource(){
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
JiaoZi jiaoZi = (JiaoZi) applicationContext.getBean("jiaozi");
System.out.println("food: " + jiaoZi);
}
}