实例一:

DiConfig 文件:
package di;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; //声明当前类是一个配置类
@Configuration
//自动扫描包下的所有@Service,@Component,@Repository和@Controller注册为Bean;
@ComponentScan("di") public class DiConfig { }

  

DFunctionService 文件:
@service相当于 singleton的scope
package di;
import org.springframework.stereotype.Service; //注入:当前类是spring管理的一个bean
//相当于singleton的scope
@Service
public class DFunctionService { public String SayHello(String word)
{
return "Hello, " + word + " !";
} }

  

UseFunctionService 文件:

package di;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; //注入:当前类是spring管理的一个bean
@Service
public class UseFunctionService { //将DFunctionService类的实体Bean注入到UseFunctionService中,让UseFunctionService拥有DFunctionService的功能
//等效注解: @Autowire=@Inject=@Resource
@Autowired
DFunctionService functionService; public String SayHello(String word)
{
return functionService.SayHello(word);
} }

  

Main 文件:
package di;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main { public static void main(String args[])
{ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(DiConfig.class);
UseFunctionService userFunctionService = context.getBean(UseFunctionService.class);
System.out.print(userFunctionService.SayHello("word"));
context.close(); } }

  

运行Main.java ,结果为 hello word

spring boot: scope (一般注入说明(一) @Autowired注解)的更多相关文章

  1. Spring boot 使用@Value注入属性

    Spring boot 使用@Value注入属性 学习了:http://blog.csdn.net/hry2015/article/details/72353994 如果启动的时候报错: spring ...

  2. Java Spring Boot VS .NetCore (八) Java 注解 vs .NetCore Attribute

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  3. 深入Spring Boot:那些注入不了的Spring占位符(${}表达式)

    Spring里的占位符 spring里的占位符通常表现的形式是: <bean id="dataSource" destroy-method="close" ...

  4. Spring Boot集成Quartz注入Spring管理的类

    摘要: 在Spring Boot中使用Quartz时,在JOB中一般需要引用Spring管理的Bean,通过定义Job Factory实现自动注入. Spring有自己的Schedule定时任务,在S ...

  5. spring boot的对象注入

    1 需求 现在我们的项目中需要引入一个java类库,我想要很方便的使用该类库中的一个类,并且我想要创建这个类的一个单例对象.然后可以很方便的在各个模块中用@AutoWired进行对象注入. 比如一个配 ...

  6. Spring/Spring boot正确集成Quartz及解决@Autowired失效问题

    周五检查以前Spring boot集成Quartz项目的时候,发现配置错误,因此通过阅读源码的方式,探索Spring正确集成Quartz的方式. 问题发现 检查去年的项目代码,发现关于QuartzJo ...

  7. Spring Boot 自定义 Shiro 过滤器,无法使用 @Autowired 解决方法

    在 Spring Boot 中集成 Shiro,并使用 JWT 进行接口认证. 为了统一对 Token 进行过滤,所以自定义了一个 JwtTokenFilter 过滤器. 期间遇到了以下几个问题,这里 ...

  8. 深入Spring Boot:那些注入不了的 Spring 占位符 ( ${} 表达式 )

    Spring里的占位符 spring里的占位符通常表现的形式是: 1 2 3 <bean id="dataSource" destroy-method="close ...

  9. Spring Boot 自动配置的原理、核心注解以及利用自动配置实现了自定义 Starter 组件

    本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 Starter 组件 摘录:读书是读完这些文字还要好好用心去想想,写书也一样,做任何事也一样 图 2 第二章目录结构图 第 2 章 Spr ...

随机推荐

  1. Oracle PL/SQL 高级编程

    1. 复合数据类型--记录类型 Ø        语法格式 type  类型名 is   record ( 字段1  字段1类型 [not null]:=表达式1; 字段2  字段2类型 [not n ...

  2. redis+node.js

    1.什么的cache 是一种更快的记忆存储数据集 存储空间有限 储存一部分重要数据 是一种相对的概念,只要比原本数据存储更快的介质就能作为cache 2.caching 策略 有限的存储空间,只能存储 ...

  3. dedecms单独调用指定文章

    {dede:arclist idlist='指定ID' limit='0,1'} <a href="[field:arcurl/]">[field:title/]< ...

  4. Mysql主从复制,实现数据同步

    大型网站为了软解大量的并发访问,除了在网站实现分布式负载均衡,远远不够.到了数据业务层.数据访问层,如果还是传统的数据结构,或者只是单单靠一台服务器扛,如此多的数据库连接操作,数据库必然会崩溃,数据丢 ...

  5. ios 手势返回<1>2

    iOS-给push出来的控制器添加全局滑动(返回)手势   在iOS中,当我们push出一个新的控制器的时候,我们可以向右拖拽屏幕的左边缘来返回(pop)到上一级控制器,但是这个功能有两个缺陷: 当自 ...

  6. ios 获取手机设备信息

    [UIDevice currentDevice]:表示设备 NSString *devices=[[NSString alloc] initWithFormat: @"unique id: ...

  7. 图片剪裁控件——ClipImageView

    这段时间在做自己的项目时,须要使用到图片剪裁功能,当时大概的思考了一些需求.想到了比較简单的实现方法.因此就抽了点时间做了这个图片剪裁控件--ClipImageView 这里先贴上ClipImageV ...

  8. 计算点与x轴正半轴夹角atan2(double y,double x),返回弧度制(-PI,PI]

    精度比acos , asin 什么的高些. Parameters y Value representing the proportion of the y-coordinate. x Value re ...

  9. 1 了解Scala

    1 定义变量 单个变量:var name = "benxintuzi" 等价于  var name : String = "benxintuzi"(即定义变量时 ...

  10. 【BZOJ3316】JC loves Mkk 分数规划+单调队列

    [BZOJ3316]JC loves Mkk Description Input 第1行,包含三个整数.n,L,R.第2行n个数,代表a[1..n]. Output 仅1行,表示询问答案.如果答案是整 ...