zoukankan      html  css  js  c++  java
  • spring bean 继承

    问题描述---为什么Bean配置需要能够继承?

    在Spring Ico容器里配置Bean时,可能存在这样一种情况:多个Bean的配置有一部分是相同的,如果在每个Bean里都进行配置,就会显得很麻烦。

    相同的配置往往有两种情况:1.多个Bean需要注入相同的Bean;2.多个<bean>元素的属性相同。

    解决方案

    将多个Bean相同的部分抽象为一个Bean,然后让这多个Bean继承它。

    实现案例

    [java] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. class Dao{  
    2.     public void daoM(){  
    3.         System.out.println("doaM");  
    4.     }  
    5. }  
    6.   
    7. class ServiceA {  
    8.     private Dao dao;  
    9.       
    10.     public void setDao(Dao dao) {  
    11.         this.dao = dao;  
    12.     }  
    13.   
    14.     public void serviceA_M(){  
    15.         dao.daoM();  
    16.     }  
    17. }  
    18.   
    19. class ServiceB {  
    20.     private Dao dao;  
    21.       
    22.     public void setDao(Dao dao) {  
    23.         this.dao = dao;  
    24.     }  
    25.   
    26.     public void serviceB_M(){  
    27.         dao.daoM();  
    28.     }  
    29. }  


    Bean配置

    [html] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <beans xmlns="http://www.springframework.org/schema/beans"  
    3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    4.        xsi:schemaLocation="http://www.springframework.org/schema/beans   
    5.            http://www.springframework.org/schema/beans/spring-beans.xsd">  
    6.              
    7.       <bean id="dao" class="com.zzj.test.Dao"></bean>  
    8.       <bean id="service" abstract="true">  
    9.         <property name="dao">  
    10.             <ref bean="dao"/>  
    11.         </property>  
    12.       </bean>  
    13.       <bean id="serviceA" class="com.zzj.test.ServiceA" parent="service"></bean>  
    14.       <bean id="serviceB" class="com.zzj.test.ServiceB" parent="service"></bean>  
    15. </beans>  

    注:抽象出来的bean并未指定class。

    测试

    [java] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. public static void main(String[] args) {  
    2.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");  
    3.         ServiceA serviceA = (ServiceA) context.getBean("serviceA");  
    4.         serviceA.serviceA_M();  
    5.         ServiceB serviceB = (ServiceB) context.getBean("serviceB");  
    6.         serviceB.serviceB_M();  
    7.   
    8.     }  

    总结

    父Bean可以作为配置模板,也可以作为Bean实例。不过,如果只想把父Bean作为不实例化的模板,那么必须把abstract属性设为true,这样spring将不会实例化这个Bean。

    注意:

    1.并不是所有在父<bean>元素里定义的属性都会被继承。例如,autowire和dependency-check属性就不能被继承。

    2.Bean配置的继承不是类的继承,它们之间没有任何关系。

    转自:http://blog.csdn.net/zhangzeyuaaa/article/details/22583681

  • 相关阅读:
    读书笔记——吴军《态度》
    JZYZOJ1237 教授的测试 dfs
    NOI1999 JZYZOJ1289 棋盘分割 dp 方差的数学结论
    [JZYZOJ 1288][洛谷 1005] NOIP2007 矩阵取数 dp 高精度
    POJ 3904 JZYZOJ 1202 Sky Code 莫比乌斯反演 组合数
    POJ2157 Check the difficulty of problems 概率DP
    HDU3853 LOOPS 期望DP 简单
    Codeforces 148D. Bag of mice 概率dp
    POJ3071 Football 概率DP 简单
    HDU4405 Aeroplane chess 飞行棋 期望dp 简单
  • 原文地址:https://www.cnblogs.com/smile0120/p/5190322.html
Copyright © 2011-2022 走看看