Spring Boot 学习笔记(一)—— 入门示例

新建一个spring boot工程

1. 使用eclipse创建一个maven jar工程,在pom中引入spring boot的依赖

<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>cn.fg</groupId>
	<artifactId>spring-boot-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.22.RELEASE</version>
	</parent>
	
	<properties>
		<!-- 重写spring-boot-starter-parent里面的属性,spring boot 建议使用jdk1.8 -->
		<java.version>1.8</java.version> 
	</properties>

	<dependencies>
		<!-- starter:场景启动器,这里我们引入的是web场景,点进去,里面引入了web相关的jar包,jar版本被统一的管理起来 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<!-- 这个插件,可以将应用打包成一个可执行的jar包 -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

2. 创建spring boot 主程序类

package cn.fg;

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

//来标注一个主程序类,说明这是一个Spring Boot应用,该类所在包以及子包下的带有spring注解的类都将被自动扫描到ioc容器中
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SpringApplication.run(DemoApplication.class, args);
	}

}

3.编写一个controller,注意要放在spring boot主程序类所在包及子包下才能被自动扫描到ioc容器中

package cn.fg.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//spring boot 适合前后端项目,不在进行jsp页面跳转,所以这里我们全部使用@RestController
//@RestController是@Controller和@ResponseBody组合注解
@RestController
public class MainController {

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

请求地址:http://127.0.0.1:8080/hello?name=中国,get请求居然能够识别中文,可能是嵌入的Tomcat被修改了url编码

为什么我没有做任何配置,Controller就可以访问了?

1. 首先我们点击@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 {

}

@ComponentScan注解已经在为我们扫描了,没有设置basePackages的值,默认就是扫当前类所在包及子包

2. 那么为什么没有开启springmvc的注解驱动,就可以访问页面了呢?继续点击@EnableAutoConfiguration

@SuppressWarnings("deprecation")
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

}

3. @Import(EnableAutoConfigurationImportSelector.class) 导入自动配置的组件,哪些组件被导入了?

点进去阅读里面的代码可以发现,Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作;在spring-boot-autoconfigure-1.5.22.RELEASE.jar包中你可以找到spring.factories,它会被当做properties文件被springboot加载

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration,\
org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration,\
org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration,\
org.springframework.boot.autoconfigure.cloud.CloudAutoConfiguration,\

.....

静态资源(js、image、css等)与html页面

1. 由于spring boot是以jar包内置的tomcat启动web程序,没有webapp目录。所有的静态资源都是放在resources/static/目录下的。例如resources/static/1.txt,那么访问地址就是http://192.168.1.100:8080/xxx/1.txt(xxx是contextpath)

2. 官方不推荐spring boot使用jsp,最佳用途仅用作数据输出,也就是前后端分离,前端使用react或vue,后端使用springboot。如果要在springboot中使用页面渲染,那么官方推荐使用模板引擎,例如freemarker、thymeleaf(官方推荐这个),在resources/static/templates/目录下,用来存放页面

全局配置文件 

在入门例子中,tomcat的端口号和contextpath,默认就给我们设置好了,如果想要修改这些,要怎么做?在resources路径下新建application.properties或application.yml

#更改端口号
server.port=8088
#上下文路径,修改后请求变为http://127.0.0.1:8080/demo/hello?name=abc
server.context-path=/demo

因为启用了自动配置,所以有些值默认就给我们设置好了,可以修改哪些值?https://docs.spring.io/spring-boot/docs/1.5.x/reference/htmlsingle/#common-application-properties

单元测试

package cn.fg.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import cn.fg.controller.MainController;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MySpringBootTest {
	
	@Autowired
	MainController mainController;
	
	@Test
	public void test01(){
		System.out.println(mainController.hello("你好,中国"));
	}
}

继续使用xml配置文件

	<!-- spring.xml -->
	<bean class="cn.fg.bean.Cat">
		<property name="name" value="tom" />
	</bean>
//使用@ImportResource引入xml配置,该注解要在ioc容器中使用才有效
//该注解也可以放在主程序类与@SpringBootApplication一起使用
@ImportResource("classpath:spring.xml")   
@Configuration
public class MainConfig {
	

}

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值