zoukankan      html  css  js  c++  java
  • Spring基于java的配置

    第一步:在XML中配置java信息,与自动检测配置XML一样:

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

    第二步:定义配置类

    cond

    第三步:声明bean和bean的注入:

    package com.springinaction.springidol;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * 这个类是使用java来配置Spring的configuration类,@configuration标注了该类为配置类
     **/
    
    @Configuration
    public class SpringIdolConfig {
    
        
        @Bean
        //函数名就是bean的id
        public Performer duke(){
            //调用Juggler的构造方法即可,此处15相当于是通过构造器方法注入了值
            return new Juggler(15);
        }
        
        @Bean
        public Performer kenny(){
            Instrumentalist kenny = new Instrumentalist();
            //通过set方法注入值
            kenny.setSong("Jingle bell");
            return kenny;
        }
        
        @Bean
        public Poem sonnet29(){
            return new Sonnet29();
        }
        
        //在一个bean中注入另一个bean,sonnet29()方法返回的是上下文中的同一个实例
        @Bean
        public Performer poeticDuke(){
            //此处通过构造方法注入了一个bean的引用
            return new PoeticJuggler(sonnet29());
        }
        
        @Bean
        public Performer poeticDuke1(){
            
            PoeticJuggler poeticDuke1 = new PoeticJuggler();
            //此处通过setter方法注入了一个bean的引用
            poeticDuke1.setPoem(sonnet29());
            return poeticDuke1;
            
        }
    }
  • 相关阅读:
    Nova中的系统状态分析
    nova Flavors
    NovaException: Unexpected vif_type=binding_failed
    nova Reboot (重启)
    nova Rescue 和 Unrescue
    nova shelve
    nova Evacuate
    ceilometer alarm 创建过程中的DB操作及优化
    Ceilometer 数据库比较
    network namespace连接的4种方法及性能
  • 原文地址:https://www.cnblogs.com/hewenwu/p/3813147.html
Copyright © 2011-2022 走看看