Make a check on AOP pointcut expression - spring

I need to make a check in my pointcut expression like.
I have this bean:
<bean id="logConfig"
class="com.celfocus.ufe.base.logging.domains.LoggingConfiguration">
<property name="logDetails" value="STANDARD" />
<property name="logLvl" value="COMPLETE" />
</bean>
In my aop pointcut expression i need to make a check to verify the value of bean property "logLvl".
<aop:config>
<aop:aspect ref="ufeLogger">
<aop:pointcut id="complete" expression="execution(* *.*(..)) and bean(logConfig)==COMPLETE" />
<aop:before pointcut-ref="complete" method="logBefore" />
</aop:aspect>
</aop:config>
My expression isn't working... what I can change to make this check?

What makes you think that and bean(logConfig)==COMPLETE is a valid pointcut? Spring AOP uses AspectJ pointcut syntax, no Spring additions. Also you are not even referencing logLvl property, so has is this suppose to work?
Unfortunately to achieve this you must implement check manually. This isn't so intrusive though: simply inject logConfig into ufeLogger aspect and add a simple condition in logBefore() method.

This is the most relevant question, for solution below. I write aspect expressions from time to time. And expression might not work. You need to write right expression, which will be matched with your target method. I found a simple decision to check that everything is clear:
#Around(value="execution(* *.find(..))")
public Entity filterEntity(ProceedingJoinPoint pjp) throws Throwable {
Entity entity = (Entity) pjp.proceed(); // put breakpoint here
}
In Debug mode you can check target method real signature that is used for match with expression, in path pjp.methodInvocation.method
I hope this answer will save your time to find an error.
P.S If there are exists better decision to check expressions, glad to see it

Related

How to get set elements in the same sequence as in a Spring bean definition

I have a lot of bean definitions that look similar to this
<bean id="TransformationMapOrganization " class="com.artifact_software.adt.plugin.transformation.RemoveColumnsTransformationImpl">
<property name="pluginId" value="Remove Division, Department, and Cost Code" />
<property name="dataStoreName" value="person_data"/>
<property name="columnNames">
<set>
<value>Division</value>
<value>Dept Code</value>
<value>Cost Code</value>
</set>
</property>
</bean>
In the code, the columnNames are defined as:
protected List<String> columnNames;
It appears that erroneous duplicate values are ignored rather causing an error which is good. I hope that I can count on that since it does make life easier!
What set implementation will Spring use?
What is the correct way to iterate through columnNames to get the columnNames in the same sequence as they are specified in the bean?
You can set the implementation class via <set set-class="com.my.SetImpl" />, (see current doc). (com.my.SetImpl must implement java.util.Set)
alternatively: define targetClass on your SetFactoryBean...
If omit, current spring, will use java.util.LinkedHashSet.
More correct, reliable & future-safe it would be to map columnNames as java.util.Set not as a List (+ to use set-class).
If no set-class attribute is supplied, the container chooses a Set implementation.
(in your case,) Obviously spring manages to convert from set to list "smoothly" ("by hand" it's also easy done thx to api design). Spring (seems to) also preserves you distinct entries, LinkedHashSet implementation additionally guarantees/should "preserve order"...
Your Bean definition and field names are different. Change
<property name="ColumnNames">
to
<property name="columnNames">

Problem with AOP/AspectJ not executing on criterion

Does anyone know why the following AOP/AspectJ doesn't work in Examples 2,3 ?
Note: Example 1 works.
My goal is to intercept the firing of Hibernate Query's executeUpdate(), which occurs throughout the application. Hibernate Query is an interface, and I see in the code that the implementing class I get back is QueryImpl. So that is the class I want to target, although I've tried generic filters too.
XML
<aop:config>
<aop:aspect id="myAspect" ref="aspectBean">
<!-- EXAMPLE 1: SIMPLE TEST: WORKS OK.
I intercept all methods in all my custom classes in "dao" package. -->
<aop:pointcut id="test1" expression="execution(* myapp.dao.*.*(..))" />
<aop:before pointcut-ref="test1" method="doTest1" />
<!-- EXAMPLE 2: DOESN'T WORK.
Target everything in Hibernate's Impl package with executeUpdate() function -->
<aop:pointcut id="executeUpdate2" expression="execution(* org.hibernate.impl..*..executeUpdate(..))" />
<aop:before pointcut-ref="executeUpdate2" method="handleExecuteUpdate" />
<!-- EXAMPLE 3: DOESN'T WORK.
Target QueryImpl specifically -->
<aop:pointcut id="executeUpdate3" expression="execution(* org.hibernate.impl.QueryImpl.executeUpdate(..))" />
<aop:before pointcut-ref="executeUpdate3" method="handleExecuteUpdate" />
</aop:aspect>
</aop:config>
<bean id="aspectBean" class="myapp.util.AOPAspect">
</bean>
I know for a fact the Hibernate hierarchy is correct. Example #1 works great so I know the AOP/AspectJ is wired properly. Is there something about external library traversal that doesn't support AOP/AspectJ?
I found out it doesn't work because I'm dealing with an external JAR (in this case, Hibernate). Example 1 works because I'm within my own code.
No easy solutions for external JAR pointcuts, only Load-Time Weaving is possible (but I haven't tried it),
Aspectj: intercept method from external jar

Injecting jaxws:endpoint configuration published URL

<jaxws:endpoint id="serviceEndpoint" implementor="#serviceImpl" address="/public/api/service/v1"" publishedEndpointUrl="#{serviceImpl.endpointUrl}"> </jaxws:endpoint>
<bean id="serviceImpl" class="com.services.ServiceProviderImpl" autowire="byType"> </bean>
I need to populate publishedEndpointUrl refering a variable in my implementor bean. (ServiceProviderImpl) Please let me know the way I can make it. I refereed http://docs.spring.io/spring/docs/current/spring-framework-reference/html/expressions.html.
Making it publishedEndpointUrl="#{serviceImpl}.publishedUrl" does not make work ?
Thanks
Assuming ServiceProviderImpl has a public getter getPublishedUrl(), the syntax you need is
#{serviceImpl.publishedUrl}
In future, when adding questions here, "does not work" is not useful at all.
Always provide the error message and/or stack trace.

AOP: A combination of two #annotation clauses is not working

I'm trying to write a pointcut, which shall hit for every method marked with certain annotation except the ones marked with another annotation.
But the following is not working:
<aop:aspect ref="...">
<aop:before method="execute" pointcut="#annotation(MyAnnotation1)
and not #annotation(MyAnnotation2)"/>
</aop:aspect>
Will you please advise what i'm doing wrong?..
The Spring AOP documentation states
When combining pointcut sub-expressions, && is awkward within an XML
document, and so the keywords and, or and not can be used in place of
&&, || and ! respectively.
However, you're not allowed to bind parameters with negation. If you're saying it doesn't exist, what value would be passed as an argument?
Change it to
<aop:before method="execute" pointcut="#annotation(MyAnnotation1)
and not #annotation(com.example.MyAnnotationName)"/>
So MyAnnotation1 can refer to a parameter, but the other can't. As such, you need to specify the fully qualified name of the annotation type. A corresponding pointcut would look like
//#Pointcut(value = "#annotation(MyAnnotation1) && !#annotation(com.example.MyAnnotationName)")
public void yesNotNo(MyAnnotation1 MyAnnotation1) {
}

How to do spring request parameter conversion

In a Spring 3 based web (portlet) application I have a controller with a method like this:
#RenderMapping
public ModelAndView handleRenderRequest(...,#RequestParam MyClass myObject)
{
...
}
Now I wonder: How do I tell spring how to convert the request parameter to MyClass. I found information about property editors and about the Converter interface and there seem to be some implications that Converter is the successor of the property editor, but nobody seems to like being explicit about it.
I implemented the converter interface for String to MyClass conversion. But how do I tell Spring about it? I am using annotation based configuration wherever possible, so I checked whether spring will detect the Converter from my classpath automatically, but it does not.
So thought that the part Configuring a ConversionService from the manual wants to tell me that I've got to add the following to my applicationContext.xml which I did:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="some.package.MyConverter"/>
</list>
</property>
</bean>
Bit still:
org.springframework.beans.ConversionNotSupportedException: Failed to
convert value [...]
So what am I missing? And is there a way, to just configure a package and let spring scan this package for converters and register them automatically? And say that in one certain method I want to use a different converter than in all other methods. For example I want an integer that has a Luhn-Checksum to be checked and the checksum removed, how can I do that? Something like #RequestParam(converter=some.package.MyConverter.class) would be great.
EDIT
Ok, I just caught in the documentation:
Use the Formatter SPI when you're working in a client environment,
such as a web application, and need to parse and print localized field
values
So I guess that means I should use the Formatter SPI, yet a third possibility next to property editors and converters (I think I could really to with a comparison table or the like). I did implement the Parser interface as well and tried to register my converter using:
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="some.package.SortOrderEnumConverterSpring"/>
</set>
</property>
</bean>
As you can see I used "set" instead of "list" for specifying the converters. I set a debugging breakpoint in the FormattingConversionServiceFactoryBean.setConverters method which did not fire upon using list, but it did fire on using set.
Additionally I added
<mvc:annotation-driven conversion-service="conversionService"/>
And the namespace for the mvc-prefix to my applicationContext. But still I get the conversion not supported exception.
I also tried going back to the converter approach and changed in my applicationContext.xml file the parameter list for converters from list to set, but that did not change anything either.
EDIT2
As digitaljoel pointed out it is possible to set different converters per controller using an initBinder method. I applied this to my controller:
#Autowired
private ConversionService conversionService;
#InitBinder
public void initBinder(WebDataBinder binder)
{
binder.setConversionService(conversionService);
}
And in my applicationContext.xml:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="some.package.with.MyConverter"/>
</set>
</property>
</bean>
And all suddenly the conversion works just fine :-). But I am not quite happy about having to apply this to each and every of my controllers. There must be a way to just set it in my applicationContext for everyone, is there not? Good to know that I can override default if I need to (after all I asked for that), but I still want to set defaults.
And what about the Formatter stuff. Shouldn't I be using that instead of Converter?
Spring Portlet MVC 3.0 does not support
<mvc:annotation-driven conversion-service="conversionService"/>
Visit https://jira.springsource.org/browse/SPR-6817 for more info about this.
However you can add this to your common applicationContext
<bean
class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean
class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
<property name="conversionService">
<list>
<ref bean="conversionService" />
</list>
</property>
</bean>
</property>
</bean>
This way you do not need add #InitBinder to every single controller
and of course
<bean id="conversionService"
class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<!-- converter implementations here -->
</list>
</property>
</bean>
You are correct that Converter (and ConverterFactory) are the successors to property editors. Your problem may be that you are not accepting the appropriate type as a parameter to your converter, but that's hard to say without seeing the converter code. If you are expecting Long or Integer you may actually be getting a String from Spring and need to perform that key conversion yourself first.
As for configuration, I believe you need to list all of your converters in the bean configuration in your xml. If you annotate your converter implementation with #Component you might be able to reference it by the bean name instead of the fully qualified path, but I have only tried that for a ConverterFactory, not a Converter.
Finally, on specific converters, it looks like you may be able to configure the conversion service at the controller level (see Javi's answer on Setting up a mixed configuration for annotation-based Spring MVC controllers ) and then you could just place that method (and others that require that controller) into a controller that uses a secondary conversion service which you ought to be able to inject by name with the #Resource annotation.
Implement a WebArgumentResolver:
public class MyArgumentResolver implements WebArgumentResolver
{
#Override
public Object resolveArgument(MethodParameter methodParameter,
NativeWebRequest webRequest) throws Exception
{
Class<?> paramType = methodParameter.getParameterType();
if (paramType == MyClass.class)
{
String parameterName = methodParameter.getParameterName();
String stringParameter = webRequest.getParameter(parameterName);
return convert(stringParameter);
}
return UNRESOLVED;
}
}
And register it in your applicationContext.xml:
<bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="customArgumentResolver">
<bean class="com.dshs.eakte.util.MyArgumentResolver" />
</property>
</bean>
This works and even has the advantage of allowing parameter conversion that is based on multiple method parameters.
To achieve something similar to what you're doing, I found this blog entry useful.
i think you need to use something like
public ModelAndView handleRenderRequest(...,#ModelAttribute("myObject") MyClass myObject)

Resources