• <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)

    抛出异常

  • 相关阅读:
    Notes of Daily Scrum Meeting(12.22)
    一个合格的程序员应该读过哪些书
    snprintf vs sprintf
    Centos 关闭图形界面
    oracle selinux 问题
    struct 和typedef struct的区别
    c语言字符串函数
    504. Base 7
    汉诺塔python实现
    VIM字符编码基础知识
  • 原文地址:https://www.cnblogs.com/beibidewomen/p/7391177.html
Copyright © 2020-2023  润新知