前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SpringBoot详细研究-01基础

SpringBoot详细研究-01基础

作者头像
用户1216676
发布2018-01-24 16:26:14
1.1K0
发布2018-01-24 16:26:14
举报
文章被收录于专栏:熊二哥熊二哥

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来声明其是一个异步任务。

代码语言:javascript
复制
 1 @Configuration
 2 @ComponentScan("com.bjorktech.cayman.web")
 3 @EnableAsync
 4 public class TaskExecutorConfig implements AsyncConfigurer {
 5     @Override
 6     public Executor getAsyncExecutor() {
 7         ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
 8         taskExecutor.setCorePoolSize(5);// 线程池基础大小
 9         taskExecutor.setMaxPoolSize(10);// 最大大小
10         taskExecutor.setQueueCapacity(25);// 支持的处理队列大小
11         taskExecutor.initialize();
12         return taskExecutor;
13     }
14 
15     @Override
16     public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
17         return null;
18     }
19 }

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

代码语言:javascript
复制
1 @Service
2 public class ScheduledTaskService {
3     @Scheduled(fixedRate = 5000) // 5秒执行一次
4     public void reportCurrentTime() {
5     }
6     @Scheduled(cron = "0 11 11 ? * *") // 制定时间执行,比如每天11点11分
7     public void fixTimeExecution() {
8     }
9 }

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

代码语言:javascript
复制
 1 public class LinuxCondition implements Condition {
 2     @Override
 3     public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
 4         return context.getEnvironment().getProperty("os.name").contains("Linux");
 5     }
 6 @Configuration
 7 public class ConditionConfig {
 8     @Bean
 9     @Conditional(LinuxCondition.class)
10     public String LinuxService() {
11         return "";
12     }
13 }

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

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端口,做如下配置即可

代码语言:javascript
复制
1 @Service
2 public class ScheduledTaskService {
3     @Scheduled(fixedRate = 5000) // 5秒执行一次
4     public void reportCurrentTime() {
5     }
6     @Scheduled(cron = "0 11 11 ? * *") // 制定时间执行,比如每天11点11分
7     public void fixTimeExecution() {
8     }
9 }

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

  • 模板引擎

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

代码语言:javascript
复制
1 @Service
2 public class ScheduledTaskService {
3     @Scheduled(fixedRate = 5000) // 5秒执行一次
4     public void reportCurrentTime() {
5     }
6     @Scheduled(cron = "0 11 11 ? * *") // 制定时间执行,比如每天11点11分
7     public void fixTimeExecution() {
8     }
9 }

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

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

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

代码语言:javascript
复制
 1      <dependency>
 2             <groupId>org.springframework.boot</groupId>
 3             <artifactId>spring-boot-starter-web</artifactId>
 4             <exclusions>
 5                 <exclusion>
 6                     <groupId>org.springframework.boot</groupId>
 7                     <artifactId>spring-boot-starter-tomcat</artifactId>
 8                 </exclusion>
 9             </exclusions>
10         </dependency>
11         <dependency>
12             <groupId>org.springframework.boot</groupId>
13             <artifactId>spring-boot-starter-jetty</artifactId>
14         </dependency>
  • SSL配置

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

代码语言:javascript
复制
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,代码如下

代码语言:javascript
复制
 1    /**
 2      * https
 3      */
 4     @Bean
 5     public EmbeddedServletContainerFactory servletContainer() {
 6         TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory() {
 7             @Override
 8             protected void postProcessContext(Context context) {
 9                 SecurityConstraint securityConstraint = new SecurityConstraint();
10                 securityConstraint.setUserConstraint("CONFIDENTIAL");
11                 SecurityCollection collection = new SecurityCollection();
12                 collection.addPattern("/*");
13                 securityConstraint.addCollection(collection);
14                 context.addConstraint(securityConstraint);
15             }
16         };
17 
18         tomcat.addAdditionalTomcatConnectors(httpConnector());
19         return tomcat;
20     }
21 
22     @Bean
23     public Connector httpConnector() {
24         Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
25         connector.setScheme("http");
26         connector.setPort(8080);
27         connector.setSecure(false);
28         connector.setRedirectPort(8443);
29         return connector;
30     }
  • 其他: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.
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-05-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档