Configure JDeveloper 11g to work with Spring 2.5 and AOP

1

In a recent article I described the interaction between JavaServer Faces (1.2) and Spring Framework (2.5.x): http://technology.amis.nl/blog/6655/spring-a-surprise-on-a-jsf-developer-how-spring-beans-can-become-jsf-managed-beans. I created a JDeveloper 11g web application that I ran on the integrated WebLogic Server 11g (10..3.2). In this article I will explain the configuration steps I had to go through for making JDeveloper and WebLogic run my simple JSF/Spring application.

1. Create a new generic JDeveloper application; set the project name and add the JSF library

2. Install the JDeveloper Spring extension through the Check for Updates facility under Help in the main menu

this downloads all Spring Framework libraries to your disk, creates the Spring Library in JDeveloper, adds the XSDs for Spring configuration files and adds the option create New Spring Bean Configuration to the New Gallery.

The Spring 2.5 library has become available after installing the Spring Extension:

 

3. Create a new Spring Context from the New Gallery under Business Tier, Spring 2.5.6

4. Edit the empty Spring context configuration:

I removed what was created and pasted in:

<?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-2.0.xsd ">
</beans>

Before I made this change, I seemed to run into SAX parse errors complaining about the fact that the beans element could not be found.

5. Copy spring.jar to WEB-INF/lib – as there was either no version of Spring on the integrated WebLogic Server at all or the version that was there did no match the one I had used

(before I added the spring.jar, I had deployment errors like: 

Unable to find class 'org.springframework.web.jsf.el.SpringBeanFacesELResolver'

)

6. Update weblogic.xml in order to have WebLogic Server prefer the JARs provided in the application's WEB-INF/lib over any libraries pre-installed on WLS:

<?xml version = '1.0' encoding = 'windows-1252'?>
<weblogic-web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd"
                  xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app">
<strong>   &lt;container-descriptor&gt;
      &lt;prefer-web-inf-classes&gt;true&lt;/prefer-web-inf-classes&gt;
   &lt;/container-descriptor&gt;
</strong>&lt;/weblogic-web-app&gt;

 

7. Modify web.xml to make it initialize the Spring bean context

  &lt;context-param&gt;
    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
    &lt;param-value&gt;/WEB-INF/my-other-managed-beans.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  &lt;listener&gt;
    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;

8. Modfy faces-config.xml to ensure that EL expressions will also recognize Spring beans as 'managed beans'

&lt;faces-config version=&quot;1.2&quot; xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;&gt;
    &lt;application&gt;
        &lt;el-resolver&gt;org.springframework.web.jsf.el.SpringBeanFacesELResolver&lt;/el-resolver&gt;
    &lt;/application&gt;
&lt;/faces-config&gt;

9. Create the class MyBean; configure the Spring Bean for that class

package nl.amis.simplejsf;

public class MyBean {

  public StringBuffer getProperty() {
    return new StringBuffer(&quot;HelloWorld&quot;);
  }
}

Add a bean configuration based on MyBean to the file my-other-managed-beans.xml in WEB-INF

&lt;bean id=&quot;someBean&quot; class=&quot;nl.amis.simplejsf.MyBean&quot;/

10. Create the class Gotcha, configure a Spring Bean for it

package nl.amis.simplejsf.aop;

public class Gotcha  {

  public void reverseResult(StringBuffer result) {
     System.out.println(&quot;Original Return Value &quot;+result);
     result.reverse();
  }
}


Add bean configuration in my-other-managed-beans.xml:

&lt;bean id=&quot;gotchaBean&quot; class=&quot;nl.amis.simplejsf.aop.Gotcha&quot;/&gt;

11. Configure AOP: the after-returning advise on any method on any class that returns a StringBuffer result, based on the Gotcha class's reverse method

  &lt;aop:aspectj-autoproxy/&gt;
  &lt;!-- proxy-target-class forces the use of CGLIB proxies, which allows proxying classes in addition to interfaces.--&gt;
  &lt;aop:config proxy-target-class=&quot;true&quot;&gt;
    &lt;aop:pointcut id=&quot;someBeanPc&quot; expression=&quot;bean(someBean)&quot;/&gt;
    &lt;aop:aspect id=&quot;upperCaserAspect&quot; ref=&quot;gotchaBean&quot;&gt;
      &lt;aop:after-returning pointcut=&quot;execution(java.lang.StringBuffer *(..))&quot;
                           method=&quot;reverseResult&quot; returning=&quot;result&quot;
                           arg-names=&quot;result&quot;/&gt;
    &lt;/aop:aspect&gt;
  &lt;/aop:config&gt;


Note: in order to be able to successfullt use the aop namespace prefix, also modify the beans root element to:

&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
       xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;
       xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
       xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd&quot;&gt;


10. In order to use these AOP facilities, we need to add the AspectJ library to the application

Download AspectJ from http://www.eclipse.org/aspectj/downloads.php and download aspectj-1.6.6.jar (or whatever is the latest release at this moment).

Extract the file aspectjweaver.jar from the aspectj.jar that was downloaded, and copy it to WEB-INF/lib.

11. AspectJ has a dependency on CGLib (at least under special circumstances: this is necessary to proxy classes, rather than interfaces. CGLIB is used by default if a business object does not implement an interface. ) 

I first downloaded cglib.jar from http://cglib.sourceforge.net/ and copied it to WEB-INF/lib. It turned out CGLib has its own dependenies when I tried to run the application:

java.lang.ClassNotFoundException: org.objectweb.asm.Type

There is a CGLib jar without dependencies, so I downloaded that one: cglib-nodeps.jar and added it to WEB-INF/lib. Then I removed cglib.jar.

12. Create simple JSF page with reference to someBean:

&lt;?xml version=&#39;1.0&#39; encoding=&#39;windows-1252&#39;?&gt;
&lt;jsp:root xmlns:jsp=&quot;http://java.sun.com/JSP/Page&quot; version=&quot;2.1&quot;
          xmlns:f=&quot;http://java.sun.com/jsf/core&quot;
          xmlns:h=&quot;http://java.sun.com/jsf/html&quot;&gt;
  &lt;jsp:output omit-xml-declaration=&quot;true&quot; doctype-root-element=&quot;HTML&quot; doctype-system=&quot;http://www.w3.org/TR/html4/loose.dtd&quot; doctype-public=&quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;/&gt;
  &lt;jsp:directive.page contentType=&quot;text/html;charset=windows-1252&quot;/&gt;
  &lt;f:view&gt;
    &lt;html&gt;
      &lt;head&gt;
        &lt;meta http-equiv=&quot;Content-Type&quot;
              content=&quot;text/html; charset=windows-1252&quot;/&gt;
        &lt;title&gt;ASimplePage&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
        &lt;h:form&gt;
          <strong>&lt;h:outputText value=&quot;#{someBean.property}&quot;/&gt;</strong>
        &lt;/h:form&gt;
      &lt;/body&gt;
    &lt;/html&gt;
  &lt;/f:view&gt;
&lt;/jsp:root&gt;

13. Run the application on the integrated WebLogic Server.

Resources

Download SimpleJSF.zip – the entire JDeveloper 11g application with the required JARs.

Spring Framework Documentation: http://static.springsource.org/spring/docs/2.0.x/reference/index.html.

AspectJ project site: http://www.eclipse.org/aspectj.

CGLib project site : http://cglib.sourceforge.net/

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

1 Comment

  1. Ashwin Almeida on

    Hi  Lucas,
    How can we integrate AspectJ with ADF without Spring AOP. Could you provide more input into this.
     
    Thanks
    Ashwin