- 首页 > 生活 > >
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);}}
经验总结扩展阅读
-
-
NANA谈健康|出现泡沫尿,是肾出现了问题吗?3种疾病确实要重视起来
-
-
为什么国产的有些电影评分那么低明星的酬劳却是那么多?
-
-
-
-
-
-
-
-
-
-
三亚海鲜市场该如何还价格,三亚的海鲜很贵吗?哪里可以买到便宜又新鲜的平民海鲜呢?
-
2022年9月19日是拜观音吉日吗 2022年9月19日适合拜观音吗
-
霍思燕 杜江霍思燕结婚9年,恩爱如热恋:所谓恩爱夫妻,全靠两人打配合
-
-
-
-
女娲|“女娲补天”或许不是神话?可能发生于上古文明,补的也许是虫洞