2012-07-25 49 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,探索並找到目標。