Java 非bean类的springaop

Java 非bean类的springaop,java,spring,spring-mvc,spring-aop,Java,Spring,Spring Mvc,Spring Aop,我正在尝试学习SpringAOP,据我所知,它是通过代理工作的,人们只能将方面应用于bean,使用代理而不是直接使用bean。 所以我有 @Controller public class CMSHelloWorld { String message; @RequestMapping("/welcome") public ModelAndView helloWorld() { if (message == null || message.equals(

我正在尝试学习SpringAOP,据我所知,它是通过代理工作的,人们只能将方面应用于bean,使用代理而不是直接使用bean。 所以我有

@Controller
public class CMSHelloWorld {

    String message;

    @RequestMapping("/welcome")
    public ModelAndView helloWorld() {

        if (message == null || message.equals("")) {
            ApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml");
            MessageBean messageBean = (MessageBean) context.getBean("simpleServiceProxy");
            message = messageBean.getMessage();
        }
        return new ModelAndView("welcome", "message", message);
    }
//getters & setters omitted
}
我还有一个非常简单的bean:

public class MessageBean {
    String message;
    //getters & setters omitted
}
和配置:

<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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">

<bean id="simpleServiceBean" class="com.beans.MessageBean">
        <property name="message" value="Hello" />
    </bean> 
    <bean id="simpleControllerBean" class="com.controller.CMSHelloWorld">
    </bean>

    <bean id="doBeforeMethodBean"
        class="com.demshin.advice.DoBeforeMethod" />

    <bean id="simpleServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="simpleServiceBean" />
        <property name="interceptorNames">
            <list>
                <value>doBeforeMethodBean</value>
            </list>
        </property>
    </bean>
    <bean id="simpleControllerProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="simpleControllerBean" />
        <property name="interceptorNames">
            <list>
                <value>doBeforeMethodBean</value>
            </list>
        </property>
    </bean>
</beans>

多比福姆法比恩
多比福姆法比恩
它适用于
MessageBean
的方法,显然不适用于
Controller
的方法,因为此类直接寻址,而不是通过其代理。
那么,将AOP应用于控制器的正确方法是什么呢

Spring
ApplicationContext
是独立的

您的
@Controller
bean正在通过web应用程序创建的
WebApplicationContext
中执行,该应用程序由
ContextLoaderListener
DispatcherServlet
(或Spring boot)创建

当您发送请求时,
DispatcherServlet
选择您的
@Controller
bean来处理该请求,您的
helloWorld
方法将创建一个新的
,它完全独立于web应用程序,具有自己的bean

豆子在那里宣布

<bean id="simpleControllerBean" class="com.controller.CMSHelloWorld">
</bean>`

`
与处理请求的bean完全没有关系


如果希望AOP建议应用于处理请求的bean,请在web应用程序的
ApplicationContext

中定义AOP逻辑,为什么在控制器内初始化的另一个
ApplicationContext
中会有这样的建议?为什么您的AOP不是web应用程序的
ApplicationContext
”的一部分?我刚到春天,所以我恐怕不明白您的意思,对不起。您能详细说明一下或给我指一些教程,清楚地说明正确的方法吗?“是Spring文档。谢谢您,但您还是可以花一些时间给我,简单地解释一下问题是什么?问题是当请求到达控制器时,您没有创建应用程序上下文。在最简单的情况下,应用程序上下文是在应用程序启动时加载的,加载过程完成后,您就可以正确地创建、配置、代理和注入所有bean,等等。