Spring boot AOP Aspect not being invoked - java

I'm trying to intercept rest service calls with an aspect in the following manner
package mypackage.services.Service;
#Component
public class Service {
#Override
public Response helloService() {
return handleResult("Hello test " + new Date());
}
}
#Component
#Aspect
public class AuditLog {
#Before("execution(* mypackage.services.Service.*(..))")
public void beforeServcie(JoinPoint jp){
log.info("Before ",jp.getSignature().getName());
}
}
I'm using the following maven dependencies
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
This maven plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.0</version>
</plugin>
And my configuration xml contains
<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"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="mypackage"/>
<aop:aspectj-autoproxy proxy-target-class="true" />
also in the Application class I've added the following annotation
#Configuration
#EnableAspectJAutoProxy(proxyTargetClass=true)
public class Configuration{
...
}
On startup, by logging beans in the ApplicationContext, I can see that the aspect class "AuditLog" is being created.
I've set 2 breakpoints, but the debugger does not stop at the "beforeServcie" method but it does stop at the "helloService".
What am I missing?

Try this
execution(* mypackage.services.Service.*.*(..))
instead of
execution(* mypackage.services.Service.*(..))

If you are using spring-boot then instead of automatically adding dependency jars you can do
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
If you're using XML config <aop:aspectj-autoproxy ... /> then no need to have #EnableAspectJAutoProxy. It probably doesn't matter since AFAIK XML config wins over annotation config but better to avoid duplication
I am not quite sure why do you need aspectj-maven-plugin since Spring implements AOP by proxy and AFAIK this plugin is only needed for compile-time, post compile-time or load time weaving which are different concepts, see Spring AOP vs AspectJ
Now all the above mentioned points may not resolve your issue but the following might
execution(* mypackage.services.Service.Service.*(..))
And, don't set proxyTargetClass=true, let it be default false.
Explanation
The format is execution(<return type> <package name>.<class name>.<method name>(..)
The package name here is mypackage.services.Service and the class name is Service.

Related

Spring AspectJ Integration Not Working

I am attempting to use the Spring/AspectJ integration with no luck. Spring version is 3.2.17 (yes, a bit old, I know).
Here is my relevant configuration:
pom.xml:
<!-- Spring dependencies, including spring-aspects -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
applicationContext.xml:
<context:annotation-config/>
<aop:aspectj-autoproxy />
<bean id="loggingAspect" class="com.jason.app.web.util.logging.LoggingAspect" />
LoggingAspect.java (relevant class):
#Aspect
public class LoggingAspect {
private Logger log = LoggerFactory.getLogger(LoggingAspect.class);
/**
* Advice for before logging
* #param joinPoint
*/
#Before("execution(* com.jason.app.web.process..*(..))")
private void beforeAdvice(JoinPoint joinPoint) {
final String outputFormat = "intercept: executing method %s(%s)";
final String method =joinPoint.getSignature().getName();
List<?> argumentList = Collections.unmodifiableList(Arrays.asList(joinPoint.getArgs()));
final String formattedArguments = argumentList.stream().map(s -> s.toString()).collect(Collectors.joining(", "));
log.debug(String.format(outputFormat, method, formattedArguments));
}
}
I've pour over online tutorials, no luck. Can anyone point out what I did wrong?
Jason
The Spring configuration tag <aop:aspectj-autoproxy /> will enable Spring's proxy based AOP infrastructure, which only applies to Spring beans, and it does so using proxies with all the limitations of this solution compared to a pure AspectJ one.
Now if you want to go with AspectJ instead of Spring AOP, you will need to choose between compile-time weaving or load-time weaving. If you go with compile-time weaving, you need to add the aspectj-maven-plugin to your build. If you choose load-time-weaving, you'll need to run your JVM with a -javaagent:path/to/aspectjweaver.jar vm argument, as documented in AspectJ Documentation.
If you need to make your aspect post-processed by Spring (autowiring, etc), you need to list it in your Spring configuration. Aspects are singleton instances created outside of Spring, so you need to specify the static factory method aspectOf() to acess the single instance of the aspectj created by the AspectJ runtime.
<bean id="loggingAspect"
class="com.jason.app.web.util.logging.LoggingAspect"
factory-method="aspectOf"
/>
or the annotated way:
#Configuration
public class AspectConfig {
#Bean
public LoggingAspect loggingAspect() {
return LoggingAspect.aspectOf();
}
}
Don't forget to remove <aop:aspectj-autoproxy /> if you're not planning to use Spring AOP in addition to AspectJ. And why would you choose to do so, when AspectJ is so much more powerful?
you could add one more dependency
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
try, changing your point cut to
#Before("execution(* com.jason.app.web.process..*.*(..))")
( means advice will be applied to all public methods defined in the service package or a sub-package: com.jason.app.web.process )
change the expression to #Before("execution(public * your.package.YourClass.yourMethod(..))")

SpringBeanAutowiringInterceptor throwing NoSuchBeanDefinitionException

I'm trying to autowire beans to EJBs with Spring using the #Interceptors(SpringBeanAutowiringInterceptor.class) annotation. The problem is that I keep on getting the NoSuchBeanDefinitionException for the autowired classes when the EJBs are instantiated. (The Filter class in my example).
I'm using Java EE 6 application (EJB 3.0), Spring 4.2.2 managed with Maven and running in a WebSphere 7 AS.
I have the following Spring dependencies:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.ldap</groupId>
<artifactId>spring-ldap-core</artifactId>
<version>2.0.4.RELEASE</version>
<exclusions>
<exclusion>
<artifactId>spring-tx</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
Implementation:
#Stateless
#Remote(ServiceRemote.class)
#Interceptors(SpringBeanAutowiringInterceptor.class)
public class Service implements ServiceRemote {
#Autowired
private Filter filter;
...
}
Class to be autowired
#Component
public class FilterImpl implements Filter { ... }
The beanRefContext.xml which SpringBeanAutowiringInterceptor will look for:
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="businessBeanFactory"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="classpath*:applicaion-context.xml" />
</bean>
</beans>
The application-context.xml with the bean definitions:
<?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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean class="com.FilterImpl" />
</beans>
Both XMLs are in the classpath, as they are in src/main/resources/.
I've tried some debugging with the following code, but couldn't find something useful.
BeanFactoryLocator factoryLocator = ContextSingletonBeanFactoryLocator.getInstance("classpath:beanRefContext.xml");
BeanFactoryReference ref = factoryLocator.useBeanFactory("businessBeanFactory");
BeanFactory factory = ref.getFactory();
FilterImpl instance = factory.getBean(FilterImpl.class);
Both contexts appear to be loaded, as I get logs like the following, when the EJB is instantiated for the first time.
org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#7730773; root of context hierarchy
org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions Loading XML bean definitions from URL [file:/C:/project/service-module/target/classes/beanRefContext.xml]
org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#5580558: root of context hierarchy
But after that Spring can't find the bean I defined in the application-context.xml.
In application-context.xml I've tried using <context:component-scan base-package="com"/> instead of the <bean class="com.FilterImpl" />, but I got the same result.
Am I missing some configuration?
Edits: Added suggestion by Steve C and logs of context loading.
Move your xml configuration files from src/main/java to src/main/resources.
Files in src/main/java are only processed by the maven-compiler-plugin.
Additional resource files are processed by the maven-resources-plugin which copies files from src/main/resources.
It seems that you are not adding <context:annotation-config/> in your spring xml file
I've just found the answer. In the <constructor-arg value="classpath*:applicaion-context.xml" /> there is an applicaion-context.xml when it should be application-context.xml. When I edited the question and posted the logs about the loading of the contexts, I noticed that only beanRefContext.xml had its definition loaded. There was no logs about some parsing error and I just ignored this possibility. Everything is working now.

Combining Spring project and Jersey

I've built a project with Spring JPA, and now I want to use it in my Jersey project.
I've added my SJPA project as a dependency in my pom.xml
I would like to use my service classes from my SJPA when I use GET/POST/PUT/DELETE methods.
Is there an easy way to do this with annotations? Or do I have to get AnnotationConfigApplicationContext in each class? Feels kind of waste.
#Path("/users")
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public final class UserResource
{
private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
private PodcastService service;
#GET
public Response getAllPodcasts() {
context.scan("org.villy.spring.service");
context.refresh();
service= context.getBean(PodcastService.class);
return Response.ok(service.findAll()).build();
}
}
NOTE: The linked example projects below are from the Jersey master branch, which is currently a snapshot of Jersey 3, which is not yet released. Jersey 3 will be using Spring 4, so you may notice a dependency jersey-spring4. This dependency does not exist yet, as Jersey 3 is not yet released (probably not for a while). So the dependency to use is jersey-spring3. All the example should still work the same, just changing that one dependency. If you want to use Spring 4, see the dependencies listed in the example pom below in this answer
You don't need to create the ApplicationContext where you need the service. You should be able to configure a global one. Jersey has a module for this that integrates the two frameworks. This allows you to simply #Autowired all your Spring services into your Jersey resource classes.
Instead of trying to produce any example, I will just link to the official examples. They are straight from the projects, so the links should be good for some time. Take special not of the Maven dependencies. You will need to make sure to have them for the example to work.
Jersey 2.x with Spring XML config
Jersey 2.x with Spring Java config [1]
Jersey 1.x with Spring XML config
Jersey 2.x with Spring Boot
Note: The ${spring3.version} version in the examples is 3.2.3.RELEASE. It's possible to use Spring 4 with the examples, but you will need to make sure to exclude all the Spring transitive dependencies from the jersey-spring3 dependency.
[1] - One thing to note about the Java config example is that it uses a standalone app. To use Java config in a webapp requires a bit of trickery. This is a known bug where Jersey looks for an param contextConfigLocation with the location of the applicationContext.xml file and will throw an exception when it doesn't find one.
I've found a few ways around this.
An example of this was mentioned by the person who raised the issue. You can create a Spring web initializer where you configure the spring context and override the param property. (See full example here).
#Order(1)
public class SpringWebContainerInitializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
registerContextLoaderListener(servletContext);
// Set the Jersey used property to it won't load a ContextLoaderListener
servletContext.setInitParameter("contextConfigLocation", "");
}
private void registerContextLoaderListener(ServletContext servletContext) {
WebApplicationContext webContext;
webContext = createWebAplicationContext(SpringAnnotationConfig.class);
servletContext.addListener(new ContextLoaderListener(webContext));
}
public WebApplicationContext createWebAplicationContext(Class... configClasses) {
AnnotationConfigWebApplicationContext context;
context = new AnnotationConfigWebApplicationContext();
context.register(configClasses);
return context;
}
}
You could simply add the applicationContext.xml to the classpath and just register the spring Java configuration class as a bean
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="config" class="com.your.pkg.SpringAnnotationConfig"/>
</beans>
There's another way I can think of, but I've save that for a time I can actually test it out.
UPDATE
"Failed to read candidate component class ... ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet"
Seems to be related to this, using Spring 3 with Java 8. Like I said, if you want to use Spring 4, you will need to exclude the Spring transitive dependencies from jersey-spring3 and just change the version of your explicitly declared Spring dependencies. Here is an example, that I tested and works.
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.0.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.0.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.1</version>
</dependency>

Spring #Value annotation not working with mockito mock

I am using spring #Value annotation and setting values for some fields in class A.
I am writing unit tests for this class A. In the test class, I am annotating the reference for class A with Mockito #Spy. I am setting the values as system properties and then invoking MockitoAnnotations.initMocks(this).
My expectation is the spied object will have the fields initialized with the values from system properties via #Value annotation. But this is not happening.
Please anybody could explain?
I have a similar test and I'm using the following relevant code:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations="/context.xml")
public class ContextTest {
private boolean started = false;
#Spy
#Autowired
private Baz baz;
#Before
public void before() {
if (!started) {
MockitoAnnotations.initMocks(this);
started = true;
}
}
#Test
public void spy() {
Assert.assertEquals("value", baz.getProperty());
Mockito.verify(baz).getProperty();
}
}
Basically it will let spring process the test annotations (due to SpringJUnitRunner) and afterwards let Mockito process them (explicitly invoked MockitoAnnotations.initMocks(instanceOfTestClass)).
Other files to have a complete test
simple Baz.java spring class:
package foo.bar;
import org.springframework.beans.factory.annotation.Value;
public class Baz {
#Value("${property:test}")
private String property;
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
}
the context.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>my.properties</value>
</property>
</bean>
<bean id="baz" class="foo.bar.Baz" />
</beans>
my.property file:
property=value
and the maven (pom.xml) file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring-test</groupId>
<artifactId>my.spring.test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
the file structure is:
+ spring-test
- pom.xml
+ src/main/java
+ foo.bar
- Baz.java
+ src/main/resources
- context.xml
- my.properties
+ src/test/java
+ foo.bar
- ContextTest.java
Mockito is not Spring aware! And will never be! You'll always have to init these kind of injection yourself as it is not pure java.
However you can take a look at springockito, it is a spring extension that enable some interesting usage of Mockito with Spring. But you'll have to create a Spring context for the test.
Note that using a spring context in a JUnit test is like crafting an integration test.

java.lang.IllegalArgumentException: warning no match for this type name: ru.sbt.filial.cards.aspect.SomeBean [Xlint:invalidAbsoluteTypeName]

I never used Spring AOP and trying to configure my first bean. It seems that I configured it properly, but I get an exception that the bean is not found.
My aspect is –
#Aspect
#Component
public class IdentificationAspect {
#Before("execution(* ru.sbt.filial.cards.aspect.SomeBean.*(..))")
public void logBefore(JoinPoint joinPoint) throws Throwable {
System.out.println("logBefore() is running!");
System.out.println("hijacked : " + joinPoint.getSignature().getName());
System.out.println("******");
}
}
And my bean that AOP doesn't find is -
package ru.sbt.filial.cards.aspect;
import org.springframework.stereotype.Component;
#Component
public class SomeBean {
public void printSmth() {
System.out.println("!!!!!!!!!!!");
}
}
I've got the following exception -
Caused by: java.lang.IllegalArgumentException: warning no match for this type name: ru.sbt.filial.cards.aspect.SomeBean [Xlint:invalidAbsoluteTypeName]
at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:301)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:207)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.getFallbackPointcutExpression(AspectJExpressionPointcut.java:358)
at org.springframework.aop.aspectj.AspectJExpressionPointcut.matches(AspectJExpressionPointcut.java:255)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:208)
at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:262)
at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:294)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:117)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:87)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:68)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:356)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:319)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:412)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.postProcessObjectFromFactoryBean(AbstractAutowireCapableBeanFactory.java:1629)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:162)
... 165 more
I have following maven dependencies -
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.3</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.3</version>
</dependency>
And my spring applicationContext.xml configuration is -
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<mvc:annotation-driven/>
<aop:aspectj-autoproxy/>
<bean id="someBean" class="ru.sbt.filial.cards.aspect.SomeBean">
</bean>
UPDATE
I added to my Spring configuration line
<context:component-scan base-package="ru.sbt.filial.cards.aspect"/>
but still same message. I also tried to annotate different ways - if I don't specify the bean i.e. write -
#Before("execution(* ru.sbt.filial.cards.aspect.*.*(..))")
instead
#Before("execution(* ru.sbt.filial.cards.aspect.SomeBean.*(..))")
I have no error on loading, but aop method is not invoked.
I also tried to annotate like this
#Before("this(ru.sbt.filial.cards.aspect.SomeBean) and execution(* printSmth(..))")
but with same result - no match for this type name. Any more ideas??
I have the same problem as you and I've solved mine. I give you two suggestions, you can try it out.
Please modify the #Before annotation in the aspect class as one of the following:
#Before("execution(* ru.sbt.filial.cards.aspect.SomeBean.logBefore(..))")
or
#Before("execution(* ru.sbt.filial.cards.aspect.SomeBean..*(..))")
Hope it helps.
you lose the class.
#Before("execution(* ru.sbt.filial.cards.aspect.SomeBean.*(..))")
In your way, "somebean" is a package, the * after it is a method, and you should put classes
between them.
You can do it like
#Before("execution(* ru.sbt.filial.cards.aspect.SomeBean.* .*(..))")
or
#Before("execution(* ru.sbt.filial.cards.aspect.SomeBean..*(..))")
To run successfully, In your spring applicationContext.xml configuration file add
<context:component-scan base-package="ru.sbt.filial.cards.aspect"/>
As you used #Component annotation in SomeBean class
Update:
try by adding method name to #Before:
#Before("execution(* ru.sbt.filial.cards.aspect.SomeBean.logBefore(..))")

Categories

Resources