学习的内容主要是汪云飞的《Spring Boot实战》
(一)命令行参数配置
springboot项目可以基于jar包运行,打开jar的程序可以通过下面命令行运行:
java -jar xxx.jar
可以通过以下命令修改tomcat端口号
java -jar xxx.jar --server.port=9090
(二)常规属性配置
在springboot项目中,我们只需在application.properties定义属性,直接使用@Value注入即可
(1)application.properties中添加属性
book.author=wangyunfei book.name=spring boot
(2)修改入口类
package com.vincent.demo; import com.vincent.demo.config.Author; import com.vincent.demo.config.HelloService; import com.vincent.demo.config.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class DemoApplication { @Value("${book.author}") private String bookAuthor; @Value("${book.name}") private String bookName; @RequestMapping("/") String index(){ return "book name is :" + bookName + " and book author is: " + bookAuthor; } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
(三)类型安全的配置(基于properties)
通过@ConfigurationProperties将properties属性和一个Bean及其相关属性关联、从而实现类型安全的配置
(1)添加配置,在application.properties上添加:
author.name=wyf author.age=32
(2)类型安全的Bean,代码如下:
package com.vincent.demo.config; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @author rw * @date 2018/12/17 下午9:50 */ @Component //加载properties文件内的配置,通过prefix属性指定properties的配置的前缀,通过locations指定properties文件的位置 @ConfigurationProperties(prefix = "author") public class Author { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
(3)可以用@Autowired直接注入该配置
package com.vincent.demo; import com.vincent.demo.config.Author; import com.vincent.demo.config.HelloService; import com.vincent.demo.config.TestService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class DemoApplication { @Value("${book.author}") private String bookAuthor; @Value("${book.name}") private String bookName; @Autowired Author author; @RequestMapping("/") String index(){ return "book name is :" + bookName + " and book author is: " + bookAuthor + " author.name is:" + author.getName(); }public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
原文地址:https://www.cnblogs.com/vincentren/p/10226885.html
时间: 2024-10-11 09:01:19