javascript
使用Spring Boot进行面向方面的编程
在上一篇文章中,我提供了一個有關如何通過使用ProxyFactoryBean并實現MethodBeforeAdvice接口在Spring實現寬高比定向的簡單示例。
在此示例中,我們將學習如何通過使用Spring Boot和Aspect4j注釋來實現方面方向。
讓我們從gradle文件開始。
group 'com.gkatzioura' version '1.0-SNAPSHOT'apply plugin: 'java' apply plugin: 'idea' apply plugin: 'spring-boot'sourceCompatibility = 1.8buildscript {repositories {mavenCentral()}dependencies {classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")} }repositories {mavenCentral() }dependencies {compile("org.springframework.boot:spring-boot-starter-web") {exclude module: "spring-boot-starter-tomcat"}compile("org.springframework.boot:spring-boot-starter-jetty")compile("org.slf4j:slf4j-api:1.6.6")compile("ch.qos.logback:logback-classic:1.0.13")compile("org.aspectj:aspectjweaver:1.8.8")testCompile("junit:junit:4.11") }除了Spring Boot插件外,我們還必須包括AspectJweaver軟件包。
應用類別
package com.gkatzioura.spring.aop;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext;/*** Created by gkatzioura on 5/28/16.*/ @SpringBootApplication public class Application {public static void main(String[] args) {SpringApplication springApplication = new SpringApplication();ApplicationContext applicationContext = springApplication.run(Application.class,args);} }我們將實現一項服務,該服務將獲取指定名稱的示例。
樣本模型將是一個簡單的pojo
package com.gkatzioura.spring.aop.model;/*** Created by gkatzioura on 5/28/16.*/ public class Sample {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;} }該服務將創建一個示例對象。
package com.gkatzioura.spring.aop.service;import com.gkatzioura.spring.aop.model.Sample; import org.springframework.stereotype.Service;/*** Created by gkatzioura on 5/28/16.*/ @Service public class SampleService {public Sample createSample(String sampleName) {Sample sample = new Sample();sample.setName(sampleName);return sample;} } 到目前為止,一切都很好。 假設我們要在創建樣本之前和之后執行一些操作。 Spring的AOP可以幫助我們做到這一點。
createSample函數是一個JoinPoint。 主要概念是與建議一起使用。 根據文檔建議,是方面在特定的連接點處采取的措施。
在我們的例子中,我們想在創建樣本之前做一些額外的日志記錄。 因此,我們將使用之前建議類型。
package com.gkatzioura.spring.aop.aspect;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component;/*** Created by gkatzioura on 5/28/16.*/ @Aspect @Component public class SampleServiceAspect {private static final Logger LOGGER = LoggerFactory.getLogger(SampleServiceAspect.class);@Before("execution(* com.gkatzioura.spring.aop.service.SampleService.createSample (java.lang.String)) && args(sampleName)")public void beforeSampleCreation(String sampleName) {LOGGER.info("A request was issued for a sample name: "+sampleName);}}我們使用@Before注釋實現了一個函數。 我們提供給注解的參數是切入點表達式。 切入點表達式可幫助我們定義函數,這將觸發我們的建議和應使用的函數參數。 因此,在執行createSample方法之前,應該在屏幕上顯示一條日志消息。
假設我們要在執行該方法之前和之后采取更多措施,甚至更改createSample函數的結果,我們可以使用@Around Advice。
package com.gkatzioura.spring.aop.aspect;import com.gkatzioura.spring.aop.model.Sample; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component;/*** Created by gkatzioura on 5/28/16.*/ @Aspect @Component public class SampleServiceAspect {private static final Logger LOGGER = LoggerFactory.getLogger(SampleServiceAspect.class);@Before("execution(* com.gkatzioura.spring.aop.service.SampleService.createSample (java.lang.String)) && args(sampleName)")public void beforeSampleCreation(String sampleName) {LOGGER.info("A request was issued for a sample name: "+sampleName);}@Around("execution(* com.gkatzioura.spring.aop.service.SampleService.createSample (java.lang.String)) && args(sampleName)")public Object aroundSampleCreation(ProceedingJoinPoint proceedingJoinPoint,String sampleName) throws Throwable {LOGGER.info("A request was issued for a sample name: "+sampleName);sampleName = sampleName+"!";Sample sample = (Sample) proceedingJoinPoint.proceed(new Object[] {sampleName});sample.setName(sample.getName().toUpperCase());return sample;}}正如我們所看到的aroundSampleCreation建議一樣,更改輸入并更改結果。 您可以在github上找到源代碼
翻譯自: https://www.javacodegeeks.com/2016/05/aspect-oriented-programming-spring-boot.html
總結
以上是生活随笔為你收集整理的使用Spring Boot进行面向方面的编程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 货代企业备案和对外贸易备案(货代企业备案
- 下一篇: linux删除文件夹命令(linux 删