javascript
使用Spring Boot进行面向方面的编程
在上一篇文章中,我提供了一個(gè)有關(guān)如何通過(guò)使用ProxyFactoryBean并實(shí)現(xiàn)MethodBeforeAdvice接口在Spring實(shí)現(xiàn)寬高比定向的簡(jiǎn)單示例。
在此示例中,我們將學(xué)習(xí)如何通過(guò)使用Spring Boot和Aspect4j注釋來(lái)實(shí)現(xiàn)方面方向。
讓我們從gradle文件開(kāi)始。
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軟件包。
應(yīng)用類別
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);} }我們將實(shí)現(xiàn)一項(xiàng)服務(wù),該服務(wù)將獲取指定名稱的示例。
樣本模型將是一個(gè)簡(jiǎn)單的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;} }該服務(wù)將創(chuàng)建一個(gè)示例對(duì)象。
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;} } 到目前為止,一切都很好。 假設(shè)我們要在創(chuàng)建樣本之前和之后執(zhí)行一些操作。 Spring的AOP可以幫助我們做到這一點(diǎn)。
createSample函數(shù)是一個(gè)JoinPoint。 主要概念是與建議一起使用。 根據(jù)文檔建議,是方面在特定的連接點(diǎn)處采取的措施。
在我們的例子中,我們想在創(chuàng)建樣本之前做一些額外的日志記錄。 因此,我們將使用之前建議類型。
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注釋實(shí)現(xiàn)了一個(gè)函數(shù)。 我們提供給注解的參數(shù)是切入點(diǎn)表達(dá)式。 切入點(diǎn)表達(dá)式可幫助我們定義函數(shù),這將觸發(fā)我們的建議和應(yīng)使用的函數(shù)參數(shù)。 因此,在執(zhí)行createSample方法之前,應(yīng)該在屏幕上顯示一條日志消息。
假設(shè)我們要在執(zhí)行該方法之前和之后采取更多措施,甚至更改createSample函數(shù)的結(jié)果,我們可以使用@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建議一樣,更改輸入并更改結(jié)果。 您可以在github上找到源代碼
翻譯自: https://www.javacodegeeks.com/2016/05/aspect-oriented-programming-spring-boot.html
總結(jié)
以上是生活随笔為你收集整理的使用Spring Boot进行面向方面的编程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 货代企业备案和对外贸易备案(货代企业备案
- 下一篇: gradle idea java ssm