Configure JDeveloper 11g to work with Spring 2.5 and AOP

In a recent article I described the interaction between JavaServer Faces (1.2) and Spring Framework (2.5.x): https://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.

Configure JDeveloper 11g to work with Spring 2.5 and AOP

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

Configure JDeveloper 11g to work with Spring 2.5 and AOP

 

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">
   <container-descriptor>
      <prefer-web-inf-classes>true</prefer-web-inf-classes>
   </container-descriptor>
</weblogic-web-app>

 

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

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/my-other-managed-beans.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

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

<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee">
    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>
</faces-config>

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("HelloWorld");
  }
}

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

<bean id="someBean" class="nl.amis.simplejsf.MyBean"/

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("Original Return Value "+result);
     result.reverse();
  }
}


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

<bean id="gotchaBean" class="nl.amis.simplejsf.aop.Gotcha"/>

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

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


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

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">


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:

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

13. Run the application on the integrated WebLogic Server.

Configure JDeveloper 11g to work with Spring 2.5 and AOP

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/

One Response

  1. Ashwin Almeida March 2, 2011