You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Robby Pelssers <ro...@ciber.com> on 2009/10/06 15:58:49 UTC

RE: using Spring AOP in cocoon block [SOLVED]

Ok... so here is what I did to get a typical Core Spring AOP example
working in a Cocoon 2.2 block.

1) generate the block using the Cocoon archetype.
2) add the necessary dependencies to the pom.xml

    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.6.4</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.4</version>
    </dependency>
    
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib-nodep</artifactId>
      <version>2.2</version>
    </dependency>
3) run "mvn eclipse:eclipse"
4) import the project in eclipse
5) Create an Aspect which logs if the message on the sample MyBean.java
included in the block archetype is set
------------------------------------------------------------------------
------------------------------------
package com.ciber;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class PropertyChangeTracker {

	private Logger logger = Logger.getLogger(getClass());
	
	@Before("execution(void com.ciber.MyBean.set*(*))")
	public void trackChange(JoinPoint point) {
		String name = point.getSignature().getName();
		Object newValue = point.getArgs()[0];
		logger.info(name + " about to change to " + newValue + "
on " + point.getTarget());
	}
}
------------------------------------------------------------------------
------------------------------------

6) add an aspects.xml to META-INF/cocoon/spring
------------------------------------------------------------------------
------------------------------------
<?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:p="http://www.springframework.org/schema/p"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:util="http://www.springframework.org/schema/util"
  xmlns:pipeline="http://cocoon.apache.org/schema/pipeline"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd
  http://cocoon.apache.org/schema/pipeline
http://cocoon.apache.org/schema/pipeline/cocoon-pipeline-1.0.xsd">
 
    <aop:aspectj-autoproxy>
      <aop:include name="propertyChangeTracker"/>
    </aop:aspectj-autoproxy>

    <bean id="propertyChangeTracker"
class="com.ciber.PropertyChangeTracker"/>
</beans>
------------------------------------------------------------------------
------------------------------------

7) change /COB-INF/flow/spring-bean.js and set the message so we can see
if our aspect is invoked

function demo() {
    var demoBean = cocoon.getComponent("demo");
    
    demoBean.setMessage("Changed message from flowscript ;-)");
    cocoon.sendPage("screens/spring-bean",
        {
            "message" : demoBean.message
        }
    );
}


I changed logger.info() to System.out.println() to do a quick test and
the output was what I'd expect:

setMessage about to change to Changed message from flowscript ;-) on
com.ciber.MyBean@16b4e30



Nice stuff.... 

Finally being able to play around with AOP in cocoon ;-)

Cheers,
Robby Pelssers



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org