非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");
非Controller类无法使用Service bean解决方案的更多相关文章
- spring非controller类获取service方法
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); pushMessageServ ...
- 非Contorller类使用@Service中的方法
组件扫描这种的是指bean,跟service没关系 service只能在Controller类中使用,如果别的类想使用,必须使用下面这种方法 内容来源:https://blog.csdn.net/u0 ...
- 非Controller中调用Service
1. 新增文件 package com.library.common; import org.springframework.beans.BeansException; import or ...
- Ionic Contoller类与Service类分开需要注意的问题
看了别人的项目把Controller类和Service类都写在了app.js文件里面,这不符合我的风格,想把他们分开成单独的文件,确遇见以下错误提示: ionic.bundle.min.js:133 ...
- Spring MVC不要在@Service bean中保存状态
先看这么一段代码: @Service public class AccountService { private String message; public void foo1() { if (tr ...
- Netty handler处理类无法使用@Autowired注入bean的解决方法
问题由来: 公司有个项目用到netty作为websocket的实现,最近打算部署双机,这使得原来在内存中的保存Channel信息的方案不再可行,需要转移到redis中,改造过程中发现通过@Autowi ...
- Spring @Autowired注解在非Controller/Service中注入为null
参考:https://blog.csdn.net/qq_35056292/article/details/78430777 问题出现: 在一个非controller/service类中,我需要注入Co ...
- 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 ...
- controller层负责创建类传递类给service;service层负责逻辑编写调用dao层 将编写后的类传递到dao层,保证事务的正确性;dao层负责数据的持久化
controller层负责创建类传递类给service:service层负责逻辑编写调用dao层 将编写后的类传递到dao层,保证事务的正确性:dao层负责数据的持久化
随机推荐
- git中进入带有空格的目录下的解决办法
比如:要进入Program Files目录下 有两种方法: 1.将Program Files目录用引号引起来. $ cd "Program Files" 2.将空格处使用空格引号 ...
- bzoj1019 [SHOI2008]汉诺塔
1019: [SHOI2008]汉诺塔 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 1030 Solved: 638[Submit][Status] ...
- 洛谷 P1118 数字三角形游戏 Label:dfs
题目描述 有这么一个游戏: 写出一个1-N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直到只剩下一个数字位置.下面是一 ...
- 【BZOJ】1452: [JSOI2009]Count
http://www.lydsy.com/JudgeOnline/problem.php?id=1452 题意:n×m的矩阵上每个点有个颜色,现在有q个操作:1 x y c 将点(x,y)的颜色改为c ...
- Sublime Text 2配置
gedit用了很久,终于换编辑器了T_T Sublime Text 自行百度谷歌. 一开始我在官网下载的压缩包,然后自己配置.搞了半天后果断删掉...还是用源的自动安装吧.T_T 恩.下面的命令 su ...
- CentOS 下安装python 之MySQLdb
yum -y install mysql-devwget http://downloads.sourceforge.net/project/mysql-python/mysql-python-test ...
- Inheritance, Association, Aggregation, and Composition 类的继承,关联,聚合和组合的区别
在C++中,类与类之间的关系大概有四种,分别为继承,关联,聚合,和组合.其中继承我们大家应该都比较熟悉,因为是C++的三大特性继承Inheritance,封装Encapsulation,和多态Poly ...
- javascript 三个 对话框
用法: 一般写在 </html>之后,<script language="javascript">代码必须放在这里面</script> 三个常用 ...
- 解决win7系统远程桌面 server 2003 卡的问题
原因在于从vista开始,微软在TCP/IP协议栈里新加了一个叫做“Window Auto-Tuning”的功能.这个功能本身的目的是为了让操作系统根据网络的实时性能(比如响应时间)来动态调整网络上传 ...
- 开发基础框架:mybatis-3.2.8 +hibernate4.0+spring3.0+struts2.3
一:项目下载地址(点击 Source code(zip)) https://github.com/fzxblgong/frame_2014-12-15/releases 版本:v1.2大小:20M 二 ...