尝试方案: 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");

非Controller类无法使用Service bean解决方案的更多相关文章

  1. spring非controller类获取service方法

    ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); pushMessageServ ...

  2. 非Contorller类使用@Service中的方法

    组件扫描这种的是指bean,跟service没关系 service只能在Controller类中使用,如果别的类想使用,必须使用下面这种方法 内容来源:https://blog.csdn.net/u0 ...

  3. 非Controller中调用Service

    1.       新增文件 package com.library.common; import org.springframework.beans.BeansException; import or ...

  4. Ionic Contoller类与Service类分开需要注意的问题

    看了别人的项目把Controller类和Service类都写在了app.js文件里面,这不符合我的风格,想把他们分开成单独的文件,确遇见以下错误提示: ionic.bundle.min.js:133 ...

  5. Spring MVC不要在@Service bean中保存状态

    先看这么一段代码: @Service public class AccountService { private String message; public void foo1() { if (tr ...

  6. Netty handler处理类无法使用@Autowired注入bean的解决方法

    问题由来: 公司有个项目用到netty作为websocket的实现,最近打算部署双机,这使得原来在内存中的保存Channel信息的方案不再可行,需要转移到redis中,改造过程中发现通过@Autowi ...

  7. Spring @Autowired注解在非Controller/Service中注入为null

    参考:https://blog.csdn.net/qq_35056292/article/details/78430777 问题出现: 在一个非controller/service类中,我需要注入Co ...

  8. Spring Cloud Ribbon负载均衡配置类放在Spring boot主类同级增加Exclude过滤后报Field config in com.cloud.web.controller.RibbonConfiguration required a bean of type 'com.netflix.client.config.IClientConfig' that could not b

    环境: Spring Cloud:Finchley.M8 Spring Boot:2.0.0.RELEASE 目录结构: 可以看到代码第13行的注释,我已经在@ComponentScan注解中添加了E ...

  9. controller层负责创建类传递类给service;service层负责逻辑编写调用dao层 将编写后的类传递到dao层,保证事务的正确性;dao层负责数据的持久化

    controller层负责创建类传递类给service:service层负责逻辑编写调用dao层 将编写后的类传递到dao层,保证事务的正确性:dao层负责数据的持久化

随机推荐

  1. Queuing

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission ...

  2. DataTable转换成List<T>

    很多时候需要将DataTable转换成一组model,直接对model执行操作会更加方便直观. 代码如下: public static class DataTableToModel { public ...

  3. POJ1947 Rebuilding Roads(树形DP)

    题目大概是给一棵树,问最少删几条边可以出现一个包含点数为p的连通块. 任何一个连通块都是某棵根属于连通块的子树的上面一部分,所以容易想到用树形DP解决: dp[u][k]表示以u为根的子树中,包含根的 ...

  4. HDU4057 Rescue the Rabbit(AC自动机+状压DP)

    题目大概是给几个DNA片段以及它们各自的权值,如果一个DNA包含某个片段那么它的价值就加上这个片段的权值,同时包含多个相同DNA片段也只加一次,问长度l的DNA可能的最大价值. 与HDU2825大同小 ...

  5. Matrix Chain Multiplication[HDU1082]

    Matrix Chain Multiplication Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  6. cocos2d CCArray

    CCArray* arr=CCArray::create(); arr->retain();//如果不加这个东西,CCArray会被清空 arr->addObject(CCSprite:: ...

  7. BZOJ3571 : [Hnoi2014]画框

    题目是要求最小乘积最小权匹配, 将一种方案看做一个二维点(x,y),x=a值的和,y=b值的和,所有方案中只有在下凸壳上的点才有可能成为最优解 首先要求出两端的方案l,r两个点 l就是a值的和最小的方 ...

  8. redis AND memcache

    memcache文章索引 MEMCACHE问题集锦[转] MEMCACHED 高可用方案 REPCACHED NOSQL之[MEMCACHED]学习 当 MySQL 和 Memcached 遇到尾部空 ...

  9. 【wikioi】1281 Xn数列(矩阵乘法)

    http://wikioi.com/problem/1281/ 矩阵真是个神奇的东西.. 只要搞出一个矩阵乘法,那么递推式可以完美的用上快速幂,然后使复杂度降到log 真是神奇. 在本题中,应该很快能 ...

  10. Ubuntu 12.04安装Google Chrome

    详细请参照:http://hi.baidu.com/kevin276/item/29bc1c96a208fabc82d29542 至于安装之后怎么打开,在终端输入google-chrome即可打开并会 ...