default medthod
;default
关键字public class DemoService {
public void doSomething(){
System.out.println("find bean in default method");
}
}
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