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;
            
        }
    }
  • 相关阅读:
    「2019冬令营提高组」原样输出
    FJWC2019
    P2763 试题库问题
    P3979 遥远的国度
    P2754 [CTSC1999]家园
    P1251 餐巾计划问题
    P1382 楼房
    P1384 幸运数与排列
    P4294 [WC2008]游览计划
    P3345 [ZJOI2015]幻想乡战略游戏
  • 原文地址:https://www.cnblogs.com/hewenwu/p/3813147.html
Copyright © 2011-2022 走看看