最近公司使用Spring boot进行开发,稍微了解一下,不过自我感觉把集中式配置applicate.properties搞明白,注解用过Spring MVC的boot绝对没问题的

比如拦截器:@Aspect

public class ControllerAspect {

//对 com.store59.creditmall.controller包下面的所有类的所有带request对象的方法 进行拦截

@Before("execution(* com.store59.creditmall.controller.*.*(javax.servlet.http.HttpServletRequest, ..))")
public void proController(JoinPoint point) {

  Object[] args = point.getArgs();

  if (args.length > 0 && args[0] instanceof HttpServletRequest) {
  HttpServletRequest request = (HttpServletRequest) args[0];//获取request对象

  ...

  }

}

再比如@Controller,方法前加 @RequestMapping注释,可以设置web页面访问该方法的路径...

下面这篇文章写的不错,转载过来

转自http://my.oschina.net/u/1027043/blog/406558

Spring Boot提供了一个强大的一键式Spring的集成开发环境,能够单独进行一个Spring应用的开发,其中:

(1)集中式配置(application.properties)+注解,大大简化了开发流程 
(2)内嵌的Tomcat和Jetty容器,可直接打成jar包启动,无需提供Java war包以及繁琐的Web配置 
(3)提供了Spring各个插件的基于Maven的pom模板配置,开箱即用,便利无比。
(4)可以在任何你想自动化配置的地方,实现可能 
(5)提供更多的企业级开发特性,如何系统监控,健康诊断,权限控制 
(6) 无冗余代码生成和XML强制配置 
(7)提供支持强大的Restfult风格的编码,非常简洁

当然Spring Boot提供的功能,远远比上面的强大,散仙会在后续文章中,逐渐以实际工作中的项目为背景,穿插记录使用Spring Boot的心得体会。

下面看一个入门级的例子:

pom依赖: 

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.spring.boot</groupId>
  7. <artifactId>springboot</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>1.2.3.RELEASE</version>
  13. </parent>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.boot</groupId>
  17. <artifactId>spring-boot-starter-web</artifactId>
  18. </dependency>
  19. </dependencies>
  20. </project>

核心代码:

    1. package controller;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    4. import org.springframework.stereotype.Controller;
    5. import org.springframework.web.bind.annotation.RequestBody;
    6. import org.springframework.web.bind.annotation.RequestMapping;
    7. import org.springframework.web.bind.annotation.ResponseBody;
    8. /**
    9. * Created by 三劫散仙 on 2015/4/24.
    10. */
    11. @Controller
    12. @EnableAutoConfiguration
    13. public class HellowController {
    14. @RequestMapping("/hellow")
    15. @ResponseBody
    16. public String hellow(){
    17. return "哈喽,Spring Boot !";
    18. }
    19. public static void main(String[] args) {
    20. //第一个简单的应用,
    21. SpringApplication.run(HellowController.class,args);
    22. }
    23. }

好了,多说了点废话,下面看下本篇记录Spring Boot的几个知识点:

(一)一个Maven+Spring Boot项目基本的包结构形式 
(二)一个简单的在Spring Boot项目集成安全控制 
(二)如何在Spring Boot中记录log日志 
(四)Spring Boot中几个常用注解介绍

ok下面开始正题:

(一)先看下,官网给出的一个简单的包结构组成:

Java代码   
  1. com
  2. +- example
  3. +- myproject
  4. +- Application.java
  5. |
  6. +- domain
  7. |   +- Customer.java
  8. |   +- CustomerRepository.java
  9. |
  10. +- service
  11. |   +- CustomerService.java
  12. |
  13. +- web
  14. +- CustomerController.java

然后,看下散仙实际工作中的项目结构:

需要注意的地方如下: 
1,在src/main/java包下的第一层结构中,是必须放一个含有main方法的主启动的类,而且只能有一个main方法,如果再出现其他的main方法,在使用maven编译打包时,会报编译错误,当然在src/test/java包下,可以出现多个,但建议最好使用Junit进行单元测试.

这个main方法启动,就会启动内嵌的tomcat或jetty容器,然后加载所有需要加载的或扫描的类或资源文件。上篇博客中,散仙为了测试方便,是直接在当前的Conroller中,启动了一个测试服务,这样做适合单独调试,如果是生产环境下的启动方法,则是散仙前面所讲的,在java包的根目录下建立一个main方法类,负责启动所有的资源。

在本例中,散仙的main代码如下:

Java代码   
  1. package com.dhgate.rate;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.context.annotation.ComponentScan;
  5. import org.springframework.context.annotation.Configuration;
  6. @Configuration//配置控制
  7. @EnableAutoConfiguration//启用自动配置
  8. @ComponentScan//组件扫描
  9. public class ApplicationMain {
  10. public static void main(String[] args) throws Exception {
  11. //启动Spring Boot项目的唯一入口
  12. SpringApplication.run(ApplicationMain.class, args);
  13. }
  14. }

2,在src/main/resource目录下面,是放置一些配置文件,或模板支持的文件,如JSP,Velocity,Freemaker等,这里面比较常用或重要的一个文件,就是Spring Boot的集中式配置文件application.properties这个文件了,这里面给其他大部分的组件,提供了一个可集中管理和配置的中心,包括安全控制,redis,solr,mangodb的连接信息,以及数据库连接池,容器端口号,jmx,java mail,动态模板等。此外这个目录下默认是可以访问静态资源的,比如我们的css,js,或者第三方的一些引用文件等。

(二)关于在Spring Boot的配置简单的安全访问控制,这一点非常容器,我们只需要做如下二步,即可。 
1,在pom文件中,引入spring-boot-starter-security的maven依赖。

Xml代码   
  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-security</artifactId>
  4. </dependency>

2,在application.properties中,配置访问的用户名和密码

Java代码   
  1. #用户名 密码配置
  2. security.user.name=admin
  3. security.user.password=test

再次,访问链接时,会出现以下,拦截页面,就代表配置成功:

当然这只是,一个初级的配置,更复杂的配置,可以分不用角色,在控制范围上,能够拦截到方法级别的权限控制。

(三)日志的重要性,不言而喻,Spring Boot支持大部分的log配置,其中包括: 
(1)java util logging 
(2)log4j 
(3)log4j2 
(4)logbak 
默认的情况下spring boot会选择logback作为日志记录的载体,当然要想它正常的工作,需要依赖 Commons Logging, Java Util Logging, Log4J 或 SLF4J,相信大部分同学都是选择使用log4j.properties作为我们的日志配置和管理,但是散仙在Spring Boot中一直没有测试集成成功,所以就放弃使用log4j.properties作为日志载体,而是选择了Spring Boot推荐的logbak作为日志的配置文件,用过之后感觉也不错。

使用步骤:

1,将logbak.xml拷贝至resource目录下的根目录,然后在logbak.xml中,配置相关的log生成规则,log级别,以及日志路径,log的字符编码集,这个非常重要,因为刚开始用这个log记录程序运行的信息时,发现它不支持中文log,后来经查名,需要配置相关的log编码才可以正确记录对应的信息。一个通用的配置如下:

Xml代码   
  1. <!-- Logback configuration. See http://logback.qos.ch/manual/index.html -->
  2. <configuration scan="true" scanPeriod="10 seconds">
  3. <!-- Simple file output -->
  4. <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
  5. <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
  6. <encoder>
  7. <pattern>
  8. [ %-5level] [%date{yyyy-MM-dd HH:mm:ss}] %logger{96} [%line] - %msg%n
  9. </pattern>
  10. <charset>UTF-8</charset> <!-- 此处设置字符集 -->
  11. </encoder>
  12. <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
  13. <!-- rollover daily 配置日志所生成的目录以及生成文件名的规则 -->
  14. <fileNamePattern>logs/mylog-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
  15. <timeBasedFileNamingAndTriggeringPolicy
  16. class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
  17. <!-- or whenever the file size reaches 64 MB -->
  18. <maxFileSize>64 MB</maxFileSize>
  19. </timeBasedFileNamingAndTriggeringPolicy>
  20. </rollingPolicy>
  21. <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
  22. <level>DEBUG</level>
  23. </filter>
  24. <!-- Safely log to the same file from multiple JVMs. Degrades performance! -->
  25. <prudent>true</prudent>
  26. </appender>
  27. <!-- Console output -->
  28. <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  29. <!-- encoder defaults to ch.qos.logback.classic.encoder.PatternLayoutEncoder -->
  30. <encoder>
  31. <pattern>
  32. [ %-5level] [%date{yyyy-MM-dd HH:mm:ss}] %logger{96} [%line] - %msg%n
  33. </pattern>
  34. <charset>GBK</charset> <!-- 此处设置字符集 -->
  35. </encoder>
  36. <!-- Only log level WARN and above -->
  37. <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
  38. <level>WARN</level>
  39. </filter>
  40. </appender>
  41. <!-- Enable FILE and STDOUT appenders for all log messages.
  42. By default, only log at level INFO and above. -->
  43. <root level="INFO">
  44. <appender-ref ref="FILE" />
  45. <appender-ref ref="STDOUT" />
  46. </root>
  47. <!-- For loggers in the these namespaces, log at all levels. -->
  48. <logger name="pedestal" level="ALL" />
  49. <logger name="hammock-cafe" level="ALL" />
  50. <logger name="user" level="ALL" />
  51. </configuration>

2,在application.properties中,指定log文件的加载路径,已经配置通用的log日志级别:

Java代码   
  1. #指定log的配置文件,以及记录Spring Boot的log级别
  2. logging.config=logback.xml
  3. logging.level.org.springframework.web: INFO

(四)介绍下Spring Boot中几个常用的注解,其中大部分都是来自Spring MVC的注解,这些注解使用的频率一般都在90%之上。

(1)@RestController和@Controller指定一个类,作为控制器的注解 
(2)@RequestMapping方法级别的映射注解,这一个用过Spring MVC的小伙伴相信都很熟悉 
(3)@EnableAutoConfiguration和@SpringBootApplication是类级别的注解,根据maven依赖的jar来自动猜测完成正确的spring的对应配置,只要引入了spring-boot-starter-web的依赖,默认会自动配置Spring MVC和tomcat容器 
(4)@Configuration类级别的注解,一般这个注解,我们用来标识main方法所在的类 
(5)@ComponentScan类级别的注解,自动扫描加载所有的Spring组件包括Bean注入,一般用在main方法所在的类上 
(6)@ImportResource类级别注解,当我们必须使用一个xml的配置时,使用@ImportResource和@Configuration来标识这个文件资源的类。 
(7)@Autowired注解,一般结合@ComponentScan注解,来自动注入一个Service或Dao级别的Bean 
(8)@Component类级别注解,用来标识一个组件,比如我自定了一个filter,则需要此注解标识之后,Spring Boot才会正确识别。

ok,本篇的讲解,就到此结束,这些都是项目工程的基础知识,了解这些,有助于我们快速的上手一个Spring Boot应用。

下篇博客的内容会写: 
(1)如何在Spring Boot项目中集成Spring Loaded框架,来完成模块热加载和代码动态编译,有了这个东西,我们开发的效率会更加高效,大部分情况下我们改了一个类或方法的代码之后,都不需要重启应用,因为Spring Loaded会定时重新编译并加载代码。

(2)如何在Spring Boot项目中集成Velocity,Spring Boot支持多种模板引擎,包括 
1,FreeMarker 
2,Groovy 
3,Thymeleaf 
4,Velocity 
5,JSP (Spring Boot官方不推荐) 
我们可以根据熟悉程度来选择自己喜欢的引擎。

一键式Spring集成工具 Spring Boot的更多相关文章

  1. Redis篇之操作、lettuce客户端、Spring集成以及Spring Boot配置

    Redis篇之操作.lettuce客户端.Spring集成以及Spring Boot配置 目录 一.Redis简介 1.1 数据结构的操作 1.2 重要概念分析 二.Redis客户端 2.1 简介 2 ...

  2. Spring开发工具Spring Tools Suite(STS)

    The Spring Tool Suite is an Eclipse-based development environment that is customized for developing ...

  3. Activiti学习——Activiti与Spring集成

    转: Activiti学习——Activiti与Spring集成 与Spring集成 基础准备 目录结构 相关jar包 Activiti的相关jar包 Activiti依赖的相关jar包 Spring ...

  4. Spring 集成开发工具(STS)安装及配置

    安装 spring 集成开发工具,下载地址:https://spring.io/tools 下载后,解压,双击 STS ,运行. 如果提示: 去oracle的网站上下载了1.8版本的jdk,下载地址如 ...

  5. Spring集成web环境(使用封装好的工具)

    接上文spring集成web环境(手动实现) ##########代码接上文############# spring提供了一个监听器ContextLoaderListener对上述功能的封装,该监听器 ...

  6. Spring Cloud和Spring Boot的区别

    Spring MVC: Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 MVC ...

  7. Spring MVC和Spring Boot的理解以及比较

    Spring MVC是什么?(1)Spring MVC是Spring提供的一个强大而灵活的模块式web框架.通过Dispatcher Servlet, ModelAndView 和 View Reso ...

  8. spring、spring mvc与spring boot的区别是什么?

    Spring 的功能 Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等.但他们的基础都是Spring 的 ioc和 aop ioc 提供了依赖注入的容器 ao ...

  9. Spring集成RabbitMQ-使用RabbitMQ更方便

    如果提到Spring,你脑海中对他的印象还停留在SSH三大框架之一,那或许你该好好重新认识这个家伙. 在IT技术日新月异的今天,他还能让你忘不了并与他朝夕相处,他,肯定有自己的绝活.如今他早已经不是孤 ...

随机推荐

  1. 【iCore3 双核心板】例程三十二:UART_IAP_ARM实验——更新升级STM32

    实验指导书及代码包下载: http://pan.baidu.com/s/1hrnLJwC iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  2. HDU 1160 FatMouse's Speed(要记录路径的二维LIS)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  3. centos同步北京时间

    yum install ntp ntpdate #ntpdate -u 202.120.2.101 //写入硬件 #hwclock -w 以下是国内常见的NTP服务器 ntp.sjtu.edu.cn ...

  4. most queries (more than 90 percent) never hit the database at all but only touch the cache layer

    https://gigaom.com/2011/12/06/facebook-shares-some-secrets-on-making-mysql-scale/ Facebook shares so ...

  5. IIS应用程序池最大进程数设置

    1.当工作进程数>1时,如果有多个连接请求就会启动多个工作进程实例来处理,所启动的最多工作进程数就是你设置的最大进程数,后续更多的连接请求会循环的发送至不同的工作进程来处理.每个工作进程都能承担 ...

  6. Elasticsearch学习笔记(一)

    批量建索引: curl -s -XPOST 'localhost:9200/_bulk' --data-binary @documents.json 查看索引mappingmyindex/_mappi ...

  7. 并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法

    在Java多线程应用中,队列的使用率很高,多数生产消费模型的首选数据结构就是队列(先进先出).Java提供的线程安全的Queue可以分为阻塞队列和非阻塞队列,其中阻塞队列的典型例子是BlockingQ ...

  8. resx文件在X64位编译,提示“未能加载文件或程序集”的问题?

    原文:resx文件在X64位编译,提示"未能加载文件或程序集"的问题? resx文件在X64位编译,提示"未能加载文件或程序集"的问题? 解答: 错误现象如下 ...

  9. 动画 CABasicAnimation animationWithKeyPath 一些规定的值

    CABasicAnimation animationWithKeyPath Types When using the ‘CABasicAnimation’ from the QuartzCore Fr ...

  10. Git add 常见用法

        Git add   git add [参数]  [--] <路径> //作用就是将我们需要提交的代码从工作区添加到暂存区,就是告诉git系统,我们要提交哪些文件,之后就可以使用gi ...