SpringBoot入门介绍

**
Springboot:
不是一个新框架,在spring4的基础上通过注解实现spring的配置文件,springboot提供了state,简化了pom的依赖配置,解决了 maven版本依赖冲突的问题
Spring的核心:
独立运行的Spring项目,内嵌的Servlet项目,提供了state简化Maven配置
**
*Spring搭建环境:
在pom文件中引入:
1)在pom文件中继承spring boot的parent

org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE

<dependencies>
<!--添加热部署,项目中常见,方便我们修改代码时不停地重起tomcat-->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<optional>true</optional>
	</dependency>

2)导入spring boot的web支持 spring-boot-starter-web

org.springframework.boot
spring-boot-starter-web

	<!-- tomcat支持 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-tomcat</artifactId>
		<scope>provided</scope>
	</dependency>
	<dependency>
		<groupId>org.apache.tomcat.embed</groupId>
		<artifactId>tomcat-embed-jasper</artifactId>
		<scope>provided</scope>
	</dependency>
	<!-- jsp标签库,虽然springboo中包含了jst的jar,但不是很全面 -->
	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>jstl</artifactId>
	</dependency>
</dependencies>

<build>
	<plugins>
	<!--3)添加Spring boot的插件配置maven插件-->
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<configuration>
				<fork>true</fork>
			</configuration>
		</plugin>
	</plugins>
</build>

编写测试类

spring4 常用注解?
@Configuration //通过该注解来表明该类是一个Spring的配置,相当于一个xml文件
@Bean // 通过该注解来表明是一个Bean对象,相当于xml中的
@PropertySource(value= {“classpath:jdbc.properties”})
@Value("${jdbc.driver}")
@ImportResource(value = { “classpath:application-bean.xml” }) 引入spring的配置文件


@SpringBootApplication
@Controller(@RestController意思是controller里面的方法都已json格式输出,不用谢jsonkon的配置)
@Configuration()
public class helloApplication {	

	@RequestMapping("hello")
	@ResponseBody
	public String hello() {
		return "hello world";
	}	
	@RequestMapping("index")
	public String index() {
		return "/index";
	}
	public static void main(String[] args) {
		SpringApplication.run(helloApplication.class, args);
	}

}

5 springboot的配置文件两种方式
application.properties
application.yml

6 springboot的配置(自行选择)
1)设置请求的端口号,设置访问地址
server.port=9090
server.context-path=/boot1

2) 配置日志
#rizhi
	logging.file=D:/mylog/log.log
	logging.level.org.springframework.web = DEBUG
	(1)springboot配置试图解析器
		#视图
		  mvc:
			view:
			  prefix: /templates/
			  suffix: .html	  
		spring.mvc.view.prefix=/WEB-INF/views
		spring.mvc.view.suffix=.jsp

在src/main.resource下添加application.properties
server.port=9090//tomcat端口
server.context-path=/
#rizhi
logging.file=D:/mylog/log.log
logging.level.org.springframework.web = DEBUG
##文件jsp所在位置
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
、##启动主程序,浏览器127.0.0.1:9090/index,展示效果

猜你喜欢

转载自blog.csdn.net/qq_43049310/article/details/85174937
今日推荐