一个简单的入门Spring Boot项目

上学期学习了SpringMVC,结果过了几个月竟然忘的差不多了,为了防止学SpringBoot的时候再忘了,导致到时候又是东找资料西找资料的,写个博客记录一下。
有些注解的作用没有写,后续再补充…

1.首先创建SpringBoot项目
创建Spring Initializr项目,再选择Spring Web依赖启动器
2.配置仓库路径和版本
在这里插入图片描述

记得路径为自己的Maven仓库的路径。

3.创建Dao层
包中创建实体类User,属性有id,name和hobby(当然还可以添加其他的属性),

@Component 
@ConfigurationProperties(prefix = "user") 
public class User { 
	private String stdId; 
	private String name; 
	private String[] hobby; 
	
	public String getStdId() {
        return stdId;
    }

    public void setStdId(String stdId) {
        this.stdId = stdId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String[] getHobby() {
        return hobby;
    }
    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }
    @Override
    public String toString() {
        return "User{" +
                "stdId='" + stdId + '\'' +
                ", name='" + name + '\'' +
                ", hobby=" + Arrays.toString(hobby) +
                '}';
    }
 }

如果使用的是idea可以使用快捷键Alt+Enter来快速创建getter和setter及toString方法

4.在resources下的application.properties配置文件中添加信息

spring.main.allow-bean-definition-overriding=true
user.stdId=007
user.name=张三
user.hobby=study,play,eat,sleep

5.创建service层
在java中创建service包,包中创建UserService类

package com.example.service;
import com.example.domian.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {
    @Autowired
    private User user;//这里通过Autowired注解方式注入user

    public User getUser() {
        return user;
    }
}

6.配置service的自定义配置类
创建config包的MyConfig类

package com.example.config;

import com.example.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {
    @Bean
    public UserService userService(){
        return new UserService();
    }
}

7.创建Controller层
在java目录下创建controller包,包中创建UserController类

package com.example.controller;

import com.example.domian.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    public User getUser(){
        return userService.getUser();
    }
}

8.最后在test类添加测试代码

import com.example.controller.UserController;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Exp1ApplicationTests {
    @Autowired
    private UserController userController;
    @Test
    void contextLoads() {
        System.out.println(userController.getUser());
    }

}

运行结果:

第一次程序运行后发现出错了

The bean ‘userService’, defined in class path resource [com/example/config/MyConfig.class], could not be registered. A bean with that name has already been defined in file [D:\workspace\exp\exp1\target\classes\com\example\service\UserService.class] and overriding is disabled.

显示我的bean已经被定义了,可能是之前写过相同的
解决方法也很简单只要在application.properties中添加

spring.main.allow-bean-definition-overriding=true

就可以了。
但又发现输出的结果中name为自己电脑的主机名,不论怎么改都是显示自己的主机名。去查了一下发现好像是个小bug,要尽量避免在properties配置文件中使用user.name。只需把@ConfigurationProperties(prefix = “user”)中的user改个名字就可以正常了。

在这里插入图片描述
此外还可能出现一个问题,如果properties中有中文的话,输出来可能会乱码。因为idea 默认的properties文件是GBK,当有中文时,不同的客户端配置的编码不同时,可能产生中文乱码。
解决方法:修改properties的默认编码,统一为utf8。右边的Transparent native-to-ascii conversion 也要勾选!!!

File -> Settings -> File Encodings -> Default encoding for properties file -> UTF-8
在这里插入图片描述
(小细节:改完之后看看你的配置文件中有没有乱码,记得改成中文,不然有什么用呢)在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值