- 首页 > 生活 > >
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=13
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() {}}
测试代码
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);}}
经验总结扩展阅读
-
-
-
-
景一说装修|四五十平的刚需小户型,学她家这么设计,耐看又高级
-
《我是真的爱你》家庭、事业、孩子之间的关系三难全?
-
导语不管是谁|婚外情发生之后,女人多久会厌倦情人?3个出轨的女人说出答案
-
-
喝酸奶可以解酒吗,喝完酒后喝酸奶能解酒吗?为什么呢?
-
-
为什么初恋总是很难遗忘 初恋于大多数人而言到底意味着什么
-
-
林噙霜|《知否》:为何盛老太太没有揭破林噙霜害死卫小娘的阴谋?
-
-
-
-
-
波长美容院做的面部抗衰项目中 含有NIR牛奶光的美白方式有哪些?
-
-
-