网上随便找的教程 这个教程介绍的有些高深 对初学者不太友好 但是基本明白这几点就可以:
代码写的很随意 是 《spring boot in action》书和 视频教程 的代码。 github代码地址
代码分为几个部分:
若要使用 dev 版的属性文件,则创建 application-dev.properties NOTE:不能改名字哦~,在 application.properties 文件中添加 spring.profiles.active=dev
// application.properties boot.name = melody boot.location = Beijing 复制代码
读取有两种方式: 1 使用注解 @Value , 2 使用注解 ConfigurationProperties
* **1 使用注解 @Value**
```
@Value("${boot.name}")
private String name;
@Value("${boot.location}")
private String location;
```
* **2 使用注解 ConfigurationProperties**
```
// ConfigInfo.java
package config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "boot")
public class ConfigInfo {
private String name;
private String location;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
// 使用
@Autowired
private ConfigInfo configInfo;
```
复制代码
错误处理读取自定义配置,ConfigurationProperties 遇到了2个问题,这里也记录下。
问题1: Configuration Annotation Proessor not found in classpath
引入依赖 spring-boot-configuration-processor
问题2: Consider defining a bean of type 'config.ConfigInfo' in your configuration
修改了 @SpringBootApplication 注解,指定 scanBasePackages 包含 config package。 原理就是 config package 不在 Application 所在package,组件自动扫描不会包含,所以需要显示指定。
readingList涉及JPA存储和前端展示thymeleaf,详细的技术木有看。。。先跑起来走通流程。 这里涉及了多个web的注解:
配置完依赖,运行报错了 blog.csdn.net/wolfking060… 是因为引入了JPA却没有相应的数据库依赖 H2数据库依赖引入 blog.csdn.net/ftdd_hw/art…
@RestController = @Controller + @ResponseBody 用于返回字符串json controller中的方法可以省略@ResponseBody
@RestController
public class MVCController {
@RequestMapping("/test/RestController")
public Object getUser() {
User user = new User();
user.setId(100);
user.setName("melody");
return user;
}
}
复制代码