首页 > 编程语言 > 详细

Spring 4.2新特性-对java8默认方法(default method)定义Bean的支持

时间:2015-06-05 02:11:40      阅读:822      评论:0      收藏:0      [点我收藏+]

2.1 默认方法(default method)

  • java8引入了一个default medthod;
  • 用来扩展已有的接口,在对已有接口的使用不产生任何影响的情况下,添加扩展
  • 使用default关键字
  • Spring 4.2支持加载在默认方法里声明的bean

2.2

  • 将要被声明成bean的类
public class DemoService {
    public void doSomething(){
        System.out.println("find bean in default method");
    }
}
  • 在接口的默认方法里定义bean
package com.wisely.spring4_2.defaultMethod;

import org.springframework.context.annotation.Bean;
public interface DemoServiceConfig {

    @Bean 
    default DemoService DemoService(){
        return new DemoService();
    }

}
  • 配置类
package com.wisely.spring4_2.defaultMethod;

import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig implements DemoServiceConfig{



}

  • 运行
package com.wisely.spring4_2.defaultMethod;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
         AnnotationConfigApplicationContext context =
                    new AnnotationConfigApplicationContext("com.wisely.spring4_2.defaultMethod");
         DemoService ds = context.getBean(DemoService.class);
         ds.doSomething();
    }
}

  • 输出结果
find bean in default method

Spring 4.2新特性-对java8默认方法(default method)定义Bean的支持

原文:http://wiselyman.iteye.com/blog/2217069

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!