本文共 1179 字,大约阅读时间需要 3 分钟。
default medthod
;default
关键字public class DemoService { public void doSomething(){ System.out.println("find bean in default method"); }}
package com.wisely.spring4_2.3defaultMethod;import org.springframework.context.annotation.Bean;public interface DemoServiceConfig { @Bean(name="demoService") default DemoService DemoService(){ return new DemoService(); }}
package com.wisely.spring4_2.3defaultMethod;import org.springframework.context.annotation.Configuration;@Configuration
public class AppConfig implements DemoServiceConfig{}
package com.wisely.spring4_2.3defaultMethod;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
转载地址:http://hjcpi.baihongyu.com/