学习 Spring (十) 注解之 @Bean, @ImportResource, @Value
Spring入门篇 学习笔记
@Bean
@Bean 标识一个用于配置和初始化一个由 Spring IoC 容器管理的新对象的方法,类似于 XML 配置文件的
可以在 Spring 的 @Configuration 注解的类中使用 @Bean 注解任何方法,在方法里面创建对象返回
@Configuration
public class AppConfig{
@Bean
public MyService myService(){
return new MyServiceImpl();
}
}
示例
新建类:
public interface Store<T> {
}
public class StringStore implements Store<String> {
public void init() {
System.out.println("This is init.");
}
public void destroy() {
System.out.println("This is destroy.");
}
}
@Configuration
public class StoreConfig {
@Bean(initMethod = "init", destroyMethod = "destroy")// 如果没有指定 name,那么name 为方法的名称
public StringStore stringStore() {
return new StringStore();
}
}
添加测试:
@RunWith(BlockJUnit4ClassRunner.class)
public class TestJavabased extends UnitTestBase {
public TestJavabased() {
super("classpath*:spring-beanannotation.xml");
}
@Test
public void test() {
Store store = super.getBean("stringStore");
System.out.println(store.getClass().getName());
}
}
@Bean 默认是单例的,如果要改变作用域范围,可以再添加 @Scope 注解
@ImportResource
<beans>
<context:annotation-config/>
<!--加载资源文件-->
<context:property-placeholder location="classpath:/com/acme/jdbc.properties"/>
<bean class="com.acme.AppConfig"/>
<!--引用资源文件中的配置内容-->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.username}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
</beans>
使用 @ImportResource 可以代替上面 XML 配置:
@Configuration
@ImportResource("classpath:/com/acme/properties-config.xml")
public class AppConfig{
@Value("${jdbc.url")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DataSource dataSource(){
return new DriverManagerDataSource(url, username, password);
}
}
示例:
添加类:
public class MyDriverManager {
public MyDriverManager(String url, String userName, String password) {
System.out.println("url : " + url);
System.out.println("userName: " + userName);
System.out.println("password: " + password);
}
}
添加配置文件 config.properties:
jdbc.username=root
jdbc.password=root
jdbc.url=127.0.0.1
添加配置文件 config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
<context:property-placeholder location="classpath:/config.properties"/>
</beans>
修改类 StoreConfig:
@Configuration
@ImportResource("classpath:config.xml")
public class StoreConfig {
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public MyDriverManager myDriverManager(){
return new MyDriverManager(url, username, password);
}
// @Bean(initMethod = "init", destroyMethod = "destroy")// 如果没有指定 name,那么name 为 方法的名称
// public StringStore stringStore() {
// return new StringStore();
// }
}
添加测试:
@Test
public void testMyDriverManager() {
MyDriverManager manager = super.getBean("myDriverManager");
System.out.println(manager.getClass().getName());
}
基于泛型的自动装配
示例
新建类:
public class IntegerStore implements Store<Integer> {
}
修改类:
@Configuration
@ImportResource("classpath:config.xml")
public class StoreConfig {
@Autowired
@Qualifier(value="stringStore")
private Store<String> s1;
@Autowired
@Qualifier(value="integerStore")
private Store<Integer> s2;
@Bean
public StringStore stringStore() {
return new StringStore();
}
@Bean
public IntegerStore integerStore() {
return new IntegerStore();
}
@Bean
public StringStore stringStoreTest(){
System.out.println("s1: " + s1.getClass().getName());
System.out.println("s2: " + s2.getClass().getName());
return new StringStore();
}
}
添加测试:
@Test
public void testG() {
Store store = super.getBean("stringStoreTest");
}
源码:learning-spring
学习 Spring (十) 注解之 @Bean, @ImportResource, @Value的更多相关文章
- 学习 Spring (八) 注解之 Bean 的定义及作用域
Spring入门篇 学习笔记 Classpath 扫描与组件管理 从 Spring 3.0 开始,Spring JavaConfig 项目提供了很多特性,包括使用 java 而不是 XML 定义 be ...
- Spring学习(十八)Bean 的三种依赖注入方式介绍
依赖注入:让调用类对某一接口实现类的依赖关系由第三方注入,以移除调用类对某一接口实现类的依赖.接下来将详细的向大家介绍Spring容器支持的三种依赖注入的方式以及具体配置方法:• 属性注入方法• ...
- mybatis源码学习--spring+mybatis注解方式为什么mybatis的dao接口不需要实现类
相信大家在刚开始学习mybatis注解方式,或者spring+mybatis注解方式的时候,一定会有一个疑问,为什么mybatis的dao接口只需要一个接口,不需要实现类,就可以正常使用,笔者最开始的 ...
- 学习 Spring (九) 注解之 @Required, @Autowired, @Qualifier
Spring入门篇 学习笔记 @Required @Required 注解适用于 bean 属性的 setter 方法 这个注解仅仅表示,受影响的 bean 属性必须在配置时被填充,通过在 bean ...
- Spring通过注解装配Bean
通过注解实现ServiceImpl业务 一.使用@Component装配Bean 1. 定义类:User 在类上面加@Component注解,在属性上面加@Value值 package com.wbg ...
- spring 通过注解装配Bean
使用注解的方式可以减少XML的配置,注解功能更为强大,它既能实现XML的功能,也提供了自动装配的功能,采用了自动装配后,程序员所需要做的决断就少了,更加有利于对程序的开发,这就是“约定优于配置”的开发 ...
- 在Listener(监听器)定时启动的TimerTask(定时任务)中使用Spring@Service注解的bean
1.有时候在项目中需要定时启动某个任务,对于这个需求,基于JavaEE规范,我们可以使用Listener与TimerTask来实现,代码如下: public class TestTaskListene ...
- Spring中注解注入bean和配置文件注入bean
注解的方式确实比手动写xml文件注入要方便快捷很多,省去了很多不必要的时间去写xml文件 按以往要注入bean的时候,需要去配置一个xml,当然也可以直接扫描包体,用xml注入bean有以下方法: & ...
- SpringBoot学习之@Configuration注解和@Bean注解
@Configuration 1.@Configuration注解底层是含有@Component ,所以@Configuration 具有和 @Component 的作用. 2.@Configurat ...
随机推荐
- Flask 框架 重定向,捕获异常,钩子方法及使用jsonify在网页返回json数据
Flask 框架中常用到重定向方法来实现路由的跳转 ,路由跳转又分为站内跳转和站外跳转 常用的站内跳转方法为url_for 而常用的站外跳转为redirect 在这里提示一下: 在使用两种方法是须调 ...
- Ubuntu14.04下如何安装TensorFlow
一.安装Anaconda Anaconda官网(www.continuum.io/downloads) 也可以在(https://repo.continuum.io/archive/)上根据自己的操作 ...
- visualbox 安装
1.下载地址:官网 2.安装步骤 3.新建虚拟机
- Luogu P3703 [SDOI2017]树点涂色
比较有趣的综合树上问题,刷LCT题单时做的但是发现后面LCT只是起了辅助作用233 首先我们分析每一个操作,\(1\)的定义就让我们联想到了access,我们回忆一下LCT的性质: LCT中每一个sp ...
- 【Qt】Qt Quick 之 QML 与 C++ 混合编程详解
Qt Quick 之 QML 与 C++ 混合编程详解 - CSDN博客 专栏:Qt Quick简明教程 - CSDN博客 .
- Jlink使用技巧之J-Scope虚拟示波器功能
J-Link简介 J-Link是SEGGER公司为支持仿真ARM内核芯片推出的JTAG仿真器.简单地说,是给一个JTAG协议转换盒.其连接到计算机用的是USB接口,而到目标板内部用的还是jtag协议. ...
- Django model中的class Meta详解
通过一个内嵌类 "class Meta" 给你的 model 定义元数据, 类似下面这样: class Foo(models.Model): bar = models.CharFi ...
- 深入浅出Tomcat系列
原本打算一篇文章就发了的,无奈文章太长,阅读压力较大.为了让阅读体验更好一些,还是分多篇吧,大概6篇. 下面是这个主题的目录: 深入浅出Tomcat/1- 来历和配置文件 深入浅出Tomcat/2 - ...
- 微软是如何让我再次爱上.Net Core和C#的
“为什么你还想用ASP.NET,难道你还活在90年代吗?”这正是我的一位老同事在几年前我们即将开始的项目中我提出考虑使用ASP.NET时所说的话.当时我很大程度上认同他的看法,微软已经开发了伟大的开发 ...
- 百度软件开发实习生c++方向面经(一面)
百度2017实习生软件开发(cpp方向) 首先说一下岗位.分为软件开发,开发测试,前端,机器学习数据挖掘,移动开发,据我观察,报的人数来看,软件开发最多,移动开发和开发测试较少.百度前台还准备了吃的喝 ...