非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层负责数据的持久化
随机推荐
- 不用写软件,纯JS 实现QQ空间自动点赞
这里分享一个自己写的点赞JS,已实现了好友动态.右侧栏猜你喜欢 点赞,有兴趣的朋友可以加上去玩玩.打开浏览器的开发者模式运行就可以看到效果了 var count = 0; var total = 0; ...
- 10个国内外jQuery的CDN性能大比拼
jQuery是前端开发最常见也是最流行的javascript库,如何去加载它才能使我们的项目性能更好以及问什么要用CDN?当用户访问自己的站点时从服务器加载文件,每个服务器同时只能下载2-4个文件,这 ...
- TYVJ P1062 合并傻子 Label:环状dp
背景 从前有一堆傻子,钟某人要合并他们~但是,合并傻子是要掉RP的...... 描述 在一个园形操场的四周站着N个傻子,现要将傻子有次序地合并成一堆.规定每次只能选相邻的2个傻子合并成新的一个傻子,并 ...
- LeetCode-Sudoku Solver (递归解法)
题目地址: https://leetcode.com/problems/sudoku-solver/ // 将字符串的数独题转换成 int[9][9] ][], char ** b, int boar ...
- iOS学习—JSON数据解析
关于在iOS平台上进行JSON解析,已经有很多第三方的开源项目,比如TouchJson,JSONKit,SBJon等,自从iOS5.0以后,苹果SDK推出了自带的JSON解决方案NSJSONSer ...
- VTK 5.10.1 VS2010 Configuration 配置
Download VTK 5.10.1 Download VS2010 Download CMake 3.2.0 I assume you've already installed VS2010 an ...
- hdu Collect More Jewels
思路: 先用bfs求出入口,宝物,出口,两两之间的最短距离. 在用dfs搜索所有情况,求出从入口走到出口能获得的最大价值. 我们要解决几个问题:1.求入口到第一个取宝物的地方的最短距离 2.求第i个取 ...
- 【iCore3双核心板】扩展引脚分布
PDF 版下载: http://files.cnblogs.com/files/xiaomagee/iCore3%E6%89%A9%E5%B1%95%E5%BC%95%E8%84%9A%E5%88%8 ...
- 34. 求e的近似值
求e的近似值 #include <stdio.h> double fact (int n); int main() { int i, n; double item, sum; while ...
- cURL 学习笔记与总结(4)使用 cURL 从 ftp 上下载文件与上传文件到 ftp
下载: <?php $curlobj = curl_init(); curl_setopt($curlobj, CURLOPT_URL, "ftp://192.***.*.***/文件 ...