0

我遇到了一个可以让我学习spring mvc的项目。我希望将会话 bean(用户)自动连接到控制器(聊天控制器)中。下面的代码示例似乎可以做到这一点,但是当我设置断点时,我注意到 User 的所有属性都保持为空。然而,printlns 会按预期打印名字。

知道我做错了什么吗?

聊天控制器

@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>

弹簧上下文.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>
4

2 回答 2

0

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

于 2012-07-25T18:56:01.657 回答
0

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

Explore the properties of the proxy and you will find the object with its properties populated; i.e. it might have advised, explore that and find targetSource, explore that and find target.

于 2016-09-28T13:58:27.893 回答