Java Web系列:Spring Boot 基础

前端之家收集整理的这篇文章主要介绍了Java Web系列:Spring Boot 基础前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

项目(参考1) 提供了一个类似ASP.NET MVC的默认模板一样的标准样板,直接集成了一系列的组件并使用了默认的配置。使用Spring Boot 不会降低学习成本,甚至增加了学习成本,但显著降低了使用成本并提高了开发效率。如果没有Spring基础不建议直接上手。

1.基础项目

这里只关注基于Maven的项目构建,使用Spring Boot CLI命令行工具和Gradle构建方式请参考官网。

(1)创建项目:

创建类型为quickstart的Maven项目,删除默认生成的.java文件保持默认的Maven目录即可。

(2)修改/

4.0.0 com.example myproject 0.0.1-SNAPSHOT 1.8 org.springframework.boot spring-boot-starter-parent 1.3.1.RELEASE org.springframework.boot spring-boot-starter-web

添加/src/main/sample/controller/文件

org.springframework.web.bind.annotation.* @RequestMapping("/" "Hello World!" }

添加/src/main/sample/文件

org.springframework.boot.* org.springframework.boot.autoconfigure.* simple.controller.* main(String[] args) SpringApplication.run( Object[] { Application.,HomeController. }

2. 添加数据访问支持

(1)修改pom,添加依赖:

4.0.0 com.example myproject 0.0.1-SNAPSHOT 1.8 org.springframework.boot spring-boot-starter-parent 1.3.1.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa com.h2database h2 runtime

如果需要在控制台查看生成sql语句,可以添加/src/main/resources/application.properties

logging.level.org.hibernate.sql=debug

(2)添加实体

添加User、Role、Category和Post实体。

User:

Role:

Category:

Post:

(3)添加资源库

添加UserRepository、RoleRepository、CategoryRepository和PostRepository接口,无需实现。

UserRepository:

org.springframework.data.repository.* simple.domain.* UserRepository CrudRepository }

RoleRepository

org.springframework.data.repository.* simple.domain.* RoleRepository CrudRepository }

CategoryRepository

org.springframework.data.repository.* simple.domain.* CategoryRepository CrudRepository }

PostRepository

<span style="color: #0000ff;">import org.springframework.data.repository.*<span style="color: #000000;">;

<span style="color: #0000ff;">import simple.domain.*<span style="color: #000000;">;

<span style="color: #0000ff;">public <span style="color: #0000ff;">interface PostRepository <span style="color: #0000ff;">extends CrudRepository<User,Long><span style="color: #000000;"> {

}

(4)在控制器中注入资源库接口

org.springframework.beans.factory.annotation.* org.springframework.web.bind.annotation.* simple.repository.* .userRepository = .roleRepository = .categoryRepository = .postReppository = @RequestMapping("/" }

使用事务时在方法上应用注解@Transactional

3.添加验证和授权支持

(1)添加依赖

4.0.0 com.example myproject 0.0.1-SNAPSHOT 1.8 org.springframework.boot spring-boot-starter-parent 1.3.1.RELEASE org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-data-jpa com.h2database h2 runtime org.springframework.boot spring-boot-starter-security

(2)修改Application.java

org.springframework.boot.* org.springframework.boot.autoconfigure.* org.springframework.security.config.annotation.method.configuration.* simple.controller.* @EnableGlobalMethodSecurity(securedEnabled = ,prePostEnabled = main(String[] args) SpringApplication.run( Object[] { Application.,args); MyWebSecurityConfigurer configure(HttpSecurity http) http.authorizeRequests().antMatchers("/account**","/admin**" http.formLogin().usernameParameter("userName").passwordParameter("password").loginPage("/login" .loginProcessingUrl("/login").successHandler( .and().logout().logoutUrl("/logout").logoutSuccessUrl("/" http.rememberMe().rememberMeParameter("rememberMe" }

访问http://localhost:8080/account会自动跳转到login登录页。Spring Security的具体使用前文已有所述。

参考:

(1)https://github.com/spring-projects/spring-boot

(2)http://projects.spring.io/spring-boot/

(3)https://github.com/qibaoguang/Spring-Boot-Reference-Guide/blob/master/SUMMARY.md

猜你在找的Spring相关文章