首页 > 其他分享 >spring-boot配置属性注入到Bean

spring-boot配置属性注入到Bean

时间:2022-10-12 14:33:22浏览次数:42  
标签:http encoding spring boot springframework Bean org


1.在属性文件中配置book实体 

spring-boot配置属性注入到Bean_bean

2.创建配置属性对应实体类与控制器 

spring-boot配置属性注入到Bean_tomcat_02

3.输出实体时乱码

spring-boot配置属性注入到Bean_bean_03

在属性配置文件中加入

spring.http.encoding.force=true

spring.http.encoding.charset=UTF-8

spring.http.encoding.enabled=true

server.tomcat.uri-encoding=UTF-8

可解决乱码问题

spring-boot配置属性注入到Bean_乱码_04

指定单个属性文件乱码时在要该属性文件的中加入: 

spring.http.encoding.force=true

spring.http.encoding.charset=UTF-8

spring.http.encoding.enabled=true

spring-boot配置属性注入到Bean_乱码_05

这里指定的属性文件为resources目录下的newbook.properties文件 

spring-boot配置属性注入到Bean_bean_06

其实application.properties与application.yaml配置文件是可以共存的,properties文件的优先级高来yaml

下图为同时使用properties与yaml

spring-boot配置属性注入到Bean_spring_07

实体类代码: 

package org.sang.model;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "book")
public class Book {
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public Float getPrice() {
return price;
}

public void setPrice(Float price) {
this.price = price;
}

private String name;
private String author;
private Float price;

@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}

配置代码:

server.port=8888
server.errorpath=/error
server.servlet.session.timeout=30
server.tomcat.uri-encoding=utf-8
server.tomcat.max_threads=500
server.tomcat.basedir=/log
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.ssl.key-store=metrox.p12
server.ssl.key-alias=tomcathttps
server.ssl.key-store-password=123456
book.name=三国演义
book.author=罗贯中
book.price=38

控制器代码

package org.sang.controller;

import org.sang.model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class BookController {
@Autowired
Book book;
@GetMapping("/book")
public String book(){
return book.toString();
}
}

pom配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.sang</groupId>
<artifactId>chapter02</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.source.Encoding>UTF-8</project.build.source.Encoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.4.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

 

标签:http,encoding,spring,boot,springframework,Bean,org
From: https://blog.51cto.com/u_15826192/5750154

相关文章

  • 【云原生】Spring Cloud Alibaba 之 Feign 远程调用 实战
    文章目录​​一、什么是远程调用?​​​​⛅远程调用的原理​​​​二、RestTemplate与Feign的区别​​​​三、Feign远程调用实战开发​​​​⏳Feign替代RestTempla......
  • Nebula Graph介绍和SpringBoot环境连接和查询
    NebulaGraph介绍和SpringBoot环境连接和查询转载请注明来源https://www.cnblogs.com/milton/p/16784098.html说明当前NebulaGraph的最新版本是3.2.1,根据官方的文档......
  • SpringBoot的yml多环境配置3种方法
    方式一:多个yml文件 步骤一、创建多个配置文件application.yml#主配置文件application-dev.yml#开发环境的配置application-prod.yml#生产环境的配置applic......
  • Spring Boot 2.3.x 升级至 2.4.x 遇到的那些事
    随着SpringBoot3.0需要Java17和SpringFramework6作为最低版本。计划逐步升级系统的SrpingBoot版本,以应对未来的趋势,当前系统SpringBoot版本是2.3.12,继续先升即到......
  • springbean的八种加载方式
    总结了一下八种bean的加载方式bold;">接口 xml+<bean/> xml:context+注解(@Component+4个@Bean) 配置类+扫描+注解(@Component+4个@Bean)@Bean定义FactoryBean接口......
  • SpringBoot_RestFul风格CURD
    一、什么是RestFulREST(英文:RepresentationalStateTransfer,简称REST,意思:表述性状态转换,描述了一个架构样式的网络系统,比如web应用),是一种软件架构风格不是标准哦!一种软......
  • SpringBoot笔记
    Idea环境创建项目:   ......
  • 这些不知道,别说你熟悉 Spring
    大家好,这篇文章跟大家来聊下Spring中提供的常用扩展点、SpringSPI机制、以及SpringBoot自动装配原理,重点介绍下Spring基于这些扩展点怎么跟配置中心(Apollo、Nacos、......
  • SpringBoot+MyBatis Plus对Map中Date格式转换的处理
    在SpringBoot项目中,如何统一JSON格式化中的日期格式问题现在的关系型数据库例如PostgreSQL/MySQL,都已经对JSON类型提供相当丰富的功能,项目中对于不需要检索但是......
  • 【SpringCloud】(一)分布式理论
    分布式架构理论方法远程调用各个模块运行于不同的tomcat,模块之间通过网络进行调用。远程调用的技术演进1WebService解决应用程序之间的跨平台访问问题,基于SOAP/WSDL......