@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. 1-26-1-expect无交互式-正则表达式

    大纲: 1.expect环境搭建及脚本编写 概述 expect脚本详解 expect环境搭建 expect脚本实现ssh远程连接 expect脚本实现ssh远程连接(通过shell传递参数) 2.正则 ...

  2. hdu4678 Mine 规律或者博弈。(博弈的sg函数不懂我是找的规律)

    链接:题意就是告诉你一个扫雷图里面每个雷的位置,有两个人,每个人都知道雷的确切位置,每个人一次可以点一部,问你谁能赢. 链接:http://acm.hdu.edu.cn/showproblem.php ...

  3. JavaScript---循环与闭包

    循环与闭包 先看一个demo <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  4. laravel中新建文件并保存数据到文件中

    //base_path()获取当前的绝对路径 $path=base_path().'\config\web.php'; $str='abcdefg'; //要声明的字符串 file_put_conte ...

  5. js Worker 线程

    在平时的运行的javascript脚本都在主线程中执行,如果当前脚本包含复杂的.耗时的代码.那么JavaScript脚本的执行将会被阻塞,甚至整个刘看齐都是提示失去响应. 例子: 假设程序需要计算.收 ...

  6. log4j2 输入日志到flume

    最近想将服务的运行日志收集起来,首先了解到flume技术栈 采用flume方案定了之后有两种方式实现 1: 在应用中,log4j2直接发送日志信息到flume , 2: 通过监控log4j2 产生的日 ...

  7. Linux下常用压缩、解压缩命令

    常用的压缩工具 一是单纯的单文件压缩工具,主要有compress,gzip,bzip2. 二是打包压缩工具,也是最常用的,tar 压缩比:compress<gzip<bzip2    co ...

  8. 20165202 2017-2018-2《Java程序设计》课程总结

    每周作业链接汇总 ++预备作业一:我期待的师生关系++ ++预备作业二:学习基础和C语言基础调查++ ++预备作业三:linux安装及学习++ ++第一周作业:初识JAVA,注册码云并配置Git++ ...

  9. jquery表单验证插件 jquery.form.js-转

    来自:http://www.cnblogs.com/luluping/archive/2009/04/15/1436177.html Form插件,支持Ajax,支持Ajax文件上传,功能强大,基本满 ...

  10. Android 仿微信朋友圈查看

    项目要做一个类似于这样的功能,就做了. 项目下载地址:http://download.csdn.net/detail/u014608640/9917626 一,看下效果: 二.activity类 pu ...