spring boot的小例子,Java两年工作经验面试题

时间:2021-09-20
本文章向大家介绍spring boot的小例子,Java两年工作经验面试题,主要包括spring boot的小例子,Java两年工作经验面试题使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

server.tomcat.uri-encoding=UTF-8

spring.http.encoding.charset=UTF-8

spring.http.encoding.enabled=true

spring.http.encoding.force=true

spring.messages.encoding=UTF-8

然后 在IntelliJ IDEA中依次点击ItelliJ idea->Preference -> Editor -> File Encodings

将Properties Files (*.properties)下的Default encoding for properties files设置为UTF-8,将Transparent native-to-ascii conversion前的勾选上。(参考【Springboot 之 解决IDEA读取properties配置文件的中文乱码问题】【Springboot 之 解决IDEA读取properties配置文件的中文乱码问题】)。

然后在变量中通过@Value直接注入就行了,如下:


 @Value(value = "${book.author}")

    private String bookAuthor;

    @Value("${book.name}")

    private String bookName;

    @Value("${book.pinyin}")

    private String bookPinYin;

修改index方法,使之返回这些值:


@RequestMapping(value = "/",produces = "text/plain;charset=UTF-8")

    String index(){

        return "Hello Spring Boot! The BookName is "+bookName+";and Book Author is "+bookAuthor+";and Book PinYin is "+bookPinYin;

    }

然后在浏览器中输入,很简单吧

类型安全的配置


刚刚说的这种方式我们在实际项目中使用的时候工作量略大,因为每个项目要注入的变量的值太多了,这种时候我们可以使用基于类型安全的配置方式,就是将properties属性和一个Bean关联在一起,这样使用起来会更加方便。我么来看看这种方式怎么实现。

1.在src/main/resources文件夹下创建文件book.properties

文件内容如下


book.name=红楼梦

book.author=曹雪芹

book.price=28

2.创建Book Bean,并注入properties文件中的值


package org.sang.test19springboot3;



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

import org.springframework.context.annotation.PropertySource;

import org.springframework.stereotype.Component;



@Component

@ConfigurationProperties(prefix = "book")//prefix是前缀

@PropertySource("classpath:book.properties")//指定注入的文件是哪个。

public class BookBean {

    private String name;

    private String author;

    private String price;



    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 String getPrice() {

        return price;

    }



    public void setPrice(String price) {

        this.price = price;

    }

}

3.添加路径映射

在Controller中添加如下代码注入Bean:


@Autowired

    private BookBean bookBean;

添加路径映射


@RequestMapping("/book")

    public String book() {

        return "Hello Spring Boot! The BookName is "+bookBean.getName()+";and Book Author is "+bookBean.getAuthor()+";and Book price is "+bookBean.getPrice();

    } 

输入浏览器可看到结果。

日志配置

默认情况下Spring Boot使用Logback作为日志框架,也就是我们前面几篇博客中用到的打印日志方式,当然如果有需要我们可以手动配置日志级别以及日志输出位置,相比于我们在Spring容器中写的日志输出代码,这里的配置简直就是小儿科了,只需要在application.properties中添加如下代码:

最后

每年转战互联网行业的人很多,说白了也是冲着高薪去的,不管你是即将步入这个行业还是想转行,学习是必不可少的。作为一个Java开发,学习成了日常生活的一部分,不学习你就会被这个行业淘汰,这也是这个行业残酷的现实。

如果你对Java感兴趣,想要转行改变自己,那就要趁着机遇行动起来。或许,这份限量版的Java零基础宝典能够对你有所帮助。

CodeChina开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频】

原文地址:https://www.cnblogs.com/dhsfdhfhgufdu/p/15313869.html