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";

    }
}

、およびlib フォルダーにあり、他には何もありませんspring jars of version 3.0.6。しかし、トレースは記録されません。つまり、 のメソッドに入るとき、またはメソッドを終了するときに、ログは記録されません。理由がわかりません。aopalliance-1.0.jarcommons-logging-1.1.1.jarWowService

4

1 に答える 1

1

次の行:

new WowService().printIt();

コンテナ(Spring)によって管理されるオブジェクトは作成されず、Beanの定義は考慮されません。

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 に答える