SpringBoot 01: JavaConfig + @ImportResource + @PropertyResource

【SpringBoot 01: JavaConfig + @ImportResource + @PropertyResource】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.*;@Configurationpublic class SpringConfig {@Beanpublic 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 {@Testpublic 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 = "https://www.huyubaike.com/biancheng/classpath:applicationContext.xml")public class SpringConfig {}
  • applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="cat" class="com.example.springboot.model.Cat"><property name="catCard" value="https://www.huyubaike.com/biancheng/0010"/><property name="catName" value="https://www.huyubaike.com/biancheng/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 {@Testpublic 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="https://www.huyubaike.com/biancheng/${key}")
需要用的其他注解
  • @Component:用在實體類上
  • @ComponentScan:SpringConfig類上
  • @Value:待注入值的屬性上
代碼實現
  • SpringConfig類
package com.example.springboot.configuration;import com.example.springboot.model.Student;import org.springframework.context.annotation.*;@PropertySource(value = "https://www.huyubaike.com/biancheng/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;@Overridepublic 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() {}}

推薦閱讀