- 首页 > 生活 > >
SpringBoot 01: JavaConfig + @ImportResource + @PropertyResource( 二 )
@ComponentScan:SpringConfig类上@Value:待注入值的属性上代码实现
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.name=饺子food.price=13package 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() {}}测试代码
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 {@Testpublic void testPropertiesSource(){ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);JiaoZi jiaoZi = (JiaoZi) applicationContext.getBean("jiaozi");System.out.println("food: " + jiaoZi);}}
经验总结扩展阅读
-
-
质地 口红种草:国货口红,随便一支都好看,平价也能涂出高级感!
-
-
1、男人出轨 丈夫抛妻弃子十几年,丈夫求回归家庭,妻子说出3个真相
-
-
-
-
-
-
有人说:“男女若是爱不下去了 一个异性突然跟你断联,其实就是分手了,不必追
-
骁龙700的参数详细简介 骁龙700相当于什么处理器
-
-
人与人最好的相处模式:尊重是标配,靠谱是高配,厚道是顶配
-
-
夏雪华|人民网评女子举报前婆婆:通报难以服众,仅自查自纠远远不够
-
摩尔庄园铿锵玫瑰获得方法 摩尔庄园铿锵玫瑰如何获得
-
-
-
-