非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层负责数据的持久化
随机推荐
- 二分搜索 UVALive 6076 Yukari's Birthday (12长春K)
题目传送门 题意:问使得sum (k^i) = n || n -1 (1 <= i <= r) 的min (r*k)组合的r和k 分析:r的最大不会超过40,枚举r,二分搜索k.注意会爆 ...
- TYVJ P1083 分糖果 Label:bfs
描述 童年的我们,将和朋友分享美好的事物作为自己的快乐.这天,C小朋友得到了Plenty of candies,将要把这些糖果分给要好的朋友们.已知糖果从一个人传给另一个人需要1 秒的时间,同一个小朋 ...
- ubuntu下新建用户的终端不显示当前路径,不能用上下光标键得到使用过的命名解决办法
这几天我装ubuntu10.10,xubuntu12.04创建新用户的时候,总会遇到这个问题 就是打开终端的时候,没有路径了,即:xxx@xxx:~$ 找了很久,最后找到了(http://www.os ...
- 【NOIP2003】传染病控制(-贪心/dfs)
我自己yy了个贪心算法,在某oj 0msAC~.然后去wikioi提交,呵呵,原来是之前oj的数据太弱给我水过了,我晕. 我之前的想法是在这棵树上维护sum,然后按时间来割边,每一时刻割已经感染的人所 ...
- Uiautomator打包使用第三方库,报错的解决方案
问题引源: 在做自动化过程中,想在用例执行完毕后,自动生成该用例测试报告: 报告定义为Excel格式文件,且在用例执行过程中生成. 所以我在Uiautomator工程中引用了jxl.jar,用以处理E ...
- How to override create,write,unlink method in Odoo v8
As we all know, Odoo 8 has new api which is different with v7. So how to override the create,write,u ...
- mysql中的comment用法
转自:http://wenku.baidu.com/view/2a54e7892cc58bd63186bd8f.html 在MySQL数据库中,字段或列的注释是用属性comment来添加. 创建新表的 ...
- WPF之拖动项滚动条自滚动(当拖动项到达高度的边界时候滚动条自己可以上下滚动)
参考 http://www.cnblogs.com/ListenFly/p/3281997.html Point svPoint = e.GetPosition(sv); if (sv.ActualH ...
- Leetcode | Minimum/Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- vi用法