我的设置非常简单
我有一个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.jar
和commons-logging-1.1.1.jar
,没有别的。但是没有记录痕迹......即当我们输入WowService
的方法或者我们离开它时,没有记录。我无法弄清楚原因。
答案 0 :(得分: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";
}
}