Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dependency injection to the AspectJ interceptor
- Loading branch information
Showing
4 changed files
with
48 additions
and
6 deletions.
There are no files selected for viewing
11 changes: 5 additions & 6 deletions
11
src/main/java/com/madadipouya/sample/aspectj/interceptor/LoggingInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,21 @@ | ||
package com.madadipouya.sample.aspectj.interceptor; | ||
|
||
import com.madadipouya.sample.aspectj.service.LoggingService; | ||
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; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.time.ZoneOffset; | ||
import java.time.ZonedDateTime; | ||
|
||
@Aspect | ||
@Component | ||
public class LoggingInterceptor { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(LoggingInterceptor.class); | ||
@Autowired | ||
private LoggingService loggingService; | ||
|
||
@Before(value = "execution(* com.madadipouya.sample.aspectj.controller.UserController.getUsersInternal(..))") | ||
public void addCommandDetailsToMessage() throws Throwable { | ||
logger.info("User controller getUsers method called at {}", ZonedDateTime.now(ZoneOffset.UTC)); | ||
loggingService.log(String.format("User controller getUsers method called at %s", ZonedDateTime.now((ZoneOffset.UTC)))); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...main/java/com/madadipouya/sample/aspectj/interceptor/config/LoggingInterceptorConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.madadipouya.sample.aspectj.interceptor.config; | ||
|
||
import com.madadipouya.sample.aspectj.interceptor.LoggingInterceptor; | ||
import org.aspectj.lang.Aspects; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class LoggingInterceptorConfig { | ||
|
||
@Bean | ||
public LoggingInterceptor getAutowireCapableLoggingInterceptor() { | ||
return Aspects.aspectOf(LoggingInterceptor.class); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/madadipouya/sample/aspectj/service/LoggingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.madadipouya.sample.aspectj.service; | ||
|
||
public interface LoggingService { | ||
|
||
void log(String message); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/madadipouya/sample/aspectj/service/impl/DefaultLoggingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.madadipouya.sample.aspectj.service.impl; | ||
|
||
import com.madadipouya.sample.aspectj.service.LoggingService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
/** | ||
* A dummy implementation of logging service, | ||
* just to inject it in {@link com.madadipouya.sample.aspectj.interceptor.LoggingInterceptor} | ||
* that's managed by AspectJ | ||
*/ | ||
@Service | ||
public class DefaultLoggingService implements LoggingService { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger("sample-spring-aspectj"); | ||
|
||
@Override | ||
public void log(String message) { | ||
logger.info(message); | ||
} | ||
} |