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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring中,Bean是由Spring IoC容器实例化、组装和管理的对象。Spring容器可以管理singleton作用域的Bean的生命周期,包括实例化、初始化和销毁。对于prototype作用域的Bean,Spring容器只负责创建实例,不管理其生命周期。 Bean的生命周期包括以下几个步骤: 1. 根据配置信息调用Bean构造方法或工厂方法实例化Bean。 2. 利用依赖注入完成属性注入。 3. 如果Bean实现了BeanNameAware接口,则Spring调用Bean的setBeanName()方法传入当前Bean的id值。 4. 如果Bean实现了BeanFactoryAware接口,则Spring调用Bean的setBeanFactory()方法传入当前工厂实例的引用。 5. 如果Bean实现了ApplicationContextAware接口,则Spring调用Bean的setApplicationContext()方法传入当前ApplicationContext实例的引用。 6. 如果BeanPostProcessor和Bean关联,则Spring将调用该接口的预初始化方法postProcessBeforeInitialization()对Bean进行加工操作。Spring的AOP操作就是通过它实现的。 7. 如果Bean实现了InitializingBean接口,则Spring调用Bean的afterPropertiesSet()方法。 8. 如果在配置文件中通过init-method属性指定了初始化方法,则调用该初始化方法。 9. 如果BeanPostProcessor和Bean关联,则Spring将调用该接口的预初始化方法postProcessAfterInitialization()。此时Bean已经可以被应用系统所使用。 10. 如果在<bean>中指定作用域scope="singleton",则将该Bean放入Spring IoC缓存中,同时触发对Bean的生命周期管理。若作用域为prototype,则由调用者管理该Bean的生命周期。 11. 如果Bean实现了DisposableBean接口,则Spring会在销毁时调用destroy()方法Spring中的Bean销毁。 12. 如果在配置文件中通过destroy-method指定了销毁方法,则Spring调用该方法销毁Bean。 在Spring中,可以使用XML配置文件来注册和管理Bean之间的依赖关系。通常使用<beans>元素作为根元素,并在其中添加<bean>元素定义Bean。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Spring中的Bean](https://blog.csdn.net/weixin_44949462/article/details/118897055)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [spring bean是什么](https://blog.csdn.net/Kariswei/article/details/117085068)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值