Springboot可以说是当前最火的java框架了,非常适合于"微服务"思路的开发,大幅缩短软件开发周期。

  • 概念

过去Spring充满了配置bean的xml文件,随着spring2.x和JDK1.5带来注解,配置大量减少,到了目前的Spring4.x和Spring Boot,更是推荐java配置的方式(这点和.NET很相似)。

总体来说,Spring框架是轻量级企业开发一个完整解决方案,通过Ioc容器来管理pojo对象,其具有模块化的特点,基本的模块如下:

核心容器:Spring-Core核心工具类,Spring-Beans定义Bean的支持,Spring-Context运行时Spring容器,Spring-Context-Support容器对第三包的支持,Spring-Expression使用表达式语言在运行时查询和操作对象。

AOP:Spring-AOP基于代理的AOP支持,基于AspectJ的AOP支持。

消息:Spring-Messaging对消息架构的和协议的支持。

Web:Spring-Web提供基础的Web集成功能,在Web项目中提供Spring容器,Spring-Webmvc提供基于Servlet的SpringMVC,Spring-WebSocket提供WebSocket功能,Spring-Webmvc-Portlet提供Portlet环境支持(?)。

数据访问:Spring-JDBC提供JDBC访问数据库的支持,Spring-TX提供编程式和声明式的事务支持,Spring-ORM提供对象/关系映射技术支持,Spring-OXM提供对象/xml映射技术支持,Spring-JMS提供JMS支持。

以上介绍的是Spring项目,实际到目前为止,Spring已经形成了完整的生态,有很多相关的项目,包括:Spring Boot使用默认开发配置快速开发;Spring XD简化大数据应用开发;Spring Cloud为分布式系统提供工具集;Spring Data对数据库的支持;Spring Integration通过消息机制对企业集成模式(EIP)的支持;Spring Batch简化批处理操作;Spring Security通过认证和授权保护应用;Spring HATEOAS简化REST服务开发;Spring Social与社交API的集成;Spring AMQP对基于AMQP消息的支持;Spring Mobile可以根据不同设备返回不同页面;Spring for Android提供在Android上消费RESTfulAPI的功能;Spring Web Flow基于Spring MVC提供基于想到流程式的Web应用开发;Spring Web Services提供基于协议的SOAP/Web服务;Spring LDAP简化LDAP开发;Spring Session提供一个API和实现来管理用户会话信息。

Tip:Spring框架4大原则:使用POJO进行轻量级&最小侵入开发;通过依赖注入和基于接口编程实现松耦合;通过AOP和默认习惯进行声明式编程;通过AOP和模板减少模式化代码。

  • 实践

可以通过项目构建工具(Maven, Gradle, Ant)快速构建Spring项目,在Eclipse中可以使用Spring Tool Suite工具快速搭建项目,当然使用付费的IDEA(仍不太熟悉)更简便。

在Spring中,常见的注解包括:@Component组件,@Service用于业务逻辑层,@Repository用于数据访问层,@Controller用于展示层,@Autowired注入bean。

Java配置@Configuration表示当前类是一个配置类,相当于xml文件;@Bean注解用在方法上,声明当前方法的返回值为一个Bean,这部分需要好好理解。

AOP:spring支持AspectJ的注解式切面编程,@Aspect表示切面,@After,@Before,@Around定义建言Advice,其中以上注解参数的拦截规则为切点Pointcut,为了切点复用,可使用@PointCut专门定义切点,其中符合条件的每一个被拦截处为连接点JointPoint。在spring中@Transactional,@Cachable就是一个很好的aop的示例,此外使用aop需要的依赖包括spring-aop, aspectjrt, aspectjweaver。

Bean的Scope:默认为Singleton单例;Prototype每次调用新建一个Bean实例,只需要在class上增加@Scope("prototype")注解;Request,Web项目中,给每个请求建立一个bean实例;Session给每个http Session建立一个Bean;GlobalSession,在portal应用中使用,为全局http会话新建一个示例。

Spring EL: Spring通过@Value注解注入各种资源,包括注入普通字符串、操作系统属性、表达式运算结果、其他Bean属性、文件内容、网址内容、属性文件等,@PropertySource可用于指定属性文件。

Profile: 根据不同环境创建不同的bean,@Profile("prod"),可以通过context.getEnvironment().setActiveProfiles("prod")设置profile。

事件Application Event: 为Bean之间的通信提供支持,指定时间需要继承ApplicationEvent,事件监听器实现ApplicationListener<?>接口,之后通过ApplicationContext发布事件即可。

  • 进阶概念

Spring Aware:其本用于Spring框架内部,但当需要访问Spring容器资源时可以使用,比如实现BeanNameAware,ResourceLoaderAware。

多线程:可以通过TaskExecutor来实现多线程编程,使用ThreadPoolTaskExecuotr实现基于线程池的TaskExecutor,通过在配置类添加@EnableAsync开启对异步任务的支持,并在实际执行的Bean方法上使用@Async来声明其是一个异步任务。

 @Configuration
@ComponentScan("com.bjorktech.cayman.web")
@EnableAsync
public class TaskExecutorConfig implements AsyncConfigurer {
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(5);// 线程池基础大小
taskExecutor.setMaxPoolSize(10);// 最大大小
taskExecutor.setQueueCapacity(25);// 支持的处理队列大小
taskExecutor.initialize();
return taskExecutor;
} @Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}

计划任务:只需在config类上添加@EnableScheduling注解即可,其包含多种类型的计划任务,如cron(Linux下的定时任务设置形式), fixDelay, fixRate。

 @Service
public class ScheduledTaskService {
@Scheduled(fixedRate = 5000) // 5秒执行一次
public void reportCurrentTime() {
}
@Scheduled(cron = "0 11 11 ? * *") // 制定时间执行,比如每天11点11分
public void fixTimeExecution() {
}
}

条件注解:之前介绍了profile通过不同环境获取不同的bean,这儿介绍更通用的@Conditional条件注解。

 public class LinuxCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
return context.getEnvironment().getProperty("os.name").contains("Linux");
}
@Configuration
public class ConditionConfig {
@Bean
@Conditional(LinuxCondition.class)
public String LinuxService() {
return "";
}
}

组合注解和元注解:概念理解,组合注解是有多个组合起来的,更加简洁。

Enable*注解的工作原理:在Enable*中,通过@Import导入配置,具体导入配置的方法包括3种,直接导入配置类,依据条件选择配置类,动态注册bean。

@EnableAspectJAutoProxy开启对AspectJ自动代理的支持;@EnableAsync开启异步方法的支持;@EnableScheduling开始计划任务支持;@EnableWebMvc开始对Web MVC配置的支持;@EnableCaching开启注解式缓存的支持;@EnableTransactionManagement开启注解式事务的支持。

测试:Spring通过Spring TestContext Framework对测试提供支持,既可以使用Junit框架,也可以TestNG。

Tip:Demo路径,https://github.com/wanliwang/cayman/tree/master/cm-web,其中还包含SSE消息推送、异步方法处理,SpringMVC测试的例子。

相比于动态语言(Scala、Nodejs),java开发显得比较笨重,开发效率低称为了java的一大痛点,SpringBoot应运而生,其核心功能包括:独立运行的Spring项目,可以以jar运行;内嵌servlet容器;提供starter简化Maven配置;自动配置spring;准生产的应用监控;无代码生产和xml配置。

项目创建:在Eclipse中,可以使用Spring Tool Suite,创建Spring Starter Project,选择web和你所需的组件即可。

SpringBoot通常有一个*Application入口类,入口类中有一个main方法(和标准java应用一样),@SpringBootApplication是SpringBoot的核心注解,它是一个组合注解,组合了@Configuration, @EnableAutoConfiguration,@CompinentScan等。核心理解,比如添加了spring-boot-starter-web依赖,就会自动添加Tomcat和Spring MVC的依赖,那么Spring Boot会对Tomcat和Spring MVC进行自动配置。可以通过@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})排除某个自动配置。@EnableAutoConfiguration中可以看到很多不同的条件注解,比如@ConditionalOnBean,@ConditionalOnJava基于JVM等。

  • 配置文件

有一个全局的application.properties文件,放置在src/main/resoures目录,比如要修改tomcat端口,做如下配置即可

 @Service
public class ScheduledTaskService {
@Scheduled(fixedRate = 5000) // 5秒执行一次
public void reportCurrentTime() {
}
@Scheduled(cron = "0 11 11 ? * *") // 制定时间执行,比如每天11点11分
public void fixTimeExecution() {
}
}

当需用使用xml配置时,可以使用@ImportResource({"classpath:some-context.xml", "classpath:anpther-context.xml"})的形式。

  • 模板引擎

模板引擎有很多,包括FreeMarker,Groovy,Thymeleaf, Velocity等,Spring Boot推荐使用Thymeleaf,既然推荐,那就用吧,示例如下。

 @Service
public class ScheduledTaskService {
@Scheduled(fixedRate = 5000) // 5秒执行一次
public void reportCurrentTime() {
}
@Scheduled(cron = "0 11 11 ? * *") // 制定时间执行,比如每天11点11分
public void fixTimeExecution() {
}
}

概念:由于springboot的支持,无需添加TemplateResolver,其遵守约定编程,页面放在template中,且对HTML5格式有严格要求。其自动配置包括ContentNegotiatingViewResolver,BeanNameViewResolver等,静态资源放在/static,/public,/resources等中,可以把脚本封装在jar包中,放在/resources/webjars/目录,自动配置Formatter,Converter,默认首页为index.html。

  • 自定义配置:只需要对WebMvcConfigureAdapter进行扩展即可,添加自己需要的Bean,如XXFilter,XXListener等,会自动注册
  • Server配置
server.port=9090
server.context-path=/cm-bootweb
server.tomcat.uri-encoding=UTF-8

通过排除tomcat来引入其他的Server

      <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
  • SSL配置

步骤:通过jdk中的工具kettool生成.keystore,命令为kettool –genkey –alias tomcat;将生成的.keystore文件保存到resources中;在配置文件中添加如下配置即可(SSL协议版本有些老旧)。

server.port=8443
server.ssl.key-store=.keystore
server.ssl.key-password=123456
server.ssl.key-store-type=JKS
server.ssl.key-alias=tomcat

为了能够自动将http的请求跳转到https,需要配置TomcatEmbedderServletContainerFactory和添加Tomcat的Connnector,代码如下

    /**
* https
*/
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
collection.addPattern("/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
}; tomcat.addAdditionalTomcatConnectors(httpConnector());
return tomcat;
} @Bean
public Connector httpConnector() {
Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
connector.setScheme("http");
connector.setPort(8080);
connector.setSecure(false);
connector.setRedirectPort(8443);
return connector;
}
  • 其他:WebSocket(点对点,广播),验证与授权,AngulaJS整合等内容请见Demo

Tip:

thymeleaf官网,http://www.thymeleaf.org/

eclipse插件:http://www.thymeleaf.org/eclipse-plugin-update-site/

完整Demo路径,https://github.com/wanliwang/cayman/tree/master/cm-bootweb, https://github.com/wanliwang/cayman/tree/master/cm-angularweb

大神博客:http://wiselyman.iteye.com/blog/2003336

参考资料

  1. 汪云飞. Spring Boot实战[M]. 北京:电子工业出版社, 2016.

最新文章

  1. Servlet 之 ServletContext
  2. 小游戏Talk表
  3. web.xml常用元素配置
  4. C# 禁止程序多个实例运行
  5. VS2010 &quot;error MSB8011” 解决方法
  6. 解析const
  7. Android 自定义View实现单击和双击事件
  8. Java常用类之【日期相关类】
  9. stm32l053r8 nucelo板的串口实验
  10. FxCop卸载后依然生成文件夹的问题
  11. python3_猜数字
  12. Android 性能优化之减少UI过度绘制
  13. 学习GT一书前九章的体会
  14. postgresql----聚合函数
  15. numpy中arange()和linspace()区别
  16. easyUI取消选中的所有行
  17. Go语言的接口interface、struct和组合、继承
  18. 利用PHPExcel导出Excel相关设置
  19. 马克飞象markdown用法
  20. lzugis—搭建属于自己的小型的版本控制SVN

热门文章

  1. 成员变量与局部变量的区别--------java基础总结
  2. Zabbix应用六:Zabbix监控Redis
  3. python高级特性和高阶函数
  4. MySQL初始化以及客户端工具的使用
  5. 基于docker的spark-hadoop分布式集群之二: 环境测试
  6. spring框架学习(一)入门
  7. html之div始终停留在屏幕中间部分
  8. ie8下jquery改变PNG的opacity出现黑边
  9. shell tr命令
  10. HDU 2073 无限的路 (模拟)