使用service&scope 进行注入
@service 声明该类为一个bean,bean的名称为类名首字母小写(customerService)
@Scope("prototype")则声明为一个原子类型,既每个getbean方法返回一个实例
package spring_service; import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service; /**
* Created by luozhitao on 2017/8/10.
*/
@Service
@Scope("prototype")
public class CustomerService { public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
} private String message; }
package spring_service; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Created by luozhitao on 2017/8/10.
*/
public class service_app { public static void main(String [] args){ ApplicationContext context=new ClassPathXmlApplicationContext("bean_service.xml"); CustomerService customerService=(CustomerService)context.getBean("customerService"); customerService.setMessage("spring server method"); System.out.println(customerService.getMessage()); } }
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="spring_service" /> </beans>
bean.xml看起来就非常简练。
使用service&scope 进行注入的更多相关文章
- spring boot: scope (一般注入说明(一) @Autowired注解)
实例一: DiConfig 文件: package di; import org.springframework.context.annotation.ComponentScan; import or ...
- 解决service层无法注入
练手时发现个问题,路径404,各种检查发现,多加了一层<context:component-scan base-package="com.yanan.controller"/ ...
- Springboot 定时任务,service层无法注入问题详细解决
开发一个微信小程序后台,建立websocket 长连接,需要后台开启定时任务, 定时任务定时查库,相应前台 但是具体执行过程中一直在报空指针错误,最后定位到service 为空,无法调用其相关的方法导 ...
- quartz整合spring框架service层对象注入为null解决方案
Job实现类代码 package cn.itcast.quartz; import org.quartz.Job; import org.quartz.JobExecutionContext; imp ...
- SpringBoot拦截器中service或者redis注入为空的问题
原文:https://my.oschina.net/u/1790105/blog/1490098 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于 ...
- service里无法注入mapper,mapper空指针
被困扰了一天,终于解决了,记录一下 下面是mapper的代码 @Mapper public interface ProductDao { @Select("select * from pro ...
- Ionic之$scope 依赖注入报错
在开发Ionic过程中,发现会报在 LoginController 中引用locals报错,具体报错问题: ionic.bundle.js:19526 Error: [$injector:unpr] ...
- 关于工具类静态方法调用@Autowired注入的service类问题
@Component //此处注解不能省却(0) 1 public class NtClient { 2 /** 3 * 日志 4 */ 5 private static String clazzNa ...
- idea的service注入mapper报错
一.问题 idea的java项目中,service类中注入mapper报错 二.解决 方法1 在mapper类上加上 @Repository 注解即可,当然不加也行,程序也不回报错,是idea的误报 ...
随机推荐
- js原码工具集
/* 原生js工具集 */ arr是否包含obj function contains1(arr, obj) { var i = arr.length; while (i--) { if (arr[i] ...
- 这真是奇葩的js题目
url:http://javascript-puzzlers.herokuapp.com/ 有兴趣的可以一看,算是比较偏门自我感觉
- Python实现CSV数据的读取--两种方法实现
方法一: 方法二:
- 1023: Pong’s Birds(概率)
1023: Pong’s Birds 时间限制: 1 Sec 内存限制: 128 MB提交: 94 解决: 21[提交][状态][讨论版] 题目描述 In order to train his b ...
- UVALive-3523 Knights of the Round Table (双连通分量+二分图匹配)
题目大意:有n个骑士要在圆桌上开会,但是相互憎恶的两个骑士不能相邻,现在已知骑士们之间的憎恶关系,问有几个骑士一定不能参加会议.参会骑士至少有3个且有奇数个. 题目分析:在可以相邻的骑士之间连一条无向 ...
- 026——VUE中事件修饰符之使用$event与$prevent修饰符操作表单
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Mysql加载配置默认路径
查看命令 mysqld --verbose --help|grep "Default options" -n1 输出结果 11-12:Default options are rea ...
- 快速切题 poj 1002 487-3279 按规则处理 模拟 难度:0
487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 247781 Accepted: 44015 Descr ...
- 【lightoj-1024】Eid (高精度)
[题意] 给定n个数,求这n个数的最小公倍数. [题解] 最小公倍数当然不能按常规方法来求,因为最大的数将近是10000^1000级别的.然鹅最小公倍数怎么搞呢? 这里发现了一个规律: 4 5 6 3 ...
- Docker的大坑小洼(一)
Docker的大坑小洼 Posted on March 2, 2015March 2, 2015 by 孙宏亮 Docker成为云计算领域的新宠儿已经是不争的事实,作为高速发展的开源项目,难免存在这样 ...