@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 进行注入的更多相关文章

  1. spring boot: scope (一般注入说明(一) @Autowired注解)

    实例一: DiConfig 文件: package di; import org.springframework.context.annotation.ComponentScan; import or ...

  2. 解决service层无法注入

    练手时发现个问题,路径404,各种检查发现,多加了一层<context:component-scan base-package="com.yanan.controller"/ ...

  3. Springboot 定时任务,service层无法注入问题详细解决

    开发一个微信小程序后台,建立websocket 长连接,需要后台开启定时任务, 定时任务定时查库,相应前台 但是具体执行过程中一直在报空指针错误,最后定位到service 为空,无法调用其相关的方法导 ...

  4. quartz整合spring框架service层对象注入为null解决方案

    Job实现类代码 package cn.itcast.quartz; import org.quartz.Job; import org.quartz.JobExecutionContext; imp ...

  5. SpringBoot拦截器中service或者redis注入为空的问题

    原文:https://my.oschina.net/u/1790105/blog/1490098 这两天遇到SpringBoot拦截器中Bean无法注入问题.下面介绍我的思考过程和解决过程: 1.由于 ...

  6. service里无法注入mapper,mapper空指针

    被困扰了一天,终于解决了,记录一下 下面是mapper的代码 @Mapper public interface ProductDao { @Select("select * from pro ...

  7. Ionic之$scope 依赖注入报错

    在开发Ionic过程中,发现会报在 LoginController 中引用locals报错,具体报错问题: ionic.bundle.js:19526 Error: [$injector:unpr] ...

  8. 关于工具类静态方法调用@Autowired注入的service类问题

    @Component //此处注解不能省却(0) 1 public class NtClient { 2 /** 3 * 日志 4 */ 5 private static String clazzNa ...

  9. idea的service注入mapper报错

    一.问题 idea的java项目中,service类中注入mapper报错 二.解决 方法1 在mapper类上加上  @Repository 注解即可,当然不加也行,程序也不回报错,是idea的误报 ...

随机推荐

  1. 【BZOJ3144】切糕(网络流,最小割)

    [BZOJ3144]切糕(网络流,最小割) 题面 BZOJ 题解 这样的类型很有趣 先不考虑相邻距离差不能超过\(D\)的限制 我们考虑答案,显然就是在每个位置选一个最小的高度割就行了 化成最小割的模 ...

  2. Git 从了解到放弃

    1. 简单介绍 1.1. git起源 在1991年linus创建了Linux从此linux成为服务器领域的佼佼者,大部分web服务器.邮件.数据库各种服务器端程序都安装在了linux上面运行,主要是因 ...

  3. 使用群晖NAS:配置Git server

    1.首先在群晖的DSM的控制面板中创建一个用户例如是Git_test(我给了管理员权限) 2.在套件中心安装 Git server 3.打开Git server 勾选用户 Git_test 4.在控制 ...

  4. JDK的KeyTool和KeyStore等加密相关

    Keytool是一个有效的安全钥匙和证书的管理工具. Java 中的 keytool.exe (位于JDK\Bin下)可以用来创建数字证书,所有的数字证书是以一条一条(用别名区别,不区分大小)地存储在 ...

  5. html表格导出Excel的一点经验心得(转载)

    最近在做统计功能,要求统计结果(表格)既能查看(BS系统,在浏览器查看),又能输出为excel文件.对于输出excel文件,在网上找到n种方案,因为还需查看,最终选择了统计结果输出为table,查看时 ...

  6. 【zzuli-1923】表达式求值

    题目描述 假设表达式定义为:1. 一个十进制的正整数 X 是一个表达式.2. 如果 X 和 Y 是 表达式,则 X+Y, X*Y 也是表达式; *优先级高于+.3. 如果 X 和 Y 是 表达式,则 ...

  7. Windows 下 左Ctrl和Caps交换

    将以下文本存为后缀为.reg文件,运行并重启即可 左Ctrl和Caps交换 Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTE ...

  8. JFinal源码详解

    JFinal的框架我24号的一篇博文写到过,它优秀的地方在精简代码上,那么有两处源码是我觉得是值得我们要好好解析一下,一处是初始化加载—servlet跳转,另一处是DB+ActiveRecord的映射 ...

  9. jq的.off解绑事件

    .off( events [, selector ] [, handler ] ) 描述:移除一个事件处理函数 events 类型: String 一个或多个空格分隔的事件类型和可选的命名空间,或仅仅 ...

  10. 我也说说Emacs吧(2) - Emacs其实就是函数的组合

    Emacs本质上是函数的组合 从帮助上看emacs有何不同 Vim和Sublime Text等编辑器,本质上是一个编辑器. 比如我们看看vim的帮助,是这个风格的,比如我要看i命令的帮助: <i ...