1. 配置文件读取
@Value读取
@Value("${my.name}")
private String name;
my:
name: forezp
配置文件 赋给实体类 ConfigurationProperties
my:
name: forezp
age: 12
number: ${random.int}
uuid : ${random.uuid}
max: ${random.int(10)}
value: ${random.value}
greeting: hi,i'm ${my.name}
spring:
profiles:
active: dev
@ConfigurationProperties(prefix = "my")
@Component
public class ConfigBean {
private String name;
private int age;
private int number;
private String uuid;
private int max;
private String value;
private String greeting;
}
@RestController
@EnableConfigurationProperties({ConfigBean.class,User.class})
public class LucyController {
@Autowired
ConfigBean configBean;
@RequestMapping(value = "/lucy")
public String lucy(){
return configBean.getGreeting();
}
}
自定义配置文件的类型 @PropertySource
com.forezp.name=forezp
com.forezp.age=12
@Configuration
@PropertySource(value = "classpath:test.properties")
@ConfigurationProperties(prefix = "com.forezp")
public class User {
private String name;
private int age;
}
@RestController
@EnableConfigurationProperties({User.class})
public class HelloController {
@Autowired
User user;
@RequestMapping(value = "/user")
public String user() {
return user.getName() + user.getAge();
}
}
使用不同的配置文件启动
java -jar .\demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=test
application-test.yml
spring:
profiles:
active: dev
监控 actuator
pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-remote-shell</artifactId>
<version>1.5.19.RELEASE</version>
</dependency> #如果不行,就引入这个包,再次取消,也可用
配置
server:
port: 8082
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
shutdown:
enabled: true
server:
port: 9002
测试
health beans shutdown
1. http://localhost:9001/actuator/health 查看运行程序的健康状态
2. http://localhost:9001/actuator/beans 查看运行程序的Bean
3. 使用Actuator关闭应该程序
curl -X POST http://localhost:9001/actuator/shutdown (运行此命令必须设置:management.endpoint.shutdown.enabled = true)
Actuator 是Spring Boot 的一个非常重要的功能, Actuator 为开发人员提供了Spring Boot的运行状态信息,通过Actuator 可以查看程序的运行状态的信息
Actuator详细介绍可以参考: https://blog.csdn.net/WYA1993/article/details/80540981
{
"message": "Shutting down, bye..."
}
http://localhost:9002/actuator/mappings
health
http://localhost:9002/actuator/health
{
"status": "UP",
"details": {
"diskSpace": {
"status": "UP",
"details": {
"total": 302643146752,
"free": 118266159104,
"threshold": 10485760
}
}
}
}
beans
http://localhost:9002/actuator/beans
{
"contexts": {
"application": {
"beans": {
"endpointCachingOperationInvokerAdvisor": {
"aliases": [],
"scope": "singleton",
"type": "org.springframework.boot.actuate.endpoint.invoker.cache.CachingOperationInvokerAdvisor",
"resource": "class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/EndpointAutoConfiguration.class]",
"dependencies": [
"environment"
]
},
- scope
- singleton
- prototype
- request
- session
引入 remote-shell (已经引用)
boot整合jpa
引入依赖 web jpa mysql
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
配置数据源
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/spring_cloud?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT%2B8
username: root
password: 123456
jpa:
hibernate:
ddl-auto: create
show-sql: true
创建实例类 dao service controller
entiry
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String username;
@Column
private String password;
}
service 和 dao
@Service
public class UserService {
@Autowired
private UserDao userRepository;
public User findUserByName(String username){
return userRepository.findByUsername(username);
}
public List<User> findAll(){
return userRepository.findAll();
}
public User saveUser(User user){
return userRepository.save(user);
}
public Optional<User> findUserById(Long id){
return userRepository.findById(id);
}
public User updateUser(User user){
return userRepository.saveAndFlush(user);
}
public void deleteUser(Long id){
userRepository.deleteById(id);
}
}
public interface UserDao extends JpaRepository<User, Long> {
User findByUsername(String username);
}
action
@RequestMapping("/user")
@RestController
public class UserController {
@Autowired
UserService userService;
@ApiOperation(value="用户列表", notes="用户列表")
@RequestMapping(value={""}, method= RequestMethod.GET)
public List<User> getUsers() {
List<User> users = userService.findAll();
return users;
}
@ApiOperation(value="创建用户", notes="创建用户")
@RequestMapping(value="", method= RequestMethod.POST)
public User postUser(@RequestBody User user) {
return userService.saveUser(user);
}
@ApiOperation(value="更新信息", notes="根据url的id来指定更新用户信息")
@RequestMapping(value="/{id}", method= RequestMethod.PUT)
public User putUser(@PathVariable Long id, @RequestBody User user) {
User user1 = new User();
user1.setUsername(user.getUsername());
user1.setPassword(user.getPassword());
user1.setId(id);
return userService.updateUser(user1);
}
@ApiOperation(value="删除用户", notes="根据url的id来指定删除用户")
@RequestMapping(value="/{id}", method= RequestMethod.DELETE)
public String deleteUser(@PathVariable Long id) {
userService.deleteUser(id);
return "success";
}
@ApiIgnore
@RequestMapping(value = "/hi", method = RequestMethod.GET)
public String jsonTest() {
return " hi you!";
}
@GetMapping("/username/{username}")
public User getUser(@PathVariable("username")String username){
return userService.findUserByName(username);
}
}