Spring 自动连线bean-eclipse调试为空
我遇到了一个可以让我学习SpringMVC的项目。我希望会话bean(用户)自动连接到控制器(ChatController)。下面的代码示例似乎就是这样做的,但是当我设置断点时,我注意到User的所有属性都保持为null。但是,println会按预期打印第一个名称 知道我做错了什么吗 聊天室管理员Spring 自动连线bean-eclipse调试为空,spring,spring-mvc,Spring,Spring Mvc,我遇到了一个可以让我学习SpringMVC的项目。我希望会话bean(用户)自动连接到控制器(ChatController)。下面的代码示例似乎就是这样做的,但是当我设置断点时,我注意到User的所有属性都保持为null。但是,println会按预期打印第一个名称 知道我做错了什么吗 聊天室管理员 @Controller public class ChatController { @Autowired private User user; @RequestMappin
@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;
}
}
使用者
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>
联系方式
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
springServlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/spring-context.xml
1.
springServlet
/*
spring-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>
通过进一步阅读CGLIB,我了解到这是代理使用该库的正常行为 代理的属性将显示null
。代理只是到实际类的路由,它不保存类本身的值
探索代理的属性,您将发现填充了其属性的对象;i、 e.它可能会建议,探索该问题并找到
targetSource
,探索该问题并找到target。发布一个链接,链接到您发现特定问题答案的位置如何。。。
<?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>