shuces_ysh 2017-05-15 02:57 采纳率: 0%
浏览 882
已结题

关于servlet中注入Spring bean 的问题

web.xml 相关配置如下

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:springConfig/applicationContext.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>clusterServlet</servlet-name>
        <servlet-class>com.shu.ces.ysh.servlet.ClusterServletProxy</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>MVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath*:springConfig/mvc*.*</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>MVC</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

Spring 配置如下

<bean id = "grid" class = "com.shu.ces.ysh.model.Grid" scope = "singleton">
        <property name = "clusters">
            <list>
                <ref bean = "cluster1"></ref>
            </list>
        </property>
    </bean>

ClusterServletProxy代码如下

public class ClusterServletProxy extends HttpServlet {

    private String targetBean;
    private Servlet proxy;

    public void init() throws ServletException {
        this.targetBean = getServletName();  
        getServletBean();  
        proxy.init(getServletConfig());
    }

    private void getServletBean(){  
        WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());  
        this.proxy = (Servlet) wac.getBean(targetBean);  
    }  
}

ClusterServlet代码如下

public class ClusterServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

//  @Autowired
    //SpringUtil spring;

    private JobQueue jobQueue;

    private Grid grid;

    public JobQueue getJobQueue() {
        return jobQueue;
    }

    public void setJobQueue(JobQueue jobQueue) {
        this.jobQueue = jobQueue;
    }

    public Grid getGrid() {
        return grid;
    }

    public void setGrid(Grid grid) {
        this.grid = grid;
    }



//  @SuppressWarnings("static-access")
    public void init() throws ServletException {
        System.out.println(grid);
        ......
        ......
        ....

controller代码如下

 @Controller
public class UserController {

    @Autowired
    private Grid grid;

    @Resource(name="userDao")
    private UserDao userDao;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    public ModelAndView login (String userName, String password) {
        System.out.println(grid);
        ....
        ....
        ....

结果: 两次打印出来的grid bean不是同一个bean
servlet 中 : com.shu.ces.ysh.model.Grid@6df773
controller中: com.shu.ces.ysh.model.Grid@16b22f8

  • 写回答

1条回答 默认 最新

  • Mr__Lee1208 2017-05-15 03:05
    关注

    classpath*:springConfig,classpath*:applicationContext.xml 符号换成逗号试试,再加一个classpath

    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏