zoukankan      html  css  js  c++  java
  • spring_JavaConfig

    从Spring 3起,JavaConfig功能已经包含在Spring核心模块,它允许开发者将bean定义和在Spring配置XML文件到Java类中。

    interface:

    package spring_config;
    
    /**
     * Created by luozhitao on 2017/8/10.
     */
    public interface hello0810 {
    
        public void printMsg(String msg);
    }

    imp:

    package spring_config;
    
    /**
     * Created by luozhitao on 2017/8/10.
     */
    public class hello0810imp implements hello0810{
        public void printMsg(String msg) {
    
            System.out.println("0810"+msg);
    
        }
    }

    使用 @Configuration 注释告诉 Spring,这是核心的 Spring 配置文件,并通过 @Bean 定义 bean。

    package spring_config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * Created by luozhitao on 2017/8/10.
     */
    @Configuration
    public class APPconfig {
    
        @Bean(name = "hellobean")
        public hello0810 hello(){
    
            return new hello0810imp();
        }
    }

    main:

    package spring_config;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    /**
     * Created by luozhitao on 2017/8/10.
     */
    public class APP_main {
    
    
        public static void main(String [] args){
    
    
            ApplicationContext context=new AnnotationConfigApplicationContext(APPconfig.class);
    
            hello0810 ho=(hello0810)context.getBean("hellobean");
    
            ho.printMsg("spring_config");
    
    
    
    
    
        }
    
    }

     -----------------------

    使用@Import加载多个配置文件。

    package com.yiibai.config;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Import;
    
    @Configuration
    @Import({ CustomerConfig.class, SchedulerConfig.class })
    public class AppConfig {
    
    }
  • 相关阅读:
    jperf windows
    Eclipse+Maven命令创建webapp项目<三>
    Eclipse+Maven创建webapp项目<二>
    Eclipse+Maven创建webapp项目<一>
    在java中定义有几种类加载器
    JAVA创建对象有哪几种方式 ?
    js创建对象的几种常用方式小结(推荐)
    maven安装以及eclipse配置maven
    MyEclipse 10.0安装及激活步骤
    jdk下载网址
  • 原文地址:https://www.cnblogs.com/luo-mao/p/7338009.html
Copyright © 2011-2022 走看看