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;
            
        }
    }
  • 相关阅读:
    css引入方式
    HTML标签
    动态导入模块impoerlib
    pymysql连接数据库
    创建数据库表之引擎
    IO多路复用互动聊天,select函数监听
    欧拉筛法求素数个数
    与三角形相关的问题 WITH 有向面积
    时间复杂度的计算
    折半查找
  • 原文地址:https://www.cnblogs.com/hewenwu/p/3813147.html
Copyright © 2011-2022 走看看