springboot 完整企业项目搭建实记

本文详细介绍如何使用 Spring Boot 搭建一个处理 HTTP/JSON 请求的基础 Web 应用,包括环境配置、添加核心依赖、创建 Controller 和 Service 层等步骤。

转载自:http://blog.csdn.net/u013187139/article/details/68944972

昨天搭建ssm框架时突然想到可以搭建springboot来完美解决配置复杂的问题,今天学习了一下springboot的搭建,在此记录一下搭建的过程和踩过的坑

这里给自己定一个该框架搭建完成的目标,如下 
框架要求功能: 
- 处理http/json 请求 
- 日志记录 
- 持久化 
- 数据源,事务控制 
- 定时任务 
- 视图模版

搭建环境: 
- 编译器:Eclipse 4.7.2 
- Maven : maven3.0 
- JDK: java8
- 系统: mac OS 10.13.3

- 数据库: mysql5.6

搭建记录 2018-3-12

1、新建maven项目

2、pom.xml中添加以下配置

<!-- Spring boot 父引用-->    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.4.0.RELEASE</version>    </parent>    <!-- Spring boot 核心web-->    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>    </dependencies>

3、新建Controller

@Controller@EnableAutoConfigurationpublic class TestBootController {    @RequestMapping("/")    @ResponseBody    String home() {    return "hello world";    }    public static void main(String[] args) throws Exception {    SpringApplication.run(TestBootController.class, args);    }}

端口默认是8080

4、在搭建了基础应用的基础上,想增加service层抽离控制层和业务层代码

//业务层接口
public interface TestInterFace {

    public int testInterFace();

    public User testUser();
}

//业务层接口实现
@Service
public class TestInterFaceImpl implements TestInterFace {
    @Override public int testInterFace() {
    return 0;
    }

    @Override public User testUser() {
    return new User();
    }
}


5、修改controller层代码

@Controller

@EnableAutoConfiguration

@ComponentScan(basePackages={"com.kx.springboot.service"})//添加的注解

public class TestBootController { @Autowired private TestInterFace testInterFace; @RequestMapping("/num") @ResponseBody int home() { int i = testInterFace.testInterFace(); return i; }  

@RequestMapping("/get") @ResponseBody User getUser() { return testInterFace.testUser(); } public static void main(String[] args) throws Exception { SpringApplication.run(TestBootController.class, args); }}

再次启动,成功,访问http://localhost:8088/get

6、这时候,又有一个问题,web应用都是多controller,这种启动方式一次只能启动一个controller,那么,怎么才能启动应用后访问多个controller,查阅springboot官方教程后,修改代码如下

@Controller@RequestMapping("test")public class TestBootController { @Autowired private TestInterFace testInterFace; @RequestMapping("/num") @ResponseBody int home() { int i = testInterFace.testInterFace(); return i; } @RequestMapping("/get") @ResponseBody User getUser() { return testInterFace.testUser(); }}

7、在包的最外层增加新的应用启动入口 —> Application

@EnableAutoConfiguration

@ComponentScan(basePackages={"com.kx.springboot"})

public class Application { public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); }}

到此,符合处理http/json 的web框架已经搭建ok了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值