SpringBoot(一)、入门及使用

一、为什么要用SpringBoot?  简化xml配置,提高开发效率。

二、SpringBoot 的作用:

1、创建独立的Spring 程序。

2、嵌入Tomcat、Jetty 或 Undertow,不需要部署war。

3、提供starter依赖以简化构建配置。

4、自动部署spring和第三方库。

5、提供生产就绪特性

6、无需xml配置

默认集成springMvc

三、第一个springboot程序

1、目录结构:

2、建立一个maven 工程,在pom.xml文件中引入spring-boot-starter-parent,和spring-boot-starter-web即可。

spring-boot-starter-parent是每个springboot项目必须有的类。

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.aeolusway</groupId>
  <artifactId>SpringBoot-1</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <parent>
  	<groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
  	<version>1.4.0.RELEASE</version>
  </parent>
  <dependencies>
  	<dependency>
  		<groupId>org.springframework.boot</groupId>
  		<artifactId>spring-boot-starter-web</artifactId>
  	</dependency>
  </dependencies>
</project>

2、User.java   实体类

package com.aeolusway.model;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

// 这个实体类只是为了从application.properties 中读取name,和age
// 如果不需要这些属性,这个类可有可无
/*
 *   @Component  注解  是吧这个bean 注入到容易中,如果没有这个注解但是又引用了这个类就会
 *   报没有这个bean 依赖
 */
@Component
public class User {

	@Value("${com.cn.name}")
	private String name;
	@Value("${com.cn.age}")
	private Integer age;
	
    //getter/setter
	
}

3、HelloSpringBoot.java

package com.aeolusway.controller;

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

import com.aeolusway.model.User;

/*
 * @RestController = @controller+@ResponseBody   也就是返回json格式的controller
 * 
 */
@RestController
public class HelloSpringBoot {

	@Autowired
	private User user;
	
	@RequestMapping("/getName")
	public String getName(){
		
		return "hello"+user.getName();
	}

    @RequestMapping("/hello")
	public String hello(){
		
		return "hello!!!";
	}
}

4、HelloSpringBootApp.java

package com.aeolusway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*
 * @SpringBootApplication  程序入口,启动项目,整合常用注解,扫包
 * @SpringBootApplication = @Configuration + @EnableAutoConfiguration + @ComponentScan。
 * 只能扫描同级包下的资源,也就是说一般情况下,其他的类都要放在这个注解所在的包之下(同级+子包)。
 * springboot的资源都是需要经过扫包的操作才能把资源加入到容器中,我们也可以采用@ComponentScan
 * 去定义自己需要扫描的包,如:@ComponentScan("com.aeolusway.controller")
 * 
 * 也就是说这里的@SpringBootApplication可以用这三个注解代替:
 * @Configuration
 * @EnableAutoConfiguration
 * @ComponentScan("com.aeolusway")  //这就是为什么@SpringBootApplication默认只能扫描当前包下 
 * 资源的原因。
 */
@SpringBootApplication
public class HelloSpringBootApp {

	public static void main(String[] args) {
		SpringApplication.run(HelloSpringBootApp.class, args);
	}
}

5、Application.properties

# 这个文件中可以配置服务地址,和tomcat的端口号,以及一些连接属性
# 如果没有这个文件,也不会影响springboot的正常运行,如果没有这个文件采用的都是默认属性。


# 测试读取属性文件中的属性
com.cn.name=SpringBoot
com.cn.age=100

# 可以不配置
# application.properties
# Server settings (ServerProperties)
server.port=8080
server.address=127.0.0.1
#server.sessionTimeout=30
# 请求路径参数
server.contextPath=/

# 可以不配置
# Tomcat specifics
#server.tomcat.accessLogEnabled=false
server.tomcat.protocolHeader=x-forwarded-proto
server.tomcat.remoteIpHeader=x-forwarded-for
server.tomcat.basedir=
server.tomcat.backgroundProcessorDelay=30

运行HelloSpringBootApp 这个类,然后浏览器发送请求:

http://127.0.0.1:8080/getName

四、静态资源访问

这里的静态资源指的是图片,js等。springboot的默认配置要求静态资源的目录必须满足特定的规则:

放在classpath下,目录命名必须是以下其中之一:

/static
/public
/resources
/META-INF/resources

如果需要访问这些静态资源,不需要加上这些包名,springboot默认定义到这些包的位置。

如,在classpath/static目录下又一张图片img.jpg,那么我们访问的路径就是:

http://localhost:8080/D.jpg

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值