Spring AOP - Standalone application but without Spring DI
Hi all,
I am working since 3 year on a tag & rename standalone (Swing) application for audio files with Discogs integration. Now I want to explore some more in aspects and aop...
I want to use Spring AOP in my application because it can add some good functionality in my oppinion like logging, would love to have that working with AOP and security also... anyways I don't use Spring for managing my beans... because I do that myself. I tried to use Spring DI but I couldn't build a proper executable jar because Spring threw weird exceptions I have never seen before... After few weeks trying I postponed it because of a deadline ... So for now I try to make it work without Spring managing my beans and instances ...
My question is, can I use beans I manage myself in combination with the Spring AOP facilities ?
I have a log aspect:
Code:
@Aspect
public class CoreLogAspect {
private static final Logger LOGGER = LoggerFactory.getLogger(CoreLogAspect.class);
public CoreLogAspect() {
super();
}
@Around("execution(* be.elitetagger.core.*(..))")
public void logAround(ProceedingJoinPoint joinPoint) throws Throwable {
LOGGER.trace("ENTRY - method ...");
LOGGER.trace("***** - Hijacked method: " + joinPoint.getSignature().getName());
LOGGER.trace("***** - Hijacked arguments: " + Arrays.toString(joinPoint.getArgs()));
joinPoint.proceed();
LOGGER.trace("LEAVE - method ...");
}
}
and a spring-aop.xml file on classpath (spring/spring-aop.xml):
HTML Code:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd ">
<aop:aspectj-autoproxy/>
<bean id="coreLogAspect" class="be.elitetagger.core.aspect.CoreLogAspect"/>
</beans>
and a config reader class to load it:
Code:
package be.elitetagger.core.aop;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import be.elitetagger.core.exception.ExceptionHandler;
public class SpringAopConfigReader implements AopConfigReader {
private static final Logger LOGGER = LoggerFactory.getLogger(SpringAopConfigReader.class);
private static final String DEFAULT_SPRING_CONTEXT_PATH = "spring/spring-aop.xml";
private ApplicationContext springApplicationContext;
public ApplicationContext getSpringApplicationContext() {
return springApplicationContext;
}
public SpringAopConfigReader() {
super();
}
@Override
public boolean readConfiguration() {
return readConfiguration(DEFAULT_SPRING_CONTEXT_PATH);
}
@Override
public boolean readConfiguration(String strContextPath) {
LOGGER.debug("Reading Spring AOP configuration ...");
try {
springApplicationContext = new ClassPathXmlApplicationContext(strContextPath);
} catch(BeansException ex) {
LOGGER.error(ExceptionHandler.getStackTrace(ex), ex);
return false;
}
return true;
}
}
When I initialize my application, the aop.xml file is parsed successfully and aspect is initialized. No errors on loading the xml file..
Now for me, I think it's logic because Spring normally auto proxy the beans that are managed by Spring container.
Now is it possible to use Spring AOP facilities but without Spring managing my beans ? Perhaps a weird question ??? ;) I use Spring a lot at work in web applications but I have no clue how to integrate it well in a standalone application... The component scanning in standalone applications doesn't work as well as in web applications. Still love spring a lot ! Respect..
Thanks in advance ! :)
Greets,
Sikke
forgot to mention, I use Spring v3.1
PS: For those who are interested => www.facebook.com/elitetaggerpro (win only, for now) :)