Springデータは、監査のサポートを提供します。つまり、エンティティが作成または変更された場合です。 Springデータは、@createdby、@lastmodifiedby、@createddate、@lastmodifieddateアノテーションをエンティティクラスのプロパティに追加し、対応する構成項目を構成して監査機能を実現することを提供します。このシステムは、createddateの4つのプロパティの値を自動的に記録します。以下は特定の構成項目です。
例
エンティティクラスを作成します
パッケージcom.hfcsbc.infrastructureService.domain; Import com.hfcsbc.repository.support.domain.abstractaudintintity; import lombok.data; import org.springframework.data.annotation.createdby; Import org.springframework.data.annotaint.data.annota.createdation org.springframework.data.annotation.lastModifiedBy; Import org.springframework.data.annotation.lastModifiedDate; Import org.springframework.data.jpa.domain.support.auditingEntityListener; Import.persence Pengchao on 2018/3/7 */@entity@data@entitylisteners({auditingentitylistener.class})public class person {@id @generatedValueプライベートLong id;プライベート文字列名;民間整数年齢; @createdby @column(name = "created_by"、nullable = false、length = 50、update = false)private string recutedby; @createddate @column(name = "created_date"、nullable = false、update = false)private date createddate = new date(); @lastmodifiedby @column(name = "last_modified_by"、length = 50)private string lastmodifiedby; @lastmodifieddate @column(name = "last_modified_date")private date lastmodifieddate = new date();}対応するリポジトリを作成します
パッケージcom.hfcsbc.repository; Import com.hfcsbc.domain.person; Import org.springframework.data.jpa.repository.jparepository;/** * create by pengchao by pengchao by Pengchao by Pengchao by Peng interface PersonRepository
ユーザー情報を取得するように豆を構成します
パッケージcom.hfcsbc.infrastructureservice.config; Import org.springframework.data.domain.auditoraware; Import org.springframework.security.core.authentication; Import org.springframework.security.core.core.context.securitycontextholder; org.springframework.stereotype.component; Import java.util.optional;/** * Create by Pengchao by Pengchao by Pengchao by Pengchao by @ @component( "Auditoraware")Public Class Auditorawareimple SecurityContextholder.getContext()。getAuthentication(); return optional.of(authentication.getPrincipal()。toString()); }}
スプリングブートエントリクラスの監査機能をオンにします
package com.hfcsbc.infrastructureservice;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.data.jpa.repository.config.EnableJpaAuditing;import org.springframework.scheduling.annotation.enableasync;@springbootapplication@enablejpaauditing(auditorawareref = "auditoraware")@enableasyncpublic class personapplication {public static void main(string [] args){springApplication.run(anryApplication.class); }}つまり、構成が完了します。リポジトリを使用してオブジェクトを保存する場合、createddateはlastmodifieddateを作成したcreateddateによって、自動的に挿入する監査関数があります。
注:非同期方法でユーザー情報を取得する方法
リポジトリを使用してオブジェクトを非同期メソッドで保存するため、ユーザー情報を取得することはできません。認証でユーザー情報を取得するには、次の構成アイテムを追加する必要があります
パッケージcom.hfcsbc.config; Import org.springframework.beans.factory.config.methodinvokingfactorybean; Import org.springframework.context.annotation.bean; Import org.springframework.context.annotation.configuration; Import; org.springframework.security.core.context.securitycontextholder;/** * create by pengchao by pengchao by @configurationpublic class auditorawareconfig { MethodInvokingFactoryBean(); MethodInvokingFactoryBean.SettargetClass(SecurityContextholder.Class); MethodInvokingFactoryBean.SettargetMethod( "SetStrategYname"); MethodVokingFactoryBean.Setarguments(new String [] {securityContextholder.Mode_InheritableThreadLocal}); MethodInvokingFactoryBeanを返します。 }}SecurityContextholderの主な機能は、現在実行されているプロセスをSecurityContextに関連付けることです。
SecurityContexTholder.Mode_InheritablEthReadLocal:スレッドに親子関係がある場合に使用される、子スレッドは親スレッドのSecurityContexTholderを統合します。
SecurityContextholder.Mode_InheritableThreadLeadLocal:GlobalがSecurityContExtholderを共有しています。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。