zoukankan      html  css  js  c++  java
  • Spring课程 Spring入门篇 4-5 Spring bean装配之基于java的容器注解说明--@Bean

    课程链接:

    1    解析

     1.1  @bean注解定义

     1.2  @bean注解的使用

    2    代码演练

     2.1  @bean的应用不带name

     2.2  @bean的应用带name 

     2.3  @bean注解调用initMethod和destroyMethod

    1    解析

     1.1  @bean注解定义

    @bean标识一个用于配置和初始化一个由springIOC容器管理的新对象的方法(生成ioc容器的bean实例),类似于xml配置的</bean>

      

    1.2  @bean注解和@Configuration的使用

    a  通常注解@bean和@Configuration使用

    b  @bean(name="",initMethod="",destroyMethod="")(详细使用见代码演练部分)

    c   @Configuration 该类创建各种Bean交付IoC容器(Bean类代码为纯净JavaBean)

     

    2    代码演练

    2.1  @bean的应用不带name

    实体类:

    package com.imooc.beanannotation.javabased;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class StoreConfig {
        
        @Bean
        public Store getStringStore(){
            return new StringStore();
        }
    
    }

    测试类:

    package com.imooc.test.beanannotation;
    
    import org.junit.Test;
    
    import com.imooc.test.base.UnitTestBase;
    
    public class TestJavaBased extends UnitTestBase{
        
        public TestJavaBased(){
            super("classpath*:spring-beanannotation.xml");
        }
        
        @Test
        public void testStoreConfig(){
            System.out.println(super.getbean("getStringStore").getClass().getName());
            
        }
        
        
    
    }

    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:component-scan base-package="com.imooc.beanannotation"/>
    
    </beans>

    impl类:

    package com.imooc.beanannotation.javabased;
    
    public class StringStore implements Store {
    
    }

    dao类:

    package com.imooc.beanannotation.javabased;
    
    public interface Store {
    
    }

    打印结果:

    三月 25, 2019 6:44:28 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5a62a404: startup date [Mon Mar 25 06:44:28 CST 2019]; root of context hierarchy
    三月 25, 2019 6:44:28 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanannotation.xml]
    com.imooc.beanannotation.javabased.StringStore
    三月 25, 2019 6:44:29 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@5a62a404: startup date [Mon Mar 25 06:44:28 CST 2019]; root of context hierarchy

    2.2  @bean的应用带name

    实体类:

    package com.imooc.beanannotation.javabased;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class StoreConfig {
        
        @Bean(name="store")
        public Store getStringStore(){
            return new StringStore();
        }
    
    }

    测试类:

    package com.imooc.test.beanannotation;
    
    import org.junit.Test;
    
    import com.imooc.test.base.UnitTestBase;
    
    public class TestJavaBased extends UnitTestBase{
        
        public TestJavaBased(){
            super("classpath*:spring-beanannotation.xml");
        }
        
        @Test
        public void testStoreConfig(){
            System.out.println(super.getbean("store").getClass().getName());
            
        }
        
        
    
    }

    打印结果:

    三月 25, 2019 6:57:00 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2121052: startup date [Mon Mar 25 06:57:00 CST 2019]; root of context hierarchy
    三月 25, 2019 6:57:00 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanannotation.xml]
    com.imooc.beanannotation.javabased.StringStore
    三月 25, 2019 6:57:01 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@2121052: startup date [Mon Mar 25 06:57:00 CST 2019]; root of context hierarchy

    2.3  bean注解调用initMethod和destroyMethod

    实体类:

    package com.imooc.beanannotation.javabased;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class StoreConfig {
        
        @Bean(name="store",initMethod="init",destroyMethod="destroy")
        public Store getStringStore(){
            return new StringStore();
        }
    
    }

    impl类:

    package com.imooc.beanannotation.javabased;
    
    public class StringStore implements Store {
        
        public void init(){
            System.out.println("This is init Method");
        }
        
        public void destroy(){
            System.out.println("This is destroy Method");
        }
        
    }

    打印结果:

    三月 26, 2019 6:15:23 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
    信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5fa34e31: startup date [Tue Mar 26 06:15:23 CST 2019]; root of context hierarchy
    三月 26, 2019 6:15:23 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    信息: Loading XML bean definitions from URL [file:/F:/xiangmu3/Xin/FuQiang/Spring/ddwei-dao/target/classes/spring-beanannotation.xml]
    This is init Method
    com.imooc.beanannotation.javabased.StringStore
    三月 26, 2019 6:15:24 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
    信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@5fa34e31: startup date [Tue Mar 26 06:15:23 CST 2019]; root of context hierarchy
    This is destroy Method



  • 相关阅读:
    《计算机网络 自顶向下方法》 第1章 计算机网络和因特网
    记一次代码优化
    不要刻意寻求局部最优解
    Eclipse Jetty插件安装
    Jetty的工作原理
    log4g 使用教程
    有用资料的网址
    Java 编程 订单、支付、退款、发货、退货等编号主动生成类
    Spring框架
    Eclipse常用快捷键大全1
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/10591670.html
Copyright © 2011-2022 走看看