zoukankan      html  css  js  c++  java
  • 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.springframework.context.support.ClassPathXmlApplicationContext@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.springframework.context.support.ClassPathXmlApplicationContext@36b8bef7: startup date [Tue Jul 07 16:13:25 CST 2015]; root of context hierarchy
    com.beanannotation.StoreImpl
    this is destory.
  • 相关阅读:
    JS知识点
    JQuery知识点
    常见简单算法
    Html知识点
    Java基础_基本语法
    Java基础_Java概述
    VBA基础——循环语句
    VBA基础知识———常用语句
    VBA基础概念
    安全、结构良好的jQuery结构模板
  • 原文地址:https://www.cnblogs.com/JsonShare/p/4626962.html
Copyright © 2011-2022 走看看