Search This Blog

Sunday 21 July 2013

AOP MVC Spring Demo





MVC AOP Spring Demo 

webDisplay.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:aop="http://www.springframework.org/schema/aop"
    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">

    <bean id="propsResolver"
        class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
       
        <property name="mappings">
        <props>
             <prop key="/showWelcome.do">showWelcome</prop>
             <prop key="/showDetails.do">showDesig</prop>
             <prop key="/showAll.do">showAll</prop>
        </props>
       
            <!--<value>
                /showWelcome.do=showWelcome
                /showDetails.do=showDesig
                /showAll.do=showAll
        </value>
        --></property>
    </bean>
    <bean id="showImpl" class="com.xyz.ShowImpl"></bean>
    <bean id="paramMultiController"
        class="com.xyz.MyController">
        <property name="methodNameResolver" ref="propsResolver" />
        <property name="name" value="MPB" />
        <property name="desig" value="SSE" />
        <property name="showImpl"><ref local="showImpl"/></property>
    </bean>

    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <bean id="profiler" class="com.xyz.SimpleProfiler"/>
    <aop:config>
    <aop:aspect ref="profiler">
        <aop:pointcut id="aopafterMethod" expression="execution(* com.xyz.ShowInterface.showCon())" />
        <aop:after pointcut-ref="aopafterMethod" method="profile" />
        <aop:pointcut id="aopBefore"
                expression="execution(* com.xyz.ShowInterface.showDis())" />
        <aop:before pointcut-ref="aopBefore" method="beforeProfile" />
        <aop:pointcut id="aopAround"
                expression="execution(* com.xyz.ShowInterface.showWel())" />
        <aop:around pointcut-ref="aopAround" method="aroundProfile" />
        </aop:aspect>
    </aop:config>
</beans>

webContext.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:aop="http://www.springframework.org/schema/aop"
    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">
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
          <list>
            <value>mvcprops_en</value>
          </list>
        </property>
      </bean>
   
    <bean id="urlHandler"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

        <property name="mappings">
         <map>
             <entry key="/showWelcome.do" value="paramMultiController"/>
             <entry key="/showDetails.do" value="paramMultiController"/>
             <entry key="/showAll.do" value="paramMultiController"/>
         </map>
   
            <!--<props>
                <prop key="/showWelcome.do">paramMultiController</prop>
                <prop key="/showDetails.do">paramMultiController</prop>
                <prop key="/showAll.do">paramMultiController</prop>
            </props>
        --></property>
        <!--<bean id = "" class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />
       

    --></bean>
</beans>
web.xml
=============================================================
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/webContext.xml, /WEB-INF/webDisplay.xml
        </param-value>
    </context-param>
    <servlet>
        <servlet-name>springapp</servlet-name>
        <servlet-class> org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springapp</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/classes/log4j.properties</param-value>
    </context-param>
    <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>   
    <listener>
       <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
   
    <!--<taglib>
        <taglib-uri>http://mytag.org/taglibs/c.tld</taglib-uri>
        <taglib-location>/WEB-INF/c.tld</taglib-location>
    </taglib>
   
    --><welcome-file-list>
        <welcome-file>
            index.jsp
        </welcome-file>
    </welcome-file-list>

</web-app>

springapp-servlet.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"
    xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


</beans>

WebContent\WEB-INF\src\log4j.properties
=============================================================
log4j.rootCategory=DEBUG,fileAppender
log4j.appender.fileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.fileAppender.File=D:\\logs\\myAppLog.log
log4j.appender.fileAppender.Encoding=UTF-8
log4j.appender.fileAppender.layout.ConversionPattern=%-4d %-C : %M - %m%n
log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout

WebContent\WEB-INF\src\mvcprops_en.properties
=============================================================
companyName=Test
location=ABC
state=XYZ
country=India

MyController.java
=============================================================
package com.xyz;

import java.util.Iterator;
import java.util.Locale;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.MessageSource;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

/**
 * @author Peda Babu M
 *
 */
public class MyController extends MultiActionController{
    public String name = "";
    public String desig = "";
    public ShowInterface showImpl;
   
    public ShowInterface getShowImpl() {
        return showImpl;
    }
    public void setShowImpl(ShowInterface showImpl) {
        this.showImpl = showImpl;
    }
    private final Log       logger  = LogFactory.getLog(getClass());
    public Log getLogger() {
        return logger;
    }
    public String getName() {
        if (logger.isDebugEnabled()) {
            logger.debug("MyController getName method start");
        }
        if (logger.isDebugEnabled()) {
            logger.debug("MyController getName method end");
        }
        return name;
    }
    public void setName(String name) {
        if (logger.isDebugEnabled()) {
            logger.debug("MyController setName method start");
        }
        System.out.println("Initilized111111111111111111111111111111111111111111111111");
        this.name = name;
        if (logger.isDebugEnabled()) {
            logger.debug("MyController setName method end");
        }
    }
    public String getDesig() {
        if (logger.isDebugEnabled()) {
            logger.debug("MyController getDesig method start");
        }
        if (logger.isDebugEnabled()) {
            logger.debug("MyController getDesig method end");
        }
        return desig;
    }
    public void setDesig(String desig) {
        if (logger.isDebugEnabled()) {
            logger.debug("MyController setDesig method start");
        }
        System.out.println("Initilized22222222222222222222222222222222222222222222222222");
        this.desig = desig;
        if (logger.isDebugEnabled()) {
            logger.debug("MyController setDesig method end");
        }       
    }
    public ModelAndView showWelcome(HttpServletRequest request,  HttpServletResponse response) {
        if (logger.isDebugEnabled()) {
            logger.debug("MyController showWelcome method start");
        }
        WebApplicationContext webAppContext = getWebApplicationContext();
        MessageSource messageSource = (MessageSource) webAppContext.getBean("messageSource");
        System.out.println("MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS");
        System.out.println(messageSource.toString());
        Properties prop = System.getProperties();
        Iterator iterator = prop.keySet().iterator();
        while (iterator.hasNext()) {
            System.out.println(prop.get(iterator.next()));
        }
        showImpl.showWel();
        String companyName = messageSource.getMessage("companyName", null, "Default", Locale.US);
        String location = messageSource.getMessage("location", null, "Default", Locale.US);
        String state = messageSource.getMessage("state", null, "Default", Locale.US);
        String country = messageSource.getMessage("country", null, "Default", Locale.US);
        String welMessage     = "Welcome "+ name + "to Test";
        ModelAndView mv = new ModelAndView("welcome");
        mv.addObject("message", welMessage);
        mv.addObject("companyName", companyName);
        mv.addObject("location", location);
        mv.addObject("state", state);
        mv.addObject("country", country);
        mv.addObject("message", welMessage);
        if (logger.isDebugEnabled()) {
            logger.debug("MyController showWelcome method end");
        }
        return mv;
    }
    public ModelAndView showDesig(HttpServletRequest request,  HttpServletResponse response) {
        if (logger.isDebugEnabled()) {
            logger.debug("MyController showDesig method start");
        }
        String welMessage     = "Employee Desig" + desig;
        showImpl.showDis();
        ModelAndView mv = new ModelAndView("desig");
        mv.addObject("message", welMessage);
        if (logger.isDebugEnabled()) {
            logger.debug("MyController showDesig method end");
        }
        return mv;
    }
    public ModelAndView showAll(HttpServletRequest request,  HttpServletResponse response){
        if (logger.isDebugEnabled()) {
            logger.debug("MyController showAll method start");
        }
        System.out.println("Initilized111111111111111111111111111111111111111111111111");
        showImpl.showCon();
        String welMessage     = "Welcome "+ name + "to Test And desig" + desig ;
        ModelAndView mv = new ModelAndView("showall");
        mv.addObject("message", welMessage);
        if (logger.isDebugEnabled()) {
            logger.debug("MyController showAll method end");
        }
        return mv;
    }
}

ShowImpl.java
===================================================
package com.xyz;

/**
 * @author Pedababu M
 *
 */
public class ShowImpl implements ShowInterface{
    public void showCon() {
        System.out.println("ShowImpl - showCon");
    }
    public void showDis() {
        System.out.println("ShowImpl - showDis");
    }
    public void showWel() {
        System.out.println("ShowImpl - showWel");
    }   
}

ShowInterface.java
=====================================================
package com.xyz;

/**
 * @author Pedababu M
 *
 */
public interface ShowInterface {
public void showCon();
public void showDis();
public void showWel();
}

SimpleProfiler.java
========================================================
package com.xyz;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.StopWatch;

/**
 * @author Pedababu M
 *
 */
public class SimpleProfiler {

   public Object aroundProfile(ProceedingJoinPoint call) throws Throwable {
      StopWatch clock = new StopWatch(
            "Profiling");
      System.out.println("SimpleProfiler - 1 - aroundProfile");
      try {
         clock.start(call.toShortString());
         Object obj = call.proceed();
         System.out.println("SimpleProfiler - 2 - aroundProfile");
         return obj;
      } finally {
         clock.stop();
         System.out.println(clock.prettyPrint());
      }
     
   }
   public void profile() throws Throwable {
          System.out.println("SimpleProfiler - After advice");
       }
       public void beforeProfile() throws Throwable {
              System.out.println("SimpleProfiler - Before advice");
       }
}


desig.jsp
=============================================================
<H1>Designation on Test</H1>
<% String message = (String)request.getAttribute("message");%>
<H1><%=message %></H1>
<A href="/SpringTest2/showWelcome.do"><B>Click for:Welcome</B></A>
<A href="/SpringTest2/showDetails.do"><B>Click for:showDetails</B></A>
<A href="/SpringTest2/showAll.do"><B>Click for: All</B></A>


showall.jsp
=============================================================
<H1>Welcome to Test And Designation in Test</H1>
<% String message = (String)request.getAttribute("message");%>
<H1><%=message %></H1>
<A href="/SpringTest2/showWelcome.do"><B>Click for:Welcome</B></A>
<A href="/SpringTest2/showDetails.do"><B>Click for:showDetails</B></A>
<A href="/SpringTest2/showAll.do"><B>Click for: All</B></A>

welcome.jsp
=============================================================
<% String message = (String)request.getAttribute("message");
String companyName = (String)request.getAttribute("companyName");
String location = (String)request.getAttribute("location");
String state = (String)request.getAttribute("state");
String country = (String)request.getAttribute("country");
%>
<H1><%=message %></H1>
<H1>Welcome to <%=companyName %> </H1>
<H1>Location: <%=location %></H1>
<H1>State: <%=state %></H1>
<H1>Country: <%=country %></H1>

<A href="/SpringTest2/showWelcome.do"><B>Click for:Welcome</B></A>
<A href="/SpringTest2/showDetails.do"><B>Click for:showDetails</B></A>
<A href="/SpringTest2/showAll.do"><B>Click for: All</B></A>

No comments:

Post a Comment