码迷,mamicode.com
首页 > 编程语言 > 详细

第二章 第二个spring-boot程序

时间:2017-07-13 13:30:02      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:domain   alt   pass   运行   locate   ges   分享   classes   info   

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

1、代码结构如下

技术分享

2、pom.xml

技术分享
<?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/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xxx</groupId>
    <artifactId>myboot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version><!-- 官方推荐 -->
    </properties>
    <!-- 引入spring-boot-starter-parent做parent是最好的方式, 
         但是有时我们可能要引入我们自己的parent,此时解决方式有两种: 
         1)我们自己的parent的pom.xml的parent设为spring-boot-starter-parent(没有做过验证,但是感觉可行) 
         2)使用springboot文档中的方式:见spring-boot-1.2.5-reference.pdf的第13页 
    -->
    <parent> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-parent</artifactId> 
        <version>1.2.5.RELEASE</version> 
    </parent>

    <!-- <dependencyManagement>
        <dependencies>
            <dependency>
                Import dependency management from Spring Boot
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>1.2.5.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement> -->

    <!-- 引入实际依赖 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 用于将应用打成可直接运行的jar(该jar就是用于生产环境中的jar) 值得注意的是,如果没有引用spring-boot-starter-parent做parent, 
                且采用了上述的第二种方式,这里也要做出相应的改动 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
View Code

说明:pom.xml文件与上一节的完全一样。

 

3、Application.java

技术分享
package com.xxx.firstboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @EnableAutoConfiguration:spring boot的注解,一般只用于主类,
 * 是无xml配置启动的关键部分,明确指定了扫描包的路径为其修饰的主类的包(这也就是为什么主类要放在根包路径下的原因)
 * 
 * @ComponentScan 进行包的扫描,扫描路径由@EnableAutoConfiguration指定了
 * 
 * 主类要位于根包路径下,方便之后的扫描(We generally recommend that you locate your main application class in a root package above other classes.)
 */
@SpringBootApplication        //same as @Configuration+@EnableAutoConfiguration+@ComponentScan
public class Application {
    /**
     * spring boot的入口,在整个子项目在内,
     * 只能有一个main方法,否则spring boot启动不起来
     */
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}
View Code

注意:

  • 主类要位于根包路径下(例如,com.xxx.firstboot),这是推荐做法,方便扫描
  • 每一个jar(即每一个子项目)都要有一个主方法,用于启动该jar(也就是一个微服务)
  • 在主类上添加注解@SpringBootApplication,该注解相当于添加了如下三个注解
    • @Configuration:该注解指明该类由spring容器管理
    • @EnableAutoConfiguration:该注解是无xml配置启动的关键部分
    • @ComponentScan:该注解指定扫描包(如果主类不是位于根路径下,这里需要指定扫描路径),类似于spring的包扫描注解

 

4、application.properties

技术分享
1 #user info
2 user.id=1
3 user.username=zhaojigang
4 user.password=123
View Code

注意:

  • application.properties文件是spring-boot的默认文件,一般各种配置(包括:数据源配置,httpclient配置等)都配在这里就好

 

5、User.java

技术分享
package com.xxx.firstboot.domain;

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

/**
 * @ConfigurationProperties(prefix="user")
 * 自动读取application.properties(是spring-boot默认查找的文件)文件中的user.*的属性
 * 在没有使用@ConfigurationProperties的情况下,可以使用@Value("${user.id}")来一个个指定属性的值
 * 
 * 注意:如果要使用@ConfigurationProperties和@Value,需要将该bean添加@Component,
 * 因为在后边的对该类的使用中,需要直接将该类使用@Autowire注解注入,这样这些属性的自动注入才起作用,
 * 具体使用查看"UserService"
 */
@Component
@ConfigurationProperties(prefix="user")
public class User {
    
    //@Value("${user.id}")
    private int id;
    
    //@Value("wangna")
    private String username;
    
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}
View Code

注意:

  • 该类就是一个普通的model,在ssm框架中我们并没有将这样的model归给spring容器去管理,在这里使用@Component注解将其交由spring容器去处理,这样在之后的使用中,就可以直接将该model注入到其使用类中。
  • 在该类上添加了@ConfigurationProperties(prefix="user")注解,这样的意思就是可以自动扫描application.properties文件相关前缀的配置,并根据名称配置到该类的每一个属性上去
  • 也可以在属性上使用@Value注解单独复值,当然前提是没有配置@ConfigurationProperties,如果配置了,@Value注解失效

 

6、UserService.java

技术分享
package com.xxx.firstboot.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xxx.firstboot.domain.User;

@Service
public class UserService {
    
    @Autowired
    private User user;
    
    public User getUser(){
        return user;
    }

}
View Code

注意:

  • 这里直接注入了User,这和类正是上边的那个model

 

7、UserController.java

技术分享
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相关的依赖包(可以根据需求自己引入):

技术分享

技术分享

技术分享

技术分享

技术分享

第二章 第二个spring-boot程序

标签:domain   alt   pass   运行   locate   ges   分享   classes   info   

原文地址:http://www.cnblogs.com/sunny3096/p/7159811.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!