我遇到了一个让我学习spring mvc的项目。我想要一个会话bean(用户)自动装入控制器(ChatController)。下面的代码示例似乎这样做,但是当我设置断点时,我注意到User的所有属性都保持为null。但是,printlns会按预期打印名字。
知道我做错了什么吗?
ChatController
@Controller
public class ChatController {
@Autowired
private User user;
@RequestMapping(value="/chat")
public ModelAndView start()
{
System.out.println("pre" + user.getFirstName());
user.setFirstName(user.getFirstName() + " foo");
System.out.println("post"+user.getFirstName());
return new ModelAndView("/WEB-INF/views/error.jsp");
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
用户
@Component("user")
@Scope(value="session")
public class User {
private String firstName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5" 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_2_5.xsd">
<display-name>ContactUs</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/spring-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
弹簧context.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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-2.5.xsd
">
<context:component-scan base-package="com" scoped-proxy="targetClass" />
<mvc:annotation-driven />
<context:annotation-config/>
<!-- <bean id="user" class="User" scope="session" autowire="byName">
<aop:scoped-proxy/>
</bean>
-->
</beans>
答案 0 :(得分:0)
我通过进一步阅读CGLIB了解到,这是使用此库的代理的正常行为。
答案 1 :(得分:0)
代理的属性将显示为null
。代理只是到实际类的路径,它不包含类本身的值。
探索代理的属性,您将找到填充了其属性的对象;即它可能已经建议,探索并找到targetSource
,探索并找到目标。