Giter Club home page Giter Club logo

spring-aspect-poc's Introduction

AOP proof of concept

This is a basic currency exchange rate API that displays aspect oriented programming used for logging and data access operations.

Annotations

  • Logged - used to annotate the functions that will be intercepted by LogAspect
  • DataAccess - used to annotate the functions that will be intercepted by DataAccessAspect

Aspect

  • LogAspect
    • Intercepts function calls to functions annotated with @Logged. These functions will trigger a log-call before and after function execution (provided function did not throw an exception) with the log level specified in the annotations field value logLevel.
  • DataAccessAspect
    • Intercepts function calls to functions annotated with @DataAccess. These functions will wrap the function call in a try/catch, transforming the potential SQL-related exceptions to a custom one along with a KFunction<*> object. This object is used to find the relevant error message for the given function that failed in the error messages resource bundle.

Setup

Clone repository, start application from src/main/kotlin/no/esa/aop/application/AopApplication.main.

Usage

  1. Build & run application in your IDE.
  2. View the Swagger-UI in a browser
  3. Try the requests in the exchange-rate-controller.
  4. To see how errors are handled, throw a few Exceptions in functions annotated with @DataAccess.

Examples

Data access operations

DataAccessAspect

This is called 'around' every function annotated with @DataAccess. joinPoint.proceed() calls the intercepted function. Any exception is immediately mapped to a DataAccess exception, which is picked up by /resource/ExceptionHandler.kt.

@Aspect
@Component
class DataAccessAspect {

	@Around("@annotation(no.esa.aop.annotation.DataAccess)")
	fun dataAccessOperation(joinPoint: ProceedingJoinPoint): Any? {
		val kFunction = getKFunction(joinPoint)

		return try {
			joinPoint.proceed()
		} catch (error: Exception) {
			getLogger(joinPoint).error(error.message)

			if (kFunction != null) {
				throw DataAccessException(kFunction, error)
			} else throw error
		}
	}
}

Function annotation

Abstracting away error handling from virtually identical functions improves readability.

@DataAccess
@Logged(APIType.DATA_ACCESS, LogLevel.INFO)
override fun getAll(): List<CurrencyEntity> {
	val query = QueryFileReader.readSqlFile(::getAll)

	return jdbcTemplate.query(query) { rs, _ ->
		CurrencyEntity(rs.getInt(PRIMARY_KEY), rs.getString(SYMBOL))
	}
}

DataAccess error logging

DataAccess standard logging

Standard logging

LogAspect

This aspect inspects the function signature and return value and logs the events in a clean format to the console & to file. The log level and api type are optional parameters that can be defined per function.

@Target(AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
annotation class Logged(val apiType: APIType = APIType.INTERNAL,
			val logLevel: LogLevel = LogLevel.DEBUG)

Various datatypes

spring-aspect-poc's People

Contributors

eivindantonsen avatar

Stargazers

 avatar

Watchers

 avatar

spring-aspect-poc's Issues

Frontend does not work

Frontend should display very simple breakdown of the values received through the call to the backend. It should be possible to update it by clicking the button, or viewing previous results.

However, frontend fails upon querying the backend. It's probably a very easy fix if you're familiar with React and JavaScript.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.