zoukankan      html  css  js  c++  java
  • 非Controller类无法使用Service bean解决方案

     

    尝试方案: 1 在Spring的配置文件springmvc.xml中,增加扫描项base-package="zxs.ssm.util",增加你需要使用service的类所在的包 <context:component-scan base-package="zxs.ssm.controller,zxs.ssm.util"> 然后在相应的类上加上注解@Component 解决方案: 1 在web.xml文件中增加类监听器,例如:   <listener>    <listener-class>zxs.ssm.util.SpringInit</listener-class>   </listener> 2 编写类SpringInit

    package zxs.ssm.util;

    import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;

    import org.springframework.context.ApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils;

    public class SpringInit implements ServletContextListener {        

              private static WebApplicationContext springContext;    

              public SpringInit() {        

                              super();  

              }    

             public void contextInitialized(ServletContextEvent event) {       

                           springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());   

             }    

            public void contextDestroyed(ServletContextEvent event) {     }    

            public static ApplicationContext getApplicationContext() {     

                              return springContext;    

            }

    }

    3 即可在非Controller类中直接使用Service类(注意:需要使用配置文件中定义好的bean名称)  DepartmentService depService = (DepartmentService)SpringInit.getApplicationContext().getBean("depService");

    Jumping from failure to failure with undiminished enthusiasm is the big secret to success.
  • 相关阅读:
    TP6框架中如何无刷新上传文件
    TP6框架设置验证码的宽度和高度后,验证码显示不全
    TP6模板缓存问题
    TP6管理后台实战第五天文章管理
    20211027技术人为什么建议写博客
    TP6管理后台实战第六天系统管理
    博客园自定义菜单
    xcat无法连接服务器
    lsf安装后配置
    xcat网络无盘启动——添加自定义安装包
  • 原文地址:https://www.cnblogs.com/chongerlishan/p/5942033.html
Copyright © 2011-2022 走看看