Spring AOP and AspectJ Integration with EJB3
Hi,
Can anyone give me an headstart on how to integrate Spring AOP ans AspectJ in EJB3.
So far I have figured out the following related to this.
EJB Stateless Session Bean. I am injecting the SaveCreditCardInfoDAO bean into my Controller EJB. But while I am calling the method using the instance I have injected, nothing is getting logged.
Code:
/**
* Session Bean implementation class Controller
*/
@Stateless(name="Controller")
@TransactionManagement(TransactionManagementType.CONTAINER)
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class Controller implements ControllerRemote, ControllerLocal {
@Resource(name=CCSConstants.CCS_DATASOURCE_JNDI)
DataSource ccsDataSource;
@Resource
private SessionContext ejbContext;
@Autowired
private SaveCardInfoDAO saveCardInfo;
/**
* Default constructor.
*/
public Controller() {
// TODO Auto-generated constructor stub
}
/**
* Exposed Message to insert the Credit Card Info into the database.
*/
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public void insertCardInfo(CreditCardDO ccdo){
//SaveCardInfoDAO saveCardInfo = new SaveCardInfoDAO(ccsDataSource);
try{
saveCardInfo.setDataSource(ccsDataSource);
saveCardInfo.openConnection(false);
saveCardInfo.insertCardInfo(ccdo);
System.out.println("Insert Success !!!");
}
catch(SQLException e){
ejbContext.setRollbackOnly();
System.out.println("Insert Failure !!!");
}
finally{
saveCardInfo.closeConnection();
}
}
}
beanRefContext.xml
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="factoryKey"
class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg value="aopContext.xml" />
</bean>
</beans>
aopContext.xml
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.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<aop:aspectj-autoproxy />
<bean id="creditCardAdvice" class="com.fedex.ccs.advice.CCSCreditCardInfoAdvice" />
<!-- <bean id="creditCardDO" class="com.fedex.ccs.domain.CreditCardDO" /> -->
<bean id="saveCardInfo" class="com.fedex.ccs.dao.SaveCardInfoDAO" />
</beans>
Advice Class
Code:
@Aspect
public class CCSCreditCardInfoAdvice {
private static CCSLoggerInterface myCcsLogger =
CCSLogger.getLogger(CCSCreditCardInfoAdvice.class);
/**
* Constructor
*/
public CCSCreditCardInfoAdvice(){}
// All the methods within com.fedex.bean package, with any return type
// belong to the Pointcut humanMethods()
@Pointcut("execution(* *.*.*(..))")
public void creditCardMethods(){}
// Method to be executed before the respective Pointcut method is run.
@Before("creditCardMethods()")
public void logBeforeMessage(JoinPoint jp) throws Throwable {
// TODO Auto-generated method stub
myCcsLogger.info("************* Method Before '"+jp.getSignature().getName()+"' Called **************");
}
// Method to be executed after the respective Pointcut method is run.
@After("creditCardMethods()")
public void logAfterMessage(JoinPoint jp) throws Throwable {
// TODO Auto-generated method stub
myCcsLogger.info("************* Method After '"+jp.getSignature().getName()+"' Called **************");
}
@Around("creditCardMethods()")
public Object logAroundMessage(ProceedingJoinPoint pjp) throws Throwable {
// TODO Auto-generated method stub
myCcsLogger.info("************* Method '"+pjp.getSignature().getName()+"' Getting Called **************");
Object output = pjp.proceed();
myCcsLogger.info("************* Method Output is '"+output+"' **************");
myCcsLogger.info("************* Method '"+pjp.getSignature().getName()+"' Completed **************");
return output;
}
}
ApplicationUtil Class used to load the logger xml file.
Code:
public class ApplicationUtil {
static{
init();
}
public static void init(){
DOMConfigurator.configure("resource/ccslogger.xml");
}
}
When I run my client it gives me following error.
Code:
Caused By: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.context.support.ClassPathXmlApplicationContext]: Constructor threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'creditCardAdvice' defined in class path resource [aopContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.fedex.ccs.advice.CCSCreditCardInfoAdvice
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:108)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:280)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1003)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:907)
Truncated. see log file for complete stacktrace
Caused By: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'creditCardAdvice' defined in class path resource [aopContext.xml]: Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.fedex.ccs.advice.CCSCreditCardInfoAdvice
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
Truncated. see log file for complete stacktrace
Caused By: java.lang.NoClassDefFoundError: Could not initialize class com.fedex.ccs.advice.CCSCreditCardInfoAdvice
at sun.reflect.GeneratedConstructorAccessor89.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:74)
Truncated. see log file for complete stacktrace
If I remove following line from aopContext.xml then the application runs fine but does not log anything.
Code:
<bean id="creditCardAdvice" class="com.fedex.ccs.advice.CCSCreditCardInfoAdvice" />
All these files and classes are in the same Eclipse project. Only SaveCreditCardInfoDAO is in different project but still its instance is getting injected into the EJB.
Please advice !!!
Regards
Damodar