ホームページ  >  記事  >  Java  >  Springboot 構成のアノテーションの詳細な紹介

Springboot 構成のアノテーションの詳細な紹介

不言
不言転載
2019-02-01 11:36:394412ブラウズ

この記事では Springboot 設定のアノテーションについて詳しく説明します。必要な方は参考にしていただければ幸いです。

1. 概要

Spring Boot は、新しい Spring アプリケーションの初期構築および開発プロセスを簡素化するように設計されています。 Spring Boot は Spring の機能を拡張するものではありませんが、Spring を素早く使用する方法を提供します。

2. 特徴

①独立した Spring アプリケーションを作成します
②組み込み Tomcat、WAR ファイルをデプロイする必要はありません
③Maven 構成を簡素化します
④Spring を自動的に構成します
⑤本番環境を提供しますインジケーター、ヘルスチェック、外部設定などのすぐに使える機能
⑥ すぐに使用できるため、コード生成や XML 設定は必要ありません。

3. アノテーションの説明

@SpringBootApplication Spring Boot プロジェクトの中核となるアノテーション。主な目的は、クラスに作用する自動構成です。 XML 構成ファイル。構成 Spring
@Bean は、XML 構成の 60e23eb984d18edbb092da6b8f295aba
に相当するメソッドに作用します。@ComponentScan は、デフォルトで @SpringBootApplication が配置されているクラスの兄弟ディレクトリとそのサブディレクトリをスキャンします。 。
@PropertySource("classpath:env.properties") は外部構成ファイルを読み取り、@Value アノテーションを通じて値を取得します。
@Transactional はトランザクションを宣言します

4.

##src/main/java: コードを格納

src/main/resources

static: css、js、画像などの静的ファイルを格納 (アクセス方法 http://localhost:8080/js /main.js)
テンプレート: 静的ページの保存 jsp、html、tpl
config: 構成ファイル、application.properties

5 の保存

SpringBoot のデフォルト構成


/META-INF/resources/
/resources/
/static/
/public/

6. Spring Boot のホット デプロイメント


①依存関係を追加する

#③アイデアを自動コンパイルに設定し、Shift Ctrl Alt / を押してレジストリを選択します

コンパイラ.automake.allow.when.app.running を確認します

④ホットデプロイされていないファイル
1. /META-INF/maven, /META-INF/resources, /resources, /static, /public、または /templates

2. 指定されたファイルはホットデプロイされません spring.devtools.restart.exclude=static/* *,public/**

3. 手動で再起動 spring.devtools.restart をトリガーします。 trigger-file=trigger.txt

再起動せずにコードを変更し、テキストで制御


7. カスタマイズ開始バナー


①http://patorjk.com/にアクセスします。 software/taag/#p=display&h=3&v=3&f=4Max&t=itcast Spring Boot
②生成された文字をテキスト ファイルにコピーし、ファイルに Banner.txt という名前を付けます。
③Banner.txt を、リソース ディレクトリにコピーします。 project

8. グローバル設定ファイル (application.properties または application.yml)


spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/

詳細については、公式 Web サイトのアドレスをご覧ください



9. スターター pom

spring-boot-starter コア Spring Boot スターター (自動構成サポート、ログ、YAML を含む)
spring-boot-starter-amqp spring-rabbit を通じて実装された高度なメッセージ キュー プロトコルのサポート

spring-boot-starter-aop spring-aop および AspectJ を含むアスペクト指向プログラミングのサポート

spring -boot-starter-data-elasticsearch spring-data-elasticsearch を含む Elasticsearch 検索エンジンのサポート
spring-boot-starter-data-jpa spring-data-jpa、spring-orm、Hibernate を含む Java 永続性 API のサポート

spring-boot-starter-jdbc JDBC データベースのサポート

spring-boot-starter- redis spring-redis

spring-boot-starter- data-redis

spring-boot-starter-security spring-security
spring-boot-starter- を含む REDIS キーと値のデータ ストレージのサポートtest JUnit、Hamcrest、Mockito などの一般的なテスト依存関係のサポート spring-test
spring -boot-starter-velocity Velocity テンプレート エンジンのサポート
spring-boot-starter-activemq
spring-boot-starter -freemarker
spring-boot-starter-thymeleaf
spring-boot-starter -web は、Tomcat や spring-webmvc
spring-boot-starter-webflux
を含むフルスタック Web 開発をサポートします (「詳細な構成については Baidu)

10. 一般的に使用される json フレームワーク

(1) JavaBean は Json にシリアル化されます。パフォーマンス:
Jackson > Gson > Json-lib
(2) Jackson 処理関連コメント
指定されたフィールドは返されません: @JsonIgnore
日付形式を指定します: @JsonFormat(pattern="yyyy-MM-dd hh:mm:ss",locale="zh ",timezone="GMT 8")

空のフィールドは返されません: @JsonInclude(Include. NON_NUll)

エイリアスを指定します: @JsonProperty

11. SpringBoot はタスク スケジューリングを使用します

(1)使用步骤:

①启动类里面 @EnableScheduling开启定时任务,自动扫描
②定时任务业务类 加注解 @Component被容器扫描
③定时执行的方法加上注解 @Scheduled(fixedRate=2000) 定期执行一次

(2)常用定时任务表达式配置和在线生成器

cron 定时任务表达式 @Scheduled(cron="*/1 * * * * *") 表示每秒

crontab 工具  https://tool.lu/crontab/

ixedRate: 定时多久执行一次(上一次开始执行时间点后xx秒再次执行;)
fixedDelay: 上一次执行结束时间点后xx秒再次执行
fixedDelayString:  字符串形式,可以通过配置文件指定

(3)异步定时任务

启动类里面使用@EnableAsync注解开启功能,自动扫描
定义异步任务类并使用@Component标记组件被容器扫描,异步方法加上@Async
①要把异步任务封装到类里面,不能直接写到Controller
②增加Futuref7e83be87db5cd2d9a8a0b8117b38cd4 返回结果 AsyncResultf7e83be87db5cd2d9a8a0b8117b38cd4("task执行完成");  
③如果需要拿到结果 需要判断全部的 task.isDone()

十二、SpringBoot拦截器、过滤器、监听器

(1)SpringBoot启动默认加载的Filter 

characterEncodingFilter
hiddenHttpMethodFilter
httpPutFormContentFilter
requestContextFilter

(2)Filter优先级

Ordered.HIGHEST_PRECEDENCE
Ordered.LOWEST_PRECEDENCE

(3)自定义Filter

1)使用Servlet3.0的注解进行配置
2)启动类里面增加 @ServletComponentScan,进行扫描
3)新建一个Filter类,implements Filter,并实现对应的接口
4) @WebFilter 标记一个类为filter,被spring进行扫描 
urlPatterns:拦截规则,支持正则
5)控制chain.doFilter的方法的调用,来实现是否通过放行
  不放行,web应用resp.sendRedirect("/index.html");
场景:权限控制、用户登录(非前端后端分离场景)等

(4)Servlet3.0的注解自定义原生Listener监听器

自定义Listener(常用的监听器 servletContextListener、httpSessionListener、servletRequestListener)
@WebListener

 public class RequestListener implements ServletRequestListener { 

@Override

public void requestDestroyed(ServletRequestEvent sre) {
// TODO Auto-generated method stub
System.out.println("======requestDestroyed========");
}

@Override

public void requestInitialized(ServletRequestEvent sre) {
System.out.println("======requestInitialized========");
}

(5)自定义拦截器
1)implements WebMvcConfigurer
@Configuration

public class CustomWebMvcConfigurer implements WebMvcConfigurer  {

@Override

public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginIntercepter()).addPathPatterns("/api2/*/**");
//.excludePathPatterns("/api2/xxx/**");
WebMvcConfigurer.super.addInterceptors(registry);
}
}

2)自定义拦截器 HandlerInterceptor
preHandle:调用Controller某个方法之前
postHandle:Controller之后调用,视图渲染之前,如果控制器Controller出现了异常,则不会执行此方法
afterCompletion:不管有没有异常,这个afterCompletion都会被调用,用于资源清理
3)按照注册顺序进行拦截,先注册,先被拦截

(6)对比

Filter是基于函数回调 doFilter(),而Interceptor则是基于AOP思想
Filter在只在Servlet前后起作用,而Interceptor够深入到方法前后、异常抛出前后等
Filter依赖于Servlet容器即web应用中,而Interceptor不依赖于Servlet容器所以可以运行在多种环境。
在接口调用的生命周期里,Interceptor可以被多次调用,而Filter只能在容器初始化时调用一次。
Filter和Interceptor的执行顺序:过滤前->拦截前->action执行->拦截后->过滤后

十三、两种部署方式jar和war

(1)jar包方式启动

添加maven插件,执行打包即可,启动命令:    java -jar **.jar &

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

(2)war包方式启动

a.在pom.xml中将打包形式 jar 修改为war  0d5281799fde01379a2f5b5ea0c57ee5warb381c39f05d1340a5f54760f38fa259d
b.添加构建项目名称 1079ce9524e93e3b51b338f3745023bdxdclass_springboot8bfec31baefed0960a447581fc9bf1d0
c.修改启动类

public class XdclassApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(XdclassApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(XdclassApplication.class, args);
}
}

 d.打包项目,启动tomcat

十四、SpringBoot多环境配置

①不同环境使用不同配置
例如数据库配置,在开发的时候,我们一般用开发数据库,而在生产环境的时候,我们是用正式的数据
②配置文件存放路径
classpath根目录的“/config”包下
classpath的根目录下
③spring boot允许通过命名约定按照一定的格式(application-{profile}.properties)来定义多个配置文件  

以上がSpringboot 構成のアノテーションの詳細な紹介の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はcnblogs.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。