实例一:

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中can not set解决方法

    原因:set autotrace on和set trimspool on在pl\sql中使用不了 解决方法:在window环境中,使用cmd命令,sqlplus user_name/password@ ...

  2. bzoj3531【SDOI2014】旅行

    3531: [Sdoi2014]旅行 Time Limit: 20 Sec  Memory Limit: 512 MB Submit: 850  Solved: 433 [Submit][Status ...

  3. Google Code Jam 2014 资格赛:Problem D. Deceitful War

    This problem is the hardest problem to understand in this round. If you are new to Code Jam, you sho ...

  4. linux下LAMP环境搭建

    ++++++++++++++++++++++++++++++++++++++++++++++ linux下LAMP环境搭建 ++++++++++++++++++++++++++++++++++++++ ...

  5. chef简介

    Chef 的简单介绍 Chef 主要分为三个部分 Chef Server.Workstation 以及 Chef Client.用户在 Workstation 上编写 Cookbook.然后,通过 k ...

  6. Swift_4_闭包(Blocks)

    import Foundation println("Hello, World!") var arr = [1,2,4,6,74,2] func hasClosure(list:[ ...

  7. MySQL复制经常使用拓扑结构具体解释

    复制的体系结构有下面一些基本原则: (1)    每一个slave仅仅能有一个master: (2)    每一个slave仅仅能有一个唯一的serverID: (3)    每一个master能够有 ...

  8. poj 2524 Ubiquitous Religions(并查集)

    Ubiquitous Religions Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 23168   Accepted:  ...

  9. 第三课 nodejs读取文件

    //引入文件操作模块var fs = require('fs'); //读取文件 使用 回调函数 utf-8编码读取 a.txt在当前文件目录fs.readFile('a.txt','UTF-8',f ...

  10. [Android]豆瓣FM离线数据

    离线目录结构: /sdcard/Android/data/com.douban.radio下 ./cache/fileCaches: 离线音乐歌词(lyric) ./cache/images: 离线音 ...