无法在spring MVC中应用方面

时间:2015-02-04 07:26:45

标签: java spring spring-mvc aop spring-aop

在我的项目中,我想在Spring MVC中应用AOP。并在网页中显示输出。但无法显示并无法在控制器类中应用建议。

Logging.java: -

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.springframework.stereotype.Component;

@Aspect
public class Logging {

   /** Following is the definition for a pointcut to select
    *  all the methods available. So advice will be called
    *  for all the methods.
    */
   @Pointcut("execution(* com.*.*(..))")
   private void selectAll(){}

   /** 
    * This is the method which I would like to execute
    * before a selected method execution.
    */
   @Before("selectAll()")
   public void beforeAdvice(){
      System.out.println("Going to setup student profile.");
   }

   /** 
    * This is the method which I would like to execute
    * after a selected method execution.
    */
   @After("selectAll()")
   public void afterAdvice(){
      System.out.println("Student profile has been setup.");
   }

   /** 
    * This is the method which I would like to execute
    * when any method returns.
    */
   @AfterReturning(pointcut = "selectAll()", returning="retVal")
   public void afterReturningAdvice(Object retVal){
      System.out.println("Returning:" + retVal.toString() );
   }

   /**
    * This is the method which I would like to execute
    * if there is an exception raised by any method.
    */
   @AfterThrowing(pointcut = "selectAll()", throwing = "ex")
   public void AfterThrowingAdvice(IllegalArgumentException ex){
      System.out.println("There has been an exception: " + ex.toString()); 
   }
  @Around("selectAll()")
   public Object aroundAdvice(ProceedingJoinPoint p) throws Throwable{
   System.out.println("Before around");
   Object o=p.proceed();
   System.out.println("After around");
   return o;
   }
   }

pojo类 Student.java: -

public class Student {
       private Integer age;
       private String name;

       public void setAge(Integer age) {
          this.age = age;
       }
       public Integer getAge() {
          System.out.println("Age : " + age );
          return age;
       }

       public void setName(String name) {
          this.name = name;
       }
       public String getName() {
          System.out.println("Name : " + name );
          return name;
       }

    }

UserController.java

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

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;





public class UserController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest arg0,
            HttpServletResponse arg1) throws Exception
            {
         ApplicationContext context = 
                 new FileSystemXmlApplicationContext("C:/Users/pcuser/Desktop/zspringmvcannotation/WebContent/WEB-INF/applicationContext.xml");

          Student student = (Student) context.getBean("student");

          student.getName();
          student.getAge();

          return new ModelAndView("success","student",student);
           // return new ModelAndView("/WEB-INF/jsp/success.jsp");

    }
}

的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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
    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
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <bean p:suffix=".jsp" p:prefix="/WEB-INF/jsp/"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        id="viewResolver" />



<!-- AOP support -->
        <bean id="logging" class="Logging" />
        <aop:aspectj-autoproxy>
            <aop:include name='logging' />

        </aop:aspectj-autoproxy>




    <!-- Definition for student bean -->
    <bean id="student" class="Student">
        <property name="name" value="Tapajyoti" />
        <property name="age" value="22" />
    </bean>




    <bean id="userController" class="UserController" />

    <bean id="urlMapping"
        class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">

        <property name="mappings">
            <props>
                <prop key="/hello.htm">userController</prop>
            </props>
        </property>
    </bean>

</beans>

分配器一servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<import resource="applicationContext.xml"></import>

</beans>

Web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>
    SpringMVC</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>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>*.htm</url-pattern>
  </servlet-mapping>

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


</web-app>

1 个答案:

答案 0 :(得分:0)

我同意M.Deinum,请首先纠正这些说明。此外,您应该删除Dispatcher-servlet.xml,因为您有一个FileSystemXmlApplicationContext并且已经在那里加载了xml中的所有bean。然后请删除&lt; listener&gt;和&lt; context-param&gt; web.xml中的元素并按如下方式配置您的方面:

<aop:aspectj-autoproxy />
<!-- Aspect -->
<bean id="loggingAspect" class="Logging" />