Friday, October 21, 2011

JSF 2.0, Spring 3.0, JSR 299 and Weblogic 10.3.5

Unfortunately  Oracle Weblogic 10.3.5 is not fully JEE6 compatible server yet and the challenge for us was to develop new project that needs be deployed in OWS and the client is not sure of upgrade path yet.  So to make our project inline with JEE6 web profile we decided to use Spring DI internally uses JSR 299 and later it would be easy for us to re-factor our code if required since Spring DI could be optional with JEE6 as CDI and DI is provided out of the box by the web profile. The real decision for us is to use limited number of @Annotations that the developer has to worry about and also to be consistent and just us @Named, @Inject and @Scope instead of many other available annotations. Below maven file has the versions that we used to setup our project.

POM file


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.dvmr.poc</groupId>
<artifactId>jsfdar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>jsf-dar</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jsf.version>2.0.6</jsf.version>
<spring.version>3.0.5.RELEASE</spring.version>
<slf4j.version>1.6.1</slf4j.version>
</properties>

<dependencies>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- JSF -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>${jsf.version}</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>${jsf.version}</version>
</dependency>
<!-- slf4j/log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
<classifier>sources</classifier>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0.1</version>
</dependency>

<!-- Unit Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
                <!-- Add JAR to your local repository as this driver not found-->
<dependency>
<groupId>com.oracle</groupId>
 <artifactId>oracle</artifactId>
 <version>11.2.0.2.0</version>
</dependency>
<!-- Spring Inject -->
<dependency>
   <groupId>javax.inject</groupId>
   <artifactId>com.springsource.javax.inject</artifactId>
   <version>1.0.0</version>
</dependency>
</dependencies>
</project>



Simple JSF Managed Bean HelloWorld.java with JSF 2.0 Annotations


import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;

import com.dvmr.poc.service.ProjectsService;

@ManagedBean
@Scope("request")
public class HelloWorld {
private String hello;

@ManagedProperty(value="#{projectsService}")
private ProjectsService projectsService;

      //additional details .......
}

Now it becomes inline with JEE6 however the @Named and @Inject are using annotations form com.springsource.javax.inject-1.0.0.jar

import javax.inject.Inject;
import javax.inject.Named;

import com.dvmr.poc.service.ProjectsService;

@Named
@Scope("request")
public class HelloWorld {
private String hello;

@Inject
private ProjectsService projectsService;

        //additional details .......
}


Spring applicationContext.xml


<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.dvmr.poc"/>

</beans>


JSF faces-config.xml


<?xml version="1.0" encoding="utf-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
              version="2.0">
    <application>
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>
</faces-config>

Make sure you update the web.xml  with ContextLoaderListener to load Spring context files.

That's it and you are good go now you can deploy it in weblogic 10.3.5 and using JEE6 annotations. How ever I really want to see weblogic 12g soon.

I will try to upload the project in github very soon :-)


PF