Spring

Spring Basic Demo

Jay SenSharma

Jay SenSharma

Few More Demos Here: (Binding/Accessing JNDI names Using Spring Framework)

Here is a very simple example which demonstrates us …How to lookup in the JNDI tree of WebLogic Server 10.3 using the Spring Framework. To run this example you need to download at least two JARs “Spring.jar” and “spring-jdbc.jar”. The best way to get these JARs is to browse to Spring framework website. But I have a very simple way for you to get these jars …please refer to http://findjar.com/ and just specify the JAR file name there  or if you remember any of the class name which is there inside these JAR then that will also work for you.

Once you have done with downloading with these JARs then start writing the Spring Bean class.

Step 1).  Writing the Spring Bean class.

package pack;
import java.io.Serializable;
public class EmployeeBean implements Serializable
{
private static final long serialVersionUID = 1L;
private int empNo;
private String empName;

public EmployeeBean()
{
System.out.println(“\n\t EmployeeBean Object Instantiated”);
}

public void init()
{
System.out.println(“\n\t init() of EmployeeBean”);
empNo=1111;
empName=”AAAA”;
}

public int getEmpNo() {
System.out.println(“\t\tgetEmpNo()”);
return empNo;
}

public void setEmpNo(int empNo) {
this.empNo = empNo;
System.out.println(“\t\tsetEmpNo()”);
}

public String getEmpName() {
System.out.println(“\t\tgetEmpName()”);
return empName;
}

public void setEmpName(String empName) {
System.out.println(“\t\tsetEmpName()”);
this.empName = empName;
}

public String toString() {
return “\n<BR>*********************\n<BR>Emp Name: ” + empName + “\n<BR>Emp Num: ” + empNo  ;
}
}

Step 2). Now we need to write the Spring Configuration XML file, here we need to define the Bean information’s like following


<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE beans PUBLIC “-//SPRING//DTD BEAN//EN” “<a href="http://www.springframework.org/dtd/spring-beans.dtd">http://www.springframework.org/dtd/spring-beans.dtd</a>“>

<beans>
<bean id=”employee”  init-method=”init”>
<property name=”empNo” value=”1000″/>
<property name=”empName” value=”Jack”/>
</bean>

<bean id=”dataSource”>
<property name=”jndiName”>
<value>jdbc/MyDS</value>
</property>
</bean>
</beans>

Step3). Now we need to provide the mapping of the Spring Configuration file into the web.xml file


<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>” xmlns=”<a href="http://java.sun.com/xml/ns/javaee">http://java.sun.com/xml/ns/javaee</a>” xmlns:web=”<a href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</a>” xsi:schemaLocation=”<a href="http://java.sun.com/xml/ns/javaee">http://java.sun.com/xml/ns/javaee</a> <a href="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd</a>” id=”WebApp_ID” version=”2.5″>
<display-name>SpringOne</display-name>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>

<servlet>
<servlet-name>MyJNDIServlet</servlet-name>
<servlet-class>servlets.MyJNDIServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyJNDIServlet</servlet-name>
<url-pattern>MyJNDIServlet</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>SpringContextServlet</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

Step 4). No we need to create a simple Servlet using Spring APIs to perform a JNDI lookup on the Server for the a DataSource which has the JNDI Name as “jdbc/MyDS” Object.


package servlets;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.net.URL;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class MyJNDIServlet extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
PrintWriter out=res.getWriter();
try
{
URL url=Thread.currentThread().getContextClassLoader().getResource(“applicationContext.xml”);
System.out.println(“\n\n\t URL: “+url);
String file=url.getFile();
System.out.println(“\n\n\t FILE: “+file);

BeanFactory factory = new XmlBeanFactory(new FileSystemResource(file));
javax.sql.DataSource bean=( javax.sql.DataSource)factory.getBean(“dataSource”);
out.println(“<h3>org.springframework.jndi.JndiObjectFactoryBean (dataSource) Bean: “+bean);
System.out.println(“\n\torg.springframework.jndi.JndiObjectFactoryBean (DataSource)Bean: “+bean);
out.println(“<h4>GOT the CONNECTION :=> ds.getConnection() : “+bean.getConnection());
//System.out.println(“\n\tds.getConnection() : “+bean.getConnection());

}
catch (Exception e)
{
out.println(e);;
e.printStackTrace();
}
}
}

Step 5). Compile the “EmployeeBean.java” and “MyJNDIServlet.java” classes by adding the Spring.jar and Spring-jdbc.jar files in the CLASSPATH of the command prompt.


javac    -d  .  MYJNDIServlet.java

javac    -d  .  EmployeeBean.java

Step 6). Now place the compiled classes and the Spring Configuration “applicationContext.xml” file inside the “WEB-INF/classes” directory.

Step 7).  Place the “spring.jar” and “spring-jdbc.jar” files inside the “WEB-INF/lib” directory.

Step 8). Write a Simple “index.jsp” page with a link to your Servlet…



<%@ page language=”java” contentType=”text/html;charset=UTF-8″%>
<center>
<a href=”MyServlet”>Click Here To Test Simple SpringBean.</a>
<BR><BR>
<a href=”MyJNDIServlet”>Click Here To Test <B><i>JNDI Spring Bean</B></i>.</a>

Step 9). Create a WAR File of above contents and then before deploying it on the Server. Finally Create a DataSource on the Server with the JNDI Name “jdbc/MyDS”.

Step 10). Deploy the Application On the Server and then access the MyJNDIServlet to perform a JNDI lookup using Spring Framework.

regards

Jay SenSharma


Enabling Spring Console Extension

Hi,

Jay SenSharma

Jay SenSharma

Here is a Simple demonstration to Enable Spring Console Extension to Monitor the Spring Bean Details in AdminConsole.  For more details please refer to : http://download.oracle.com/docs/cd/E12840_01/wls/docs103/programming/spring.html#wp1081223

Step1). Create a Directory somewhere sothat we can develope a Simple Spring Application inorder to achieve this:

Example: C:\PostOnSpring

Step2). Open a Command Window  and run “. ./setWLSEnv.sh”

Step3). Use the following command to deploy the “weblogic-spring.jar” file as a Library (Optional Package) in the targeted Server where we are intended to deploy our Spring Applications.

For details of  Optional Package TestCase/Demo —>please refer to : Optional Packages In WebLogic

C:\PostOnSpring>java weblogic.Deployer -library -deploy -source C:\bea103\wlserver_10.3\server\lib\weblogic-spring.jar -targets AdminServer -adminurl t3://localhost:7001 -user weblogic -password weblogic

Step4). Login to Admin Console —>Home—>Preferences (Link top of the page)—> Extensions (TAB)—>     spring-console (Check)

and then click on the “Enable” Button

Step5). Restart your Server in order to take the Changes Affect.

================Developing Spring Application Part================

Step6). Create a Spring WebApplication directory with name “SpringBeanReference” inside “C:\PostOnSpring”.

Step7). Create “index.jsp” page inside “C:\PostOnSpring\SpringBeanReference”.

Step8). Create “META-INF” and “WEB-INF” Directories inside “C:\PostOnSpring”

Step9). Inside “META-INF” directory provide the “MANIFEST.MF” file with the following informations…to make sure that the Application gets linked to the Optional Package which we have deployed on step 2)

Extension-List: WeblogicSpring
WeblogicSpring-Extension-Name: weblogic-spring
WeblogicSpring-Specification-Version: 10.3.0.0
WeblogicSpring-Implementation-Version: 10.3.0.0

NOTE: Make Sure that there is no extra spaces in the Above  4 lines either at the beginning or at the end of each lines.

Step10). Create a “lib” directory inside “C:\PostOnSpring\SpringBeanReference\WEB-INF” and place the following 3-Jar files there:

spring.jar
wlcommons-logging.jar
commons-logging.jar

Step11). Create “classes” Directory inside “C:\PostOnSpring\SpringBeanReference\WEB-INF” and provide the following 1-Interface and 1-Implementation Class there like following:

“HelloService.java”

package pack;
public interface HelloService {
public String sayHello(Name name);
}

and
“HelloService_Impl”

package pack;
public class HelloService_Impl implements HelloService {
    public HelloService_Impl()
      {
        System.out.println("\n\tHelloService_Impl() object Created...");
       }

    public String sayHello(Name name)
       {
         System.out.println("\n\tsayHello() of HelloService_Impl called");
         return "**** Hello  " + name.getFirstName() + " " + name.getLastName();
        }
}

Step12). CCompile above two classes there:

C:\PostOnSpring\SpringBeanReference\WEB-INF\classes> javac -d . HelloService.java
C:\PostOnSpring\SpringBeanReference\WEB-INF\classes> javac -d . HelloService_Impl.java

Step13). Provide a Simple Servlet just to see beans are accessible on not inside “C:\PostOnSpring\SpringBeanReference\WEB-INF\classes” provide the “MyServlet.java.java”

package servlets;
import javax.servlet.http.*;
import java.io.*;
import javax.servlet.*;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import pack.Name;
public class MyServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
PrintWriter out=res.getWriter();
ServletContext servletContext =this.getServletContext();
WebApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
// Call your HelloBean here
}
}

Step14).  Provide the follwing “applicationContext.xml” … inside “C:\PostOnSpring\SpringBeanReference\WEB-INF” directory

Step15).Please provide the “web.xml” file ..inside “C:\PostOnSpring\SpringBeanReference\WEB-INF\” like following;

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Spring_ReferenceBean</display-name>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>servlets.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>MyServlet</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>

Step16). Provide the “commons-logging.properties” file inside “C:\PostOnSpring\SpringBeanReference\WEB-INF\classes” with the following Line of code inside it:

org.apache.commons.logging.LogFactory = weblogic.logging.commons.LogFactoryImpl

Step17).Deploy the Application and then Login to the AdminConsole…..

Deployments—> Deployment Summary—> SpringBeanReference —->Configuration (Tab)—>Spring Framework (SubTab)

.
.
Thanks
Jay SenSharma

1 Comment more...

  • Testimonials

  • RSS Middleware Magic – JBoss

  • Receive FREE Updates


    FREE Email updates of our new posts Enter your email address:



  • Magic Archives

  • Sitemeter Status

  • ClusterMap 7-Nov-2011 till Date

  • ClusterMap 6-Nov-2010 till 7-Nov-2011

  • Copyright © 2010-2012 Middleware Magic. All rights reserved. | Disclaimer