博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2. Spring 4.2.3前瞻-对java8默认方法(default method)定义Bean的支持
阅读量:4119 次
发布时间:2019-05-25

本文共 1179 字,大约阅读时间需要 3 分钟。

2.1 默认方法(default method)

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

2.2

  • 将要被声明成bean的类
public class DemoService {    public void doSomething(){        System.out.println("find bean in default method");    }}
  • 在接口的默认方法里定义bean
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/

你可能感兴趣的文章
《读书笔记》—–书单推荐
查看>>
【设计模式】—-(2)工厂方法模式(创建型)
查看>>
有return的情况下try catch finally的执行顺序(最有说服力的总结)
查看>>
String s1 = new String("abc"); String s2 = ("abc");
查看>>
JAVA数据类型
查看>>
Xshell 4 入门
查看>>
SoapUI-入门
查看>>
Oracle -常用命令
查看>>
JAVA技术简称
查看>>
ORACLE模糊查询优化浅谈
查看>>
2016——个人年度总结
查看>>
2017——新的开始,加油!
查看>>
【Python】学习笔记——-6.2、使用第三方模块
查看>>
【Python】学习笔记——-7.0、面向对象编程
查看>>
【Python】学习笔记——-7.2、访问限制
查看>>
【Python】学习笔记——-7.3、继承和多态
查看>>
【Python】学习笔记——-7.5、实例属性和类属性
查看>>
Linux设备模型(总线、设备、驱动程序和类)之四:class_register
查看>>
git中文安装教程
查看>>
虚拟机 CentOS7/RedHat7/OracleLinux7 配置静态IP地址 Ping 物理机和互联网
查看>>