pom.xml

	<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Run.java
package hello;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*; @SpringBootApplication
public class Run{
public static void main(String[] args) throws Exception {
SpringApplication.run(Run.class, args);
}
}
这个Run是一个单独的项目启动类,这里不应该有业务功能,上一篇的hello world业务代码应该写在一个单独的@Controller里面,和上一篇相比,这里用@SpringBootApplication替换了@EnableAutoConfiguration。
@SpringBootApplication
@EnableAutoConfiguration:只是实现自动配置一个功能,具体参考上一篇
@SpringBootApplication:是一个组合注解,包括@EnableAutoConfiguration及其他多个注解,是一个项目的启动注解
在eclipse的代码中 按 crtl+左键 点击@SpringBootApplication注解可以查看他的源码,如下
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication
前四个注解:是元注解,用来修饰当前注解,就像public类的修饰词,没有实际功能,如果不打算写自定义注解,不需要了解
后三个注解:是真正起作用的注解,包括
@SpringBootConfiguration:当前类是一个配置类,就像xml配置文件,而现在是用java配置文件,效果是一样的
@EnableAutoConfiguration:上篇已经讲了
@ComponentScan:用注解配置实现自动扫描,默认会扫描当前包和所有子包,和xml配置自动扫描效果一样,@Filter是排除了两个系统类

@SpringBootConfiguration和@Bean

package hello;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean; @SpringBootConfiguration
public class Config {
@Bean
public String hello(){
return "Hello World";
}
}

@SpringBootConfiguration:说明这是一个配置文件类,它会被@ComponentScan扫描到

@Bean:就是在spring容器中声明了一个bean,赋值为hello world,String方法类型就是bean的类型,hello方法名是bean的id

如果是用xml配置文件来声明bean,如下图

<bean id="hello" class="String"></bean>

SampleController.java

package hello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*; @Controller
public class SampleController {
@Autowired
String hello; @RequestMapping(value="/")
@ResponseBody
String test(){
return hello;
} }

把hello world业务功能独立出来,在这里注入了spring容器中的那个String类型的Bean,并且打印到页面

运行项目

现在的项目结构如下,共三个文件

通过Run.java的main方法启动项目,访问http://localhost:8080/

spring boot(2)-@SpringBootApplication详解的更多相关文章

  1. Spring Boot 自定义日志详解

    本节内容基于 Spring Boot 2.0. 你所需具备的基础 什么是 Spring Boot? Spring Boot 核心配置文件详解 Spring Boot 开启的 2 种方式 Spring ...

  2. Spring Boot 集成 FreeMarker 详解案例(十五)

    一.Springboot 那些事 SpringBoot 很方便的集成 FreeMarker ,DAO 数据库操作层依旧用的是 Mybatis,本文将会一步一步到来如何集成 FreeMarker 以及配 ...

  3. Spring Boot 之 Redis详解

    Redis是目前业界使用最广泛的内存数据存储. Redis支持丰富的数据结构,同时支持数据持久化. Redis还提供一些类数据库的特性,比如事务,HA,主从库. REmote DIctionary S ...

  4. Spring Boot 之 HelloWorld详解

    摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “以前是人放狗看家,现在是狗牵着人散步” — 随笔 一.Spring Boot 自述 世界上最好 ...

  5. Spring Boot启动流程详解(一)

    环境 本文基于Spring Boot版本1.3.3, 使用了spring-boot-starter-web. 配置完成后,编写了代码如下: @SpringBootApplication public ...

  6. spring boot容器启动详解

    目录 一.前言 二.容器启动 三.总结 =======正文分割线====== 一.前言 spring cloud大行其道的当下,如果不了解基本原理那么是很纠结的(看见的都是约定大于配置,但是原理呢?为 ...

  7. Spring Boot启动流程详解

    注:本文转自http://zhaox.github.io/java/2016/03/22/spring-boot-start-flow 环境 本文基于Spring Boot版本1.3.3, 使用了sp ...

  8. spring boot 面试题详解

    1.什么是springboot 用来简化spring应用的初始搭建以及开发过程 使用特定的方式来进行配置(properties或yml文件) 创建独立的spring引用程序 main方法运行 嵌入的T ...

  9. spring boot application.properties详解

    附上最新文档地址:https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-propertie ...

随机推荐

  1. service iptables xxx无效命令的情况下,如何启动/重启iptables

    最近在CentOS 7.6下使用service iptables xxx相关命令,提示如下错误:The service command supports only basic LSB actions ...

  2. servlet的url-pattern规则

    https://www.cnblogs.com/canger/p/6084846.html

  3. Windows Server 2008 R2 Enterprise 上用 SqlServer 2008 R2 创建发布出现异常

    标题: 新建发布向导------------------------------ SQL Server 无法将“AC”配置为分发服务器. ------------------------------其 ...

  4. 自定义 checkbox 样式

    前言:最近在做一个网站,为了统一风格,需要自定义checkbox的样式.所以花了点时间参考了 研究了一下.感觉上面的方法略微麻烦.所以自己重新写了下面的代码,欢迎大家指教.同时,感谢w3cplus提供 ...

  5. shiro学习笔记_0500_授权

    1,授权:给身份认证通过的人,授予他可以访问某些资源的权限. 2,权限粒度:分为粗粒度和细粒度. 粗粒度:例如对 user 的 crud,也就是通常所说的对表的操作. 细粒度:对表中记录的操作.如 只 ...

  6. 基于angularJS的表单验证练习

    今天看了一下angularJS的表单验证,看的有点云里雾里(也有可能是雾霾吸多了),于是做了一个小练习来巩固一下. html: <div ng-controller="Aaa" ...

  7. 031-CookieUtils 工具类模板

    模板一: package com.leo.common.utils; import java.io.UnsupportedEncodingException; import java.net.URLD ...

  8. CentOS6.4安装OpenSSL

    1.下载 wget https://www.openssl.org/source/openssl-1.0.2h.tar.gz 2.解压 tar zxf openssl-1.0.2h.tar.gz cd ...

  9. 断开所有远程连接(sql server)

    DECLARE @d VARCHAR(8000) SET @d = ' ' SELECT @d = @d + ' kill ' + CAST(spid AS VARCHAR) + CHAR(13)FR ...

  10. tomcat 控制台乱码问题

    环境:win7 + tomcat 8.5 问题描述:web端一切正常,不管是返回页面的数据还是控制台打印的日志,都没乱码,给app提供的api接口返回乱码,然后就去百度,像什么 在tomcat的ser ...