@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. ASP.NET 4.5 MVC 4 无法运行在Windows2008的IIS7.0上显示404的解决方案

    需要在web.config下加上这个 <system.webServer> <modules runAllManagedModulesForAllRequests="tru ...

  2. Calendar时间获取天,周,月,季度,年度时间段

    Date类 Date类的大多数构造函数和方法都已经抛弃,只留下和毫秒有关的操作,目前常用的构造函数 常用方法 设置毫秒数 读取毫秒数 toString()打印结果 //创建日期对象,把当前的毫秒值转成 ...

  3. classpath到底指的哪里

    之前一直对classpath不太明白到底指的哪里,今天研究了一下,做个总结.. classpath顾名思义就是指类路径,但是这样解释可能还是不明白,这里拿一个SpringBoot应用编译后生成的tar ...

  4. 第六天 文件的基本管理和xfs文进系统备份恢复

    1.1 Linux系统目录结构,相对路径/绝对路径 1.1.1 Linux系统目录结构 在linux系统中一切都是文件 / 根目录,一切的起点,就像是一个树杈一样,他是所有叉的根 /bin 在单用户模 ...

  5. 【51nod-1010】因子只含有2 3 5的数

    K的因子中只包含2 3 5.满足条件的前10个数是:2,3,4,5,6,8,9,10,12,15. 所有这样的K组成了一个序列S,现在给出一个数n,求S中 >= 给定数的最小的数. 例如:n = ...

  6. 基于IDEA和Maven的SSM分层项目搭建

    前言 虽然现在SpringBoot开始流行,但是SSM作为一个经典框架,还是有必要去了解一下. 项目建立 1.新建一个空白的Maven项目,如下图.然后把IDEA自动生成的多余src目录删掉. 2.右 ...

  7. 4: 模块化应用程序开发 Modular Application Development Using Prism Library 5.0 for WPF (英汉对照版)

    A modular application is an application that is divided into a set of loosely coupled functional uni ...

  8. h5和app原生联调触发方法

    //路径跳转 urlHref(item) {//人物.访谈.动态是一个页面 var para = {}; para.title = "动态详情"; para.type = &quo ...

  9. wxpython的简单的应用

    import wximport wx.xrc import pandas as pd from conf.env import * # 允许选择的文件wildcard = "Python s ...

  10. LeetCode OJ:Nim Game(Nim游戏)

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...