2

我有一个非常简单的设置

我有一个applicationContext.xml这样的文件:

    <context:component-scan base-package="com.mkyong.common.controller" />

    <bean id="customizableTraceInterceptor"
        class="org.springframework.aop.interceptor.CustomizableTraceInterceptor">
        <property name="enterMessage" value="Entering $[methodName]($[arguments])" />
        <property name="exitMessage" value="Leaving $[methodName](): $[returnValue]" />
    </bean>
    <bean
        class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
            <list>
                <value>*Service</value>
            </list>
        </property>
        <!-- <property name="proxyTargetClass" value="true" /> -->
        <property name="interceptorNames">
            <list>
                <value>customizableTraceInterceptor</value>
            </list>
        </property>
    </bean>
    <mvc:annotation-driven />

    <bean id="wowService" class="org.daud.WowService" scope="prototype" />
</beans>

控制器类也很简单:

@Controller
@RequestMapping("/welcome")
public class HelloController {

    @Autowired
    private WowService wowService;

    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        getWowService().printIt();
        model.addAttribute("message", "Spring 3 MVC Hello World");
        return "hello";

    }

    public WowService getWowService() {
        return wowService;
    }

    public void setWowService(WowService wowService) {
        this.wowService = wowService;
    }

}

web.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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-app_3_0.xsd"
         version="3.0" metadata-complete="true">

    <display-name>simple-form</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>simple-form</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>simple-form</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

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

</web-app>

WowService也很简单

public class WowService {

    public String printIt() {
        return "daud";

    }
}

我有spring jars of version 3.0.6,aopalliance-1.0.jarcommons-logging-1.1.1.jar在我的 lib 文件夹中,没有别的。但是没有记录任何痕迹.. 即当我们进入 的方法WowService或离开它时,没有记录。我不知道为什么。

4

1 回答 1

1

以下行:

new WowService().printIt();

不创建由容器(Spring)管理的对象,并且不考虑 bean 的定义。

豆定义:

<bean id="wowService" class="org.daud.WowService" scope="prototype" />

看起来不错,但你没有使用它。

将您的控制器更改为:

@Controller
@RequestMapping("/welcome")
public class HelloController {

    @Autowired
    WowService wowService;

    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {

        wowService.printIt();
        model.addAttribute("message", "Spring 3 MVC Hello World");
        return "hello";

    }
}
于 2012-10-31T16:05:07.193 回答