JBoss Microcontainer with JBoss AOP in a standalone app.
pekarna Nov 8, 2009 10:24 PMHi,
I'm trying to create a standalone app using JBoss Microcontainer for IoC and JBoss AOP for, well, AOP.
I've boot-strapped, deployed a descriptor with AOP XML, so far so good.
But the aspect is not performed. Do I need to enable AOP plugin ors something?
Please check the code below.
Thanks for help.
<deployment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:jboss:bean-deployer:2.0 bean-deployer_2_0.xsd" xmlns="urn:jboss:bean-deployer:2.0" xmlns:aop="urn:jboss:aop-beans:1.0"> <bean name="myGarage" class="cz.zizka.ondra.jbmctest.Garage"> <property name="car"> <bean name="myCar" class="cz.zizka.ondra.jbmctest.Car"> <property name="name">Red Devil</property> </bean> </property> </bean> <aop:interceptor name="FuelInterceptor" class="cz.zizka.ondra.jbmctest.FuelInterceptor"/> <aop:bind pointcut="execution(* cz.zizka.ondra.jbmctest.Car->set*Fuel(..)"> <aop:interceptor-ref name="FuelInterceptor"/> </aop:bind> </deployment>
public static void main( String[] args ) throws Throwable { // Bootstrap. BasicBootstrap bootstrap = new BasicBootstrap(); bootstrap.run(); // Load the bean definitions. URL url = Thread.currentThread().getContextClassLoader().getResource("jboss-beans.xml"); BasicXMLDeployer deployer = new BasicXMLDeployer( bootstrap.getKernel() ); deployer.deploy( url ); KernelController cont = bootstrap.getKernel().getController(); ControllerContext context = cont.getInstalledContext("myGarage"); System.out.println( "I have a garage: "+context.getTarget()); // Put some fuel in. Car myCar = (Car) cont.getInstalledContext("myCar").getTarget(); myCar.setLitresOfFuel( myCar.getLitresOfFuel() + 1 ); System.out.println( "I have a garage: "+context.getTarget()); deployer.shutdown(); }
public class FuelInterceptor implements Interceptor { private static final Logger log = Logger.getLogger( FuelInterceptor.class.getName() ); public Object invoke(Invocation invocation) throws Throwable { System.out.println( "In FuelInterceptor." ); if( true ) throw new Exception(); // To see that it's really NOT called. Object target = invocation.getTargetObject(); long time = System.currentTimeMillis(); log.info("Invocation [" + target + "] start: " + time); try{ return invocation.invokeNext(); } finally{ log.info("Invocation [" + target + "] time: " + (System.currentTimeMillis() - time)); } } public String getName() { return FuelInterceptor.class.getName(); } }// class FuelInterceptor