Invoke annotated aspect on all methods with a specific annotation - java

I have an application based on MVC and I want to log inputs/outputs from all my controllers.
Also, each of my controller methods is annotated with some specific annotation (eg: #MyControllerMethod).
I wrote an Annotated Aspect which works fine when I use the annotation (eg: LogRequestReply) from the aspect on my controller methods.
#Around("#annotation(com.xyz.LogRequestReply)")
public Object logRequestResponse(ProceedingJoinPoint pjp) throws Throwable {
#MyControllerMethod
#LogRequestReply /* It invokes my logRequestResponse method */
public ResponseEntity controllerMethodA(...) {}
However, I have a lot of controller methods and each with annotation MyControllerMethod.
Is there a way I can make my aspect annotation make work on all controller methods by default, such that each of the methods automatically calls my aspect logger and I can log the inputs/outputs?
Note:
I also looked into writing an interceptor extending from HandlerInterceptorAdapter. But it has its own complexity of being able to log request/response where the stream can only be read once.
Please suggest.

One option is to let the #Around definition less restrictive, add a restriction by package or something like that.
Internally you can check if this is a Controller by this check bellow and then print the request/response:
#Pointcut("within(com.mypackage.controller..*)")
private void inControllerPackage() {
}
#Around("inControllerPackage()")
public Object logRequestResponse(ProceedingJoinPoint pjp) throws Throwable {
if (pjp.getSignature().getDeclaringType().isAnnotationPresent(RestController.class)) {
...
}

Perhaps you should include the AOP in your controller-config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<mvc:default-servlet-handler />
<context:component-scan base-package="com.xyz.controller" />
<aop:aspectj-autoproxy proxy-target-class="true" />
<bean id="whatever" class="com.xyz.controller.MyRestController"/>
</bean>
and in your Aspect you can also include the within combined with annotation:
#Around(value = "#within(com.xyz.server.annotation.MyAnnotationForController) || #annotation(com.xyz.server.annotation.MyAnnotationForController)")
public Object ownersTimeZone(ProceedingJoinPoint joinPoint) throws Throwable {
//whatever you need before
Object obj = joinPoint.proceed();
//whatever you need after
return obj;
}

Related

Spring doesn't instantiate bean at start

I'm stuck with the following problem. My application doesn't create my Services at startup and I end up with a NullPointerException when I try to use them in my Maven Tests after Inject them with #Autowired.
I don't understand where it comes from. I have done some research but I still don't understand why it doesn't work.
Here is the class where my Autowired administrationActionService is null:
public class AdministrationActionTests extends EntityTests {
#Autowired
AdministrationActionService administrationActionService;
#Test
public void equalsTests() {
administrationActionService.getExample();
[...]
The class :
package com.bloombooking.services;
#org.springframework.stereotype.Service
public class AdministrationActionService extends ServiceEntity{
#Autowired private AdministrationActionDao administrationActionDao;
[...]
And my ApplicationContext.xml. I've placed it in src/ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.bloombooking.services, com.bloombooking.dao"/>
</beans>
I really don't know what I could have done wrong. Can someone help me? Thanks!
To make it work you have to make the following changes:
add #RunWith(SpringJUnit4ClassRunner.class) to your class.
add #ContextConfiguration("path_to_you_spring_beans.xml")
So your class becomes:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration("path_to_you_spring_beans.xml")
AdministrationActionTests {
}

Spring: define custom #Transactional behavior in Java config

I would like Spring to rollback a transaction on methods annotated with #Transactional in case the method throws a checked exception. An equivalent of this:
#Transactional(rollbackFor=MyCheckedException.class)
public void method() throws MyCheckedException {
}
But I need this behavior to be default for all #Transactional annotations without the need to write it everywhere. We are using Java to configure Spring (configuration classes).
I tried the configuration suggested by spring documentation, which is only available in XML. So I tried to create this XML file:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" rollback-for="com.example.MyCheckedException" />
</tx:attributes>
</tx:advice>
</beans>
... and import it via #ImportResource. Spring did recognize and parse the file (I had some errors in it at first), but it doesn't work. The behavior of #Transactional has not changed.
I also tried defining my own transaction property source, as suggested in this answer. But it also used the XML configuration so I had to transform it into Java like this:
#Bean
public AnnotationTransactionAttributeSource getTransactionAttributeSource() {
return new RollbackForAllAnnotationTransactionAttributeSource();
}
#Bean
public TransactionInterceptor getTransactionInterceptor(TransactionAttributeSource transactionAttributeSource) {
TransactionInterceptor transactionInterceptor = new TransactionInterceptor();
transactionInterceptor.setTransactionAttributeSource(transactionAttributeSource);
return transactionInterceptor;
}
#Bean
public BeanFactoryTransactionAttributeSourceAdvisor getBeanFactoryTransactionAttributeSourceAdvisor(TransactionAttributeSource transactionAttributeSource) {
BeanFactoryTransactionAttributeSourceAdvisor advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
advisor.setTransactionAttributeSource(transactionAttributeSource);
return advisor;
}
This also didn't work - Spring kept using its own transaction property source (different instance than the one which was created in the configuration).
What is the correct way to achieve this in Java?
You should rather implement own annotation - reference
#Target({ElementType.METHOD, ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
#Transactional(rollbackFor=MyCheckedException.class)
public #interface TransactionalWithRollback {
}

Spring Integration gateway proxy bean for <int:gateway> not being created

I have a context as follows
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int="http://www.springframework.org/schema/integration"
xmlns:int-amqp="http://www.springframework.org/schema/integration/amqp"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
http://www.springframework.org/schema/integration/amqp
http://www.springframework.org/schema/integration/amqp/spring-integration-amqp.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<int:gateway id="fooGateway" service-interface="com.foo.FooGateway" />
</beans>
My gateway interface is as follows:
public interface FooGateway
{
public void foo(String foo);
}
This context is imported into another context but the fooGateway bean is never created by GatewayProxyFactoryBean.
I've set a breakpoint in GatewayProxyFactoryBean and I can see that it is not creating a proxy for this interface.
Turn on DEBUG logging for org.springframework; the bean creation process emits lots of information including when it reads particular configuration files.

Reading from properties file in spring

I have a web application in which I'm using spring MVC. I have defined few constants in sample.properties file. spring-servlet is the servlet that gets initialized for all urls in my app. spring-servlet:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<context:property-placeholder location="classpath:sample.properties"/>
</beans>
In one of my java classes I access the value of one of the properties like this:
package com.sample;
import com.google.gson.JsonArray;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
#Component
public class Validator {
#Value("${credit}")
String credits;
private static final Logger LOG = Logger.getLogger(Validator.class);
public void checkRating() {
LOG.debug(credits);
}
}
sample.properties:
credit=[{"duration":15,"credit":10},{"duration":30,"credit":20}]
When Validator is called from a controller, it just logs it as ${credit} in my log file. I dont get its value. Similarly, if I put an integer / floating point number in the properties file, I get an error saying it cannot be cast into integer / float. Am I missing any configuration?
The reason for this is because propertyplaceholder configurer is a BeanFactoryPostProcessor and it processess only the beans defined in the context in which the propertyplaceholder is configured. In a web application typically there is a heirarchy of context as explained here. Hence you need to declare this at appropriate place.

logging with AOP in spring?

I am new to spring in my office . So there is no guidance for me.
I need to implement the logging with the AOP using the log4j.
I have implemented the logging without AOP in basic spring MVC example ?
Also did the small sample in AOP using the aspectJ without logging (just made the Sysout) ?
I don't know how to integrate it ?
Can any one please give me a start up idea?
Good answers are definitely appreciated ...
Spring makes it really easy for us to make use of AOP. Here's a simple logging example:
#Aspect
public class MyLogger {
private Logger log = Logger.getLogger(getClass());
#After("execution(* com.example.web.HomeController.*(..))")
public void log(JoinPoint point) {
log.info(point.getSignature().getName() + " called...");
}
}
Then simply configure your applicationContext.xml (or equivalent):
<aop:aspectj-autoproxy>
<aop:include name="myLogger"/>
</aop:aspectj-autoproxy>
<bean id="myLogger" class="com.example.aspect.MyLogger"/>
You'll notice in the MyLogger class that I specified #After right above the method. This is called the advice and it basically specifies that this 'log' method will be called after the method in question. Other options include #Before, #Around, #AfterThrowing.
The expression "execution(* com.example.web.HomeController.*(..))" is called a pointcut expression and specifies what we're targeting (in this case all methods of the HomeController class).
P.S. The aop namespace (xmlns:aop="http://www.springframework.org/schema/aop") and the schema location (version dependent) would need to be added to your applicationContext.xml right at the top. Here is my setup:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
You need to perform several steps to integrate Aspectj:
Install AspectJ
Add your aop.xml to META-INF\aop.xml in your project
Add aspectjrt-x.x.0.jar and aspectjweaver-x.x.0.jar in your project classpath
Add -javaagent:/path to aspectj installation/aspectjweaver-1.7.0.jar to your server's JVM.
Here is a sample aop.xml:
<aspectj>
<aspects>
<aspect name="test.MySimpleLoggerAspect" />
</aspects>
<weaver>
<include within="test.myproject.*" />
</weaver>
</aspectj>
If you are already using Spring then it is better to use Spring to simplify your setup.

Categories