`
wandejun1012
  • 浏览: 2693986 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

autowired resource component

    博客分类:
  • java
 
阅读更多

背景:同样的bean类型有2个。

尝试用autowired+componet或者resource+component来解决。

 

 

1、component指定名称示例,id为maizims

package hello;

import org.springframework.stereotype.Component;

@Component("maizims")
public class MessageServiceImpl implements MessageService {

	public String getMessage(String str) {
		return "麦子金服欢迎"+str;
	}

}

 

可以观察到,直接在component后面的括号里面加上一个字符串即可。字符串的名字就是beanId。

 

 

2、autowired。

可以放在成员变量上面,也可以放在构造方法上。

如果有多个bean,需要结合@qualifier使用。

使用方法也简单,直接在qualifier后面的括号里面加个字符串,代表引用的bean的Id

package germmy.groovy

import hello.MessageService


import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier;

class Simple implements ScriptService {
 
        @Autowired
	@Qualifier("maizims")
    private MessageService messageService
 
    public void run() {
        String name = messageService.getMessage("16楼MM");
        System.out.println(name);
    }
}

  

 

 

3、利用resource。

举例:

 

package germmy.groovy

import hello.MessageService


import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Qualifier;

class Simple implements ScriptService {
 

    @Resource(name="maizims")
    private MessageService messageService
 
    public void run() {
        String name = messageService.getMessage("16楼MM");
        System.out.println(name);
    }
}

 

 总结,既然是用到id来识别的,比如maizims。这个就相当于xml文件中的beanId。

 

resource用法举例:http://stackoverflow.com/questions/4462466/autowiring-two-different-beans-of-same-class

 

autowired和qualifier举例:http://stackoverflow.com/questions/15516769/spring-autowire-two-beans-of-the-same-class-which-are-not-defined-in-applicatio

 

 

配置文件外挂的用法:http://stackoverflow.com/questions/23555149/spring-3-mvc-using-component-value-to-read-properties-from-a-file-is-not-wor

 

component扫描多个包的写法,很简单,再加一个就行:https://segmentfault.com/q/1010000000686297

 

http://www.mkyong.com/spring/spring-autowiring-qualifier-example/   qualifier举例。

分享到:
评论

相关推荐

    spring4框架系列 [ 5 ]

    spring4框架系列 [ 5 ] 基于注释注入@Component @Value @Resource @Autowired @PostConstruct @PreDestroy

    基于注解的DI.docx

    @Component、创建对象 @Respotory、创建dao对象,用来访问数据库 @Service、创建Service对象,处理业务逻辑,可以有事务的功能 @Controller、创建控制器对象,接收请求,显示处理结果 @Value、简单类型的...

    Spring Boot最常用的30个注解.docx

    4 @Component 5 @Bean 6 @Configuration 三、 注入相关 1 @Resource 2 @Autowired 3 @Qualifier 4 @value 5 @ConfigurationProperties 四、 HTTP请求相关 1 @GetMapping 2 @PostMapping 3 @RequestMapping 五、 前后...

    SpringBoot常用注解详解含使用示例(值得珍藏)

    包括@SpringBootApplication、@Component、@Service、@Repository、@Controller、@RequestMapping、@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@Autowired、@Resource、@Profile、@Value、@...

    Spring注解 - 52注解 - 原稿笔记

    注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...

    springMVC详解以及注解说明

    • @Autowired • @RequestMapping • @RequestParam • @ModelAttribute • @Cacheable • @CacheFlush • @Resource • @PostConstruct • @PreDestroy • @Repository • @Component (不推荐使用) ...

    java7源码-spring-boot-starter-lock:spring分布式锁

    java7 ...@resource 指定 lockName 的方式获取lockBean * 当然你也可以使用 @Autowired + @Qualifier("test_lock") 获取lockBean * 注意:如果不指定lockName 则默认获取到的 名称为:global_lock 的 l

    Spring.html

    Component:不分层的注解 Controller:web层 Service:service层 Repository:dao层 管理对象 注入 AutoWired Qualifier Resource Value 声明周期 Scope PostConstruct PreDestroy 新注解 ...

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    3.3.2. Qualifying autowired beans 3.4. Scoping beans 3.4.1. Working with request and session scope 3.4.2. Declaring scoped proxies in XML 3.5. Runtime value injection 3.5.1. Injecting external values ...

    Spring中文帮助文档

    3.12.1. @Component和更多典型化注解 3.12.2. 自动检测组件 3.12.3. 使用过滤器自定义扫描 3.12.4. 自动检测组件的命名 3.12.5. 为自动检测的组件提供一个作用域 3.12.6. 用注解提供限定符元数据 3.13. 注册一...

    Spring API

    3.12.1. @Component和更多典型化注解 3.12.2. 自动检测组件 3.12.3. 使用过滤器自定义扫描 3.12.4. 自动检测组件的命名 3.12.5. 为自动检测的组件提供一个作用域 3.12.6. 用注解提供限定符元数据 3.13. 注册一...

    springboot学习思维笔记.xmind

    @Component组件,没有明确的角色 @Service在业务逻辑层(service层) @Repository在数据访问层(dao层) @Controller在展现层(MVC→SpringMVC) 注入Bean的注解 @Autowired:Spring提供的注解...

    Spring 3 Reference中文

    4.9.2 @Autowired和@Inject.. 86 4.9.3 使用限定符来微调基于注解的自动装配 89 4.9.4 CustomAutowireConfigurer. 94 4.9.5 @Resource.. 95 4.9.6 @PostConstruct和@PreDestroy . 96 4.10 ...

    springmybatis

    1. 在MyBatis 里面创建两个源码目录,分别为 src_user,test_src, 用如下方式建立,鼠标右键点击 JavaResource. 2. 设置mybatis 配置文件:Configuration.xml, 在src_user目录下建立此文件,内容如下: 程序代码 程序...

    spring-framework-reference4.1.4

    Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................

    spring-framework-reference-4.1.2

    Not Using Commons Logging ................................................................... 12 Using SLF4J ..............................................................................................

    spring3.1中文参考文档

    spring3.1中文参考文档,南磊翻译,现在有4章,目录如下: 第一部分 Spring framework概述.......................................................................................................................

Global site tag (gtag.js) - Google Analytics