快速构建项目

第 1 步:将这个 Spring Boot 项目的打包方式设置为 war。

<packaging>war</packaging>

SpringBoot 默认有内嵌的 tomcat 模块,因此,我们要把这一部分排除掉。 即:我们在 spring-boot-starter-web  里面排除了 spring-boot-starter-tomcat ,但是我们为了在本机测试方便,我们还要引入它:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>log4j-over-slf4j</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency><dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

第 2 步:提供一个 SpringBootServletInitializer 子类,并覆盖它的 configure 方法。我们可以把应用的主类改为继承 SpringBootServletInitializer。或者另外写一个类。@SpringBootApplicationpublic class App extends SpringBootServletInitializer {

    @Override    protected SpringApplicationBuilder configure(            SpringApplicationBuilder application) {        return application.sources(App.class);    }

    public static void main(String[] args) {        SpringApplication.run(App.class, args);    }}注意:部署到 tomcat 以后,访问这个项目的时候,须要带上这个项目的 war 包名。另外,我们还可以使用 war 插件来定义打包以后的 war 包名称,以免 maven 为我们默认地起了一个带版本号的 war 包名称。例如:

<plugin>    <groupId>org.apache.maven.plugins</groupId>    <artifactId>maven-war-plugin</artifactId>    <configuration>        <warName>1024</warName>    </configuration></plugin>

例如:

http://localhost:8080/1024/login

其中,1024 是我放在 tomcat 上的 war 包名。

常用注解

(1)@SpringBootApplication             申明让spring boot自动给程序进行必要的配置,这个配置等同于:@Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置。

(2)@ResponseBody

该注解修饰的函数,会将结果直接填充到HTTP的响应体中,一般用于构建RESTful的api,该注解一般会配合@RequestMapping一起使用。

示例代码:

@RequestMapping("/test")

@ResponseBody

public String test(){

return"ok";

}

(3)@Controller

用于定义控制器类,在spring 项目中由控制器负责将用户发来的URL请求转发到对应的服务接口(service层),一般这个注解在类中,通常方法需要配合注解@RequestMapping。

示例代码:

@Controller

@RequestMapping("/demoInfo")

public class DemoController {

@Autowired

private DemoInfoService demoInfoService;

@RequestMapping("/hello")

public String hello(Map<String,Object> map){

System.out.println("DemoController.hello()");

map.put("hello","from TemplateController.helloHtml");       //会使用hello.html或者hello.ftl模板进行渲染显示.

return"/hello";

}

}

(4)@RestController

@ResponseBody和@Controller的合集

示例代码:

@RestController

@RequestMapping("/demoInfo2")

public class DemoController2 {

@RequestMapping("/test")

public String test(){

return"ok";

}

}

(5)@RequestMapping

提供路由信息,负责URL到Controller中的具体函数的映射。

(6)@EnableAutoConfiguration

Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring应用。例如,如果你的classpath下存在HSQLDB,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库”。你可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration注解的排除属性来禁用它们。

(7)@ComponentScan 

表示将该类自动发现(扫描)并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。如果没有配置的话,Spring Boot会扫描启动类所在包下以及子包下的使用了@Service,@Repository等注解的类。

(8)@Configuration

相当于传统的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过@Configuration类作为项目的配置主类——可以使用@ImportResource注解加载xml配置文件。

(9)@Import

用来导入其他配置类。

(10)@ImportResource

用来加载xml配置文件。

(11)@Autowired

自动导入依赖的bean

(12)@Service

一般用于修饰service层的组件

(13)@Repository

使用@Repository注解可以确保DAO或者repositories提供异常转译,这个注解修饰的DAO或者repositories类会被ComponetScan发现并配置,同时也不需要为它们提供XML配置项。

(14)@Bean

用@Bean标注方法等价于XML中配置的bean。

(15)@Value

注入Spring boot application.properties配置的属性的值。

示例代码: @Value(value = "#{message}")

private String message;

(16)@Qualifier

@Qualifier限定描述符除了能根据名字进行注入,但能进行更细粒度的控制如何选择候选者,具体使用方式如下:

@Autowired

@Qualifier(value = "demoInfoService")

private DemoInfoService demoInfoService;

(17)@Inject

等价于默认的@Autowired,只是没有required属性;



SpringBoot相关的更多相关文章

  1. Springboot 相关注解大全

    1.Spring注解 1.@Autowired 标注在方法,Spring容器创建当前对象,就会调用方法,完成赋值:方法使用的参数,自定义类型的值从ioc容器中获取自动装配; Spring利用依赖注入( ...

  2. SpringBoot相关的注解

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

  3. Spring和Springboot相关知识点整理

    简介 本文主要整理一些Spring & SpringBoot应用时和相关原理的知识点,对于源码不做没有深入的讲解. 1. 思维导图 右键新窗口打开可以放大. 说明 使用@Configurati ...

  4. springboot相关资料

    SpringBoot应用 rabbitmq先关资料: rabbitmq详解 springboot+rabbitmq整合示例程 RabbitMQ Exchange Queue RoutingKey Bi ...

  5. springboot相关链接

    springboot的三种启动方式 https://blog.csdn.net/my__Sun_/article/details/72866329 springboot学历历程 https://www ...

  6. springboot相关的pom依赖文件

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  7. 面试准备——springboot相关

    https://www.jianshu.com/p/63ad69c480fe https://blog.csdn.net/u013605060/article/details/80255192 htt ...

  8. Idea Spring 、SpringBoot相关设置技巧

    1.Spring变量依赖注入出现红色波浪线 Could not autowire. No beans of 'UserMapper' type found. less... (Ctrl+F1) Che ...

  9. SpringBoot相关错误

    1.org.springframework.boot.builder.SpringApplicationBuilder.<init>([Ljava/lang/Object;)V 搭建spr ...

随机推荐

  1. HDU4901 The Romantic Hero 计数DP

    2014多校4的1005 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4901 The Romantic Hero Time Limit: 6000/30 ...

  2. (JS高手不用看了!我只是在碎碎念,因为我也不知道面什么)JavaScript的算术运算

    Math.pow(2,53)    //2的51次幂 Math.round(0.6)    //四舍五入 Math.cell(0.6)      //向上求整 Math.floor(0.6)    / ...

  3. Entity Framework 关系约束配置

    前言 简单的说一下自己的理解,大家应该都很明白ADO.NET,也就是原生态的数据库操作,直接通过拼接SQL语句,表与表之间通过链接(inner join  left join  或者子查询),也就是在 ...

  4. Bootstrap速学教程之简要介绍

    Bootstrap是Twitter推出的一个用于前端开发的开源工具包,由Twitter的设计师Mark Otto和Jacob Thornton合作开发,是一个CSS/HTML框架,不用请UI设计师也能 ...

  5. Word Amalgamation(枚举 + 排序)

    Word Amalgamation Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 373  Solved: 247 Description In mil ...

  6. UVa 11210 - Chinese Mahjong

    解题报告:麻将的规则这里就不说了,这题我们可以用暴力的方法,所以我们应该这样枚举,即将34张牌的每一张牌都放到原来的十三张牌里面去,所以这时我们只要判断这十四张牌能不能胡,因为若要胡的话一定要有一个对 ...

  7. [Effective JavaScript 笔记]第35条:使用闭包存储私有数据

    js的对象系统并没有特别鼓励或强制信息隐藏.所有的属性名都是一个字符串,任意一个程序都可以简单地通过访问属性名来获取相应的对象属性.例如,for...in循环.ES5的Object.keys()和Ob ...

  8. Linux下使用fdisk扩展分区容量

    导读 我们管理的服务器可能会随着业务量的不断增长造成磁盘空间不足的情况,比如:共享文件服务器硬盘空间不足,在这个时候我们就需要增加磁盘空间,来满足线上的业务:又或者我们在使用linux的过程中, 有时 ...

  9. 变色龙安装程序 Chameleon Install 2.2 svn 2281发布

    变色龙安装程序 Chameleon Install 2.2 svn 2281发布 1.更好的支持10.9 Mavericks2.更新ATi.nVidia显卡支持列表3.添加新的 CPU Model I ...

  10. UIImage imageNamed和UIImage imageWithContentsOfFile区别

    UIImage imageNamed和 [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageNam ...