2012-07-25 47 views
0

我遇到过一个项目,可以让我学习spring mvc。我想要一个会话bean(用户)被自动装入控制器(ChatController)。下面的代码示例似乎这样做,但是当我设置断点时,我注意到用户的所有属性都保留为空。然而,printlns按预期打印名字。Autowiring bean - eclipse调试为空

任何想法我做错了吗?

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

我通过进一步阅读CGLIB了解到,这是使用此库的代理的正常行为。

+0

如何发布链接到您发现问题的答案的地方...... – chrisjleu 2014-07-25 14:21:23

0

代理的属性将出现null。代理只是实际类的路由,它不包含类本身的值。

探索代理的属性,你会发现它的属性填充的对象;即它可能建议,探索并找到targetSource,探索并找到目标。