您的位置:首页 > 编程语言 > Java开发

第二章 第二个spring-boot程序

2016-03-30 11:15 387 查看
上一节的代码是spring-boot的入门程序,也是官方文档上的一个程序。这一节会引入spring-boot官方文档推荐的方式来开发代码,并引入我们在spring开发中service层等的调用。

1、代码结构如下

package com.xxx.firstboot.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xxx.firstboot.domain.User;
import com.xxx.firstboot.service.UserService;
/**
* @RestController:spring mvc的注解,
* 相当于@Controller与@ResponseBody的合体,可以直接返回json
*/
@RestController
@RequestMapping("/user")
public class UserController {

@Autowired
private UserService userService;

@RequestMapping("/getUser")
public User getUser() {
return userService.getUser();
}

}


View Code

说明:

这个类其实就是开发中,开发一个spring-boot程序的最基本最常用的方式。(在微服务应用中,用到类似于"Java企业应用开发实践"系列中的父子模块开发,之后再说)

相对于ssm而言,spring-boot的读取属性文件的方式也相当容易,读取属性文件常用的三种方式

使用FileUtil去读:见第一章 属性文件操作工具类

使用如上的注解实现(最推荐的方式)

使用Environment这个类来获取就行(这个可能写错类名了)

对于spring-boot而言,其本身有很多集成的jar包(见下边),我们可以根据自己的需求引入相应的jar,但是暂无与mybatis集成的jar。

spring-boot相关的依赖包(可以根据需求自己引入):









内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: