SpringBoot 快速入门


SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,

从而使开发人员不再需要定义样板化的配置。通过这种方式,Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。


Spring boot的特点

1. 创建独立的Spring应用程序
2. 嵌入的Tomcat,无需部署WAR文件
3. 简化Maven配置
4. 自动配置Spring
5. 提供生产就绪型功能,如指标,健康检查和外部配置
6. 绝对没有代码生成和对XML没有要求配置


Spring boot的优点

spring boot 可以支持你快速的开发出 restful 风格的微服务架构,自动化确实方便,做微服务再合适不过了,单一jar包部署和管理都非常方便。

只要系统架构设计合理,大型项目也能用,加上nginx负载均衡,轻松实现横向扩展,spring boot 要解决的问题, 精简配置是一方面,

另外一方面是如何方便的让spring生态圈和其他工具链整合(比如Redis, email, elasticsearch)


搭建:

创建一个Maven项目,在maven的pom.xml贴上依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.6.RELEASE</version>
	</parent>
<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.comilper.source>1.8</maven.comilper.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

创建一个java类:

package com.gcx.spring.SpringBoot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * Hello world!
 *
 */
@SpringBootApplication
public class App {
	
	@Bean
	public Runnable createRunnable(){
		return () -> {System.out.println("SpringBoot is run");};
	}
	
    public static void main( String[] args ){
    	ConfigurableApplicationContext context=SpringApplication.run(App.class, args);
    	context.getBean(Runnable.class).run();
    }

}

@SpringBootApplication注解解释:

@Configuration,@EnableAutoConfiguration,@ComponentScan 这些注解一般都是一起使用,

spring boot提供了一个统一的注解@SpringBootApplication。

@SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan。


@Configuration注解解释:

@Configuration:提到@Configuration就要提到他的搭档@Bean。使用这两个注解就可以创建一个简单的spring配置类,

可以用来替代相应的xml配置文件

@Configuration的注解类标识这个类可以使用Spring IOC容器作为bean定义的来源。@Bean注解告诉Spring,

一个带有@Bean的注解方法将返回一个对象,该对象应该被注册为在Spring应用程序上下文中的bean


@EnableAutoConfiguration注解解释:

@EnableAutoConfiguration:能够自动配置spring的上下文,试图猜测和配置你想要的bean类,

通常会自动根据你的类路径和你的bean定义自动配置


@ComponentScan注解解释:

@ComponentScan:会自动扫描指定包下的全部标有@Component的类,并注册成bean,

当然包括@Component下的子注解@Service,@Repository,@Controller


运行结果:



如果我们不想继承一个父module可以用这种方式 :修改pom.xml 删除原来parent,加上

<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-dependencies</artifactId>
				<version>1.5.6.RELEASE</version>
				<scope>import</scope>
				<type>pom</type>
			</dependency>
		</dependencies>
	</dependencyManagement>
同样达到同样的输出结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值