- 首页 > 生活 > >
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);}}
经验总结扩展阅读
-
-
ETC夏天暴晒有事吗,ETC电池防爆吗 现在炎炎夏日汽车内的温度很高,而ETC有电池会不会晒爆呢
-
-
医养库 清理肺部的食物,不是猪血,而是它,平时多吃点,清肺更养肺!
-
-
十二星座喜欢的恋爱状态,水瓶真是太自恋了,双鱼居然怕鬼
-
-
八字算命,2020年三月十七适合新店或公司开张吗?
-
凤凰衣是生鸡蛋还是熟鸡蛋,中医上熟鸡蛋里面的薄皮是什么
-
-
-
异性聊天|异性聊天,将在干嘛换成这3句,对方指定反过来 撩你
-
-
乳液 经典好用的清爽型面霜推荐!美白补水修复强,效果不输贵妇货
-
-
炒熟的鸡蛋可以隔夜第二天吃吗,炒的鸡蛋可以隔夜吃吗 炒的鸡蛋过夜吃好吗
-
-
-
-