常规属性配置

在常规Spring环境下,注入properties文件里的值的方式,通过@PropertySource指明properties文件的位置,然后通过@Value注入之,在SpringBoot里,只需要在application.properties定义属性,直接使用@Value注入即可。

1、application.properties添加项目的属性值:

2、修改入口类这个入口类写在controller类中:

@RestController
@SpringBootApplication
public class Example{
          
        @Value("{application.author}")
        private String applicationAuthor;
        @Value("{application.name}")
        private String applicationName;

        @RequestMappin("/")
        String index(){
             return "Application name is : " + applicationName + " application author is : "+ applicationAuthor;
        }
        
        public static void main(String[] args){
            SpringApplication.run(Example.class,args);
        }
}

3、运行结果:

    

猜你喜欢

转载自www.cnblogs.com/yourGod/p/9208109.html