<bean abstract="true"> 即该bean不能被实例化

第一步:新建项目  SecondSpring

文件目录结构如下:

 

 第二步:导入spring相对应的jar包

过程略...

 

第三步: 新建类

TestAbstract.java

package com.xuzhiwen.spring8;

public class TestAbstract {
    public String name;
    
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "TestAbstract [name=" + name + "]";
    }
}

 

第四步: 新建spring配置文件

common.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
    <import resource="xmlfolder/app1.xml" />
    <import resource="xmlfolder/innerbean.xml" />
    <import resource="xmlfolder/singleton.xml" />
    <import resource="xmlfolder/annotation.xml" />
    <import resource="xmlfolder/gather.xml" />
    <import resource="xmlfolder/date.xml" />
    <import resource="xmlfolder/db.xml" />
    <import resource="xmlfolder/parent.xml" />
    
    <import resource="xmlFile/abstract.xml" />
    
</beans>    

abstract.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
    <bean id="abs" class="com.xuzhiwen.spring8.TestAbstract">
        <property name="name" value="helloworld" />
    </bean>    
    
</beans>    

 

第五步:新建测试类

Test.java

package com.xuzhiwen.spring8;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("common.xml");
        TestAbstract test = (TestAbstract) app.getBean("abs");
        System.out.println(test);
    }
}

 

第六步:运行结果如下

 

 第七步:给abstract.xml 配置文件<bean>中添加 abstract属性

<bean id="abs" class="com.xuzhiwen.spring8.TestAbstract" abstract="true">
        <property name="name" value="helloworld" />
    </bean>    

 

 第八步: 再次运行测试代码,结果如下

Exception in thread "main" org.springframework.beans.factory.BeanIsAbstractException: Error creating bean with name 'abs': Bean definition is abstract
    at org.springframework.beans.factory.support.AbstractBeanFactory.checkMergedBeanDefinition(AbstractBeanFactory.java:1293)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:285)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076)
    at com.xuzhiwen.spring8.Test.main(Test.java:9)

抛出异常

 

转载于:https://www.cnblogs.com/beibidewomen/p/7391177.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值