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.
  • 相关阅读:
    ASP.NET 数据绑定常用代码及其性能分析
    替代Eval的两种方式
    C# MySQL 数据库操作类
    百度地图api经纬度气死我了!
    APP审核关于3.2.1金融资格的审核回复苹果
    ios 导航栏底部黑线隐藏
    ios 涉及到支付金额计算的相关总结
    ios 图片上传与压缩,UIImagePickerController设置中文
    ios UISegmentedControl的定制
    iOS APP应用之间的跳转
  • 原文地址:https://www.cnblogs.com/chongerlishan/p/5942033.html
Copyright © 2011-2022 走看看