使用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的误报 ...
随机推荐
- Android我的便签-----SQLite的使用方法
在Android开发中也有数据库的存在,最近有空,把以前写的一个便签来讲述一下Android中的数据库,跟大家分享分享的,希望对大家有所帮助. SQLite简介 SQLite,是一款轻量级的关系型数据 ...
- 如何把数字字符'1'转换为整数(java 实现)
在一些表达式计算时,如 “3+2” 表达式自身是个字符串,通过切片得到的是数字字符和操作符,不能直接进行计算,在表达式计算中需要进行一步操作是,把数字字符'2','3'转化为整数. 如何操作? ...
- 三重Des对称加密在Android、Ios 和Java 平台的实现
引言 如今手机app五彩缤纷,确保手机用户的数据安全是开发人员必须掌握的技巧,下面通过实例介绍DES在android.ios.java平台的使用方法: DES加密是目前最常用的对称加密方式, ...
- 身份证真实性校验js、mini ui身份证长度正则验证
身份证号码真实性校验 <input type="text" value="请输入身份证号" id="cards" ><bu ...
- mysql数据库的笔记
增删改查置顶: 插入数据: 基本语法 : insert into [表名](字段名1,字段名2……) values(记录1),(记录2): insert into [表名] values(记录1),( ...
- 剑指offer--43.连续子数组的最大和
最大子段和,最大能取所有 ---------------------------------------------------------------- 时间限制:1秒 空间限制:32768K 热度 ...
- New Concept English three(15)
31w/m 43 Children always appreciate small gifts of money. Father, of course, provides a regular supp ...
- Windows 64位系统下安装JAVA环境
文件名称:jdk-7u51-windows-x64 下载Windows 64位平台的JDK安装文件. 安装 1.双击打开下载好的文件,点击“下一步”开始安装. 2. ①默认选择安装全部功能,不做调整. ...
- sql密码修改
首先,以window 认证登陆. 2 然后,在 安全性---->登录名---->sa.右击 选择属性.直接修改 星号密码即可. 3 然后 在右上角 文件--->链接对象资源管理器-- ...
- Jsoup教程,jsoup开发指南,jsoup中文使用手册,jsoup中文文档
jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据. jsou ...