温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring抽象Bean和子Bean的定义与用法

发布时间:2021-08-12 12:50:43 来源:亿速云 阅读:130 作者:chen 栏目:编程语言

这篇文章主要介绍“Spring抽象Bean和子Bean的定义与用法”,在日常操作中,相信很多人在Spring抽象Bean和子Bean的定义与用法问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Spring抽象Bean和子Bean的定义与用法”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一 配置

<?xml version="1.0" encoding="GBK"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xmlns="http://www.springframework.org/schema/beans"   xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">   <!-- 定义Axe实例 -->   <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/>   <!-- 指定abstract="true"定义抽象Bean -->   <bean id="personTemplate" abstract="true">      <property name="name" value="crazyit"/>      <property name="axe" ref="steelAxe"/>   </bean>   <!-- 通过指定parent属性指定下面Bean配置可从父Bean继承得到配置信息 -->   <bean id="chinese" class="org.crazyit.app.service.impl.Chinese"      parent="personTemplate"/>   <bean id="american" class="org.crazyit.app.service.impl.American"      parent="personTemplate"/></beans>

二 接口

Axe

package org.crazyit.app.service;public interface Axe{   public String chop();}

Person

package org.crazyit.app.service;public interface Person{   public void useAxe();}

三 实现类

1 American

package org.crazyit.app.service.impl;import org.crazyit.app.service.*;public class American implements Person{   private Axe axe;   private String name;   public void setAxe(Axe axe)   {      System.out.println("Spring执行依赖关系注入...");      this.axe = axe;   }   public void setName(String name)   {      this.name = name;   }   public void useAxe()   {      System.out.println("我是美国人:名字为:" + name        + "。正在用斧头" + axe.chop());   }}

2 Chinese

package org.crazyit.app.service.impl;import org.crazyit.app.service.*;public class Chinese implements Person{   private Axe axe;   private String name;   public void setAxe(Axe axe)   {      System.out.println("Spring执行依赖关系注入...");      this.axe = axe;   }   public void setName(String name)   {      this.name = name;   }   public void useAxe()   {      System.out.println("我是中国人:名字为:" + name        + "。正在用斧头" + axe.chop());   }}

3 SteelAxe

package org.crazyit.app.service.impl;import org.crazyit.app.service.*;public class SteelAxe implements Axe{   public String chop()   {      return "钢斧砍柴真快";   }}

4 StoneAxe

package org.crazyit.app.service.impl;import org.crazyit.app.service.*;public class StoneAxe implements Axe{   public String chop()   {      return "石斧砍柴好慢";   }}

四 测试类

package lee;import org.springframework.context.ApplicationContext;import org.springframework.context.support.*;import org.crazyit.app.service.*;public class BeanTest{  public static void main(String[] args)  {    ApplicationContext ctx = new      ClassPathXmlApplicationContext("beans.xml");  }}

五 测试结果

Spring执行依赖关系注入...Spring执行依赖关系注入...

到此,关于“Spring抽象Bean和子Bean的定义与用法”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI