Spring学习(13)--- 基于Java类的配置Bean 之 @Configuration & @Bean注解

基于Java配置选项,可以编写大多数的Spring不用配置XML,但有几个基于Java的注释的帮助下解释。从Spring3.0开始支持使用java代码来代替XML来配置Spring,基于Java配置Spring依靠Spring的JavaConfig项目提供的很多优点。通过使用@Configuration, @Bean ,@Import ,@DependsOn 来实现Java配置Spring.

1) @Configuration & @Bean 注解:

在Spring的新的Java-Configuration的中间产物是基于类的@Configuration的注解和基于方法的@Bean注解。

@Bean注解是用来指明方法的实例化,配置和初始化一个对象是通过Spring的IoC容器来管理的。对于那些熟悉使用以XML配置Spring的<beans /> 标签,@Bean注解和<bean />标签是起相同作用的。可以在Spring的@Component注解中的类使用@Bean注解任何方法(仅仅是可以),但是,通常使用的是@Configuration

@Configuration注解的类指明该类主要是作为一个bean的来源定义。此外,@Configuration定义的classes允许在同一个类中使用@Bean定义的方法来定义依赖的bean

注释类与@Configuration表示这个类可以使用Spring IoC容器为bean定义来源。在@Bean 注解告诉Spring的注解为@Bean的一个方法将返回应注册为在Spring应用程序上下文中的bean对象。

@Configuration
public class MovieFinder {

	@Bean
	public InjectionService injectionService(){
		return new InjectionServiceImpl();
	}
}

上面的代码将等同于下面的XML配置:

<bean id="movieFinder " class="com.mypackage.MovieFinder"></bean>

例子:

定义Store接口,及实现类StoreImpl

package com.beanannotation;

public interface Store {

}
package com.beanannotation;

public class StoreImpl implements Store {

	public void init(){
		System.out.println("this is init.");
	}

    public void destory(){
    	System.out.println("this is destory.");
	}
}

定义StoreConfig:

package com.beanannotation;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class StoreConfig {

	@Bean(name="store",initMethod="init",destroyMethod="destory")//若不指定name,默认为方法名
	public Store getStore(){
		return new StoreImpl();
	}
}

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-4.1.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.1.xsd">

        <context:component-scan base-package="com.beanannotation">
        </context:component-scan> 

</beans>

单元测试:

package com.beanannotation;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UnitTest {

	@Test
	public void test(){
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-beanannotation.xml");
		Store service=(Store)context.getBean("store");
		System.out.println(service.getClass().getName());
		context.close();   // 关闭 Spring 容器,以触发 Bean 销毁方法的执行
	}
}

结果:

2015-7-7 16:13:25 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org[email protected]36b8bef7: startup date [Tue Jul 07 16:13:25 CST 2015]; root of context hierarchy
2015-7-7 16:13:25 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-beanannotation.xml]
this is init.
2015-7-7 16:13:25 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org[email protected]36b8bef7: startup date [Tue Jul 07 16:13:25 CST 2015]; root of context hierarchy
com.beanannotation.StoreImpl
this is destory.
时间: 2024-08-06 20:01:05

Spring学习(13)--- 基于Java类的配置Bean 之 @Configuration & @Bean注解的相关文章

【Spring】IOC之基于Java类的配置Bean

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 基于Java配置选项,可以编写大多数的Spring不用配置XML,但有几个基于Java的注释的帮助下解释.从Spring3.0开始支持使用java代码来代替XML来配置Spring,基于Java配置Spring依靠Spring的JavaConfig项目提供的很多优点.通过使用@Configuration, @Bean ,@Importand,@DependsOnannotations来实现J

(五)使用注解开发,基于java类进行配置

1.使用注解代替bean 配置扫描哪些包下的注解 <!--指定注解扫描包--> <context:component-scan base-package="com.alan.pojo"/> 在指定包下编写类,增加注解 @Component("user") // 相当于配置文件中 <bean id="user" class="当前注解的类"/> public class User { publ

Spring学习(13)--- 基于Java类的配置Bean 之 @ImportResource &amp; @Value 注解

学习如何使用@ImportResource 和 @Value 注解进行资源文件读取 例子: 先创建一个MyDriverManager类(模拟读取数据库配置信息) package com.beanannotation; public class MyDriverManager { public MyDriverManager(String url,String username,String password){ System.out.println("url : "+url); Sys

Spring学习(16)--- 基于Java类的配置Bean 之 基于泛型的自动装配(spring4新增)

例子: 定义泛型Store package javabased; public interface Store<T> { } 两个实现类StringStore,IntegerStore package javabased; public class IntegerStore implements Store<Integer> { } package javabased; public class StringStore implements Store<String>

Spring学习(15)--- 基于Java类的配置Bean 之 @Bean &amp; @Scope 注解

默认@Bean是单例的,但可以使用@Scope注解来覆盖此如下: @Configuration public class MyConfiguration { @Bean @Scope("prototype") public MovieCatalog movieCatalog(){ //... } } Bean的作用域包括singleton.prototype.request.session.global session 例子:

开涛spring3(12.4) - 零配置 之 12.4 基于Java类定义Bean配置元数据

12.4  基于Java类定义Bean配置元数据 12.4.1  概述 基于Java类定义Bean配置元数据,其实就是通过Java类定义Spring配置元数据,且直接消除XML配置文件. 基于Java类定义Bean配置元数据中的@Configuration注解的类等价于XML配置文件,@Bean注解的方法等价于XML配置文件中的Bean定义. 基于Java类定义Bean配置元数据需要通过AnnotationConfigApplicationContext加载配置类及初始化容器,类似于XML配置文

Spring Ioc 基于Java的容器配置

一.基于Java的容器配置 @Configuration & @Bean 注解: 在Spring的新的Java-Configuration的中间产物是基于类的@Configuration的注解和基于方法的@Bean注解.         @Bean注解是用来指明方法的实例化,配置和初始化一个对象是通过Spring的IoC容器来管理的.对于那些熟悉使用以XML配置Spring的<beans /> 标签,@Bean注解和<bean />标签是起相同作用的.你能和Spring的@

[译]17-spring基于java代码的配置元数据

spring还支持基于java代码的配置元数据.不过这种方式不太常用,但是还有一些人使用.所以还是很有必要介绍一下. spring基于java代码的配置元数据,可以通过@Configuration注解把一个声明为配置类;通过@Bean注解把一个新 创建的类交由spring容器来管理.在这种配置方式下,我们可以手动装配bean,也可以自动装配bean.我感觉在这种 方式下使用手动装配非常不爽,尤其是有多个配置类的时候. 下面看个例子: 1.新建包com.tutorialspoint.javacod

spring学习2:基于注解+xml实现ioc和依赖注入

spring学习2:基于注解+xml实现ioc和依赖注入 一.在spring配置文件中开启spring对注解的支持 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&qu