`
wiselyman
  • 浏览: 2080216 次
  • 性别: Icon_minigender_1
  • 来自: 合肥
博客专栏
Group-logo
点睛Spring4.1
浏览量:81031
74ae1471-94c5-3ae2-b227-779326b57435
点睛Spring MVC4...
浏览量:130111
社区版块
存档分类
最新评论

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

 
阅读更多

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

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

 

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


0
0
分享到:
评论

相关推荐

    thymeleaf-extras-springsecurity-3.0-master.zip

    Spring Security version 3.0.x to 4.2.x Web environment (Spring Security integration cannot work offline) Maven info groupId: org.thymeleaf.extras artifactId: Spring Security 3 integration package: ...

    Spring Security-3.0.1中文官方文档(翻译版)

    Spring Security-3.0.1 中文官方文档(翻译版) 这次发布的Spring Security-3.0.1 是一个bug fix 版,主要是对3.0 中存在的一些问题进 行修 正。文档中没有添加新功能的介绍,但是将之前拼写错误的一些类名进行...

    spring-boot-reference.pdf

    8. Introducing Spring Boot 9. System Requirements 9.1. Servlet Containers 10. Installing Spring Boot 10.1. Installation Instructions for the Java Developer 10.1.1. Maven Installation 10.1.2. Gradle ...

    spring 配置文件简单说明

    1)default-init-method ="方法名" 定义在此配置文件中的bean都会执行指定的init方法。 2)default-destroy-method="方法名" 定义在此配置文件中的bean都会在执行指定的destroy方法。 3)default-lazy-init ="false|...

    spring3.2+strut2+hibernate4

    -- 启用spring注解支持 --> <context:annotation-config /> <!-- <bean id="sessionFactory"--> <!-- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">--> <!-- ...

    SpringSecurity 3.0.1.RELEASE.CHM

    2.3.4. 对OpenID的支持 2.3.4.1. 属性交换 2.3.5. 添加你自己的filter 2.3.5.1. 设置自定义 AuthenticationEntryPoint 2.4. 保护方法 2.4.1. <global-method-security>元素 2.4.1.1. 使用protect-pointcut添加...

    Spring Security 中文教程.pdf

    2.3.4. 对OpenID的支持 2.3.4.1. 属性交换 2.3.5. 添加你自己的filter 2.3.5.1. 设置自定义 AuthenticationEntryPoint 2.4. 保护方法 2.4.1. <global-method-security> 元素 2.4.1.1. 使用protect-...

    Spring.html

    实例工厂 <bean bean-factory="" factory-method=""> 管理对象 对象关系DI 构造器注入<construct-arg> set注入 生命周期 scope:prototype/singleton init-method destroy-method API ...

    spring-xmemcached

    spring+xmemcached aop切面 需要xmemcached-1.2.5+spring-2.5.6 <bean name="factoryMemcachedClient" class="net.rubyeye.xmemcached.utils.XMemcachedClientFactoryBean" destroy-method="shutdown"> $...

    spring-framework-reference-4.1.2

    3.3. Java 8 (as well as 6 and 7) ............................................................................... 17 3.4. Java EE 6 and 7 ..................................................................

    spring1.2学习心得分享

    资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用关系的控制. (3)DI概念 Dependecy ...

    spring学习心得

    资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用关系的控制. (3)DI概念 Dependecy ...

    spring_MVC源码

    弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...

    springmybatis

    ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; Insert INTO `user` VALUES ('1', 'summer', '100', 'shanghai,pudong'); 到此为止,前期准备工作就完成了。下面开始真正配置mybatis项目了。 1. 在...

    springframework.5.0.12.RELEASE

    Allow java.time types for setting the Last-Modified header [SPR-17571] #22103 StringHttpMessageConverter should assume charset UTF-8 for application/json [SPR-17568] #22100 NettyDataBufferFactory.join...

    Spring的学习笔记

    一、 建立一个新的项目 8 二、 建立spring的配置文件 8 三、 引入spring的jar包 8 四、 测试代码: 8 五、 注意接口的使用: 8 第五课:IOC(DI)配置及应用 9 一、 什么是IOC、DI 9 二、 编辑xml文件时,没有提示 9 ...

    Spring3中配置DBCP,C3P0,Proxool,Bonecp数据源

    在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: ...

    spring1.1开发理解

    资源释放:<bean destroy-method=""/>仅对单例对象有效 (2)IoC概念 Inversion of Control 控制反转或控制转移 Don't Call Me,We will call you! 控制权:对象的创建和调用关系的控制. (3)DI概念 Dependecy ...

    spring-framework-reference4.1.4

    3.3. Java 8 (as well as 6 and 7) ............................................................................... 17 3.4. Java EE 6 and 7 ..................................................................

Global site tag (gtag.js) - Google Analytics