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

抛出异常

时间: 2024-08-26 07:46:50

<bean abstract="true"> 即该bean不能被实例化的相关文章

Spring初学之bean之间的关系和bean的作用域

一.bean之间的关系 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.o

峰Spring4学习(5)bean之间的关系和bean的作用范围

一.bean之间的关系: 1)继承: People.java实体类: package com.cy.entity; public class People { private int id; private String name; private int age; private String className; public int getId() { return id; } public void setId(int id) { this.id = id; } public Strin

解决Spring中singleton的Bean依赖于prototype的Bean的问题

在spring bean的配置的时候,可能会出现一个singleton的bean依赖一个prototype的bean.因为singleton的bean只有一次初始化的机会,所以他们的依赖关系页只有在初始化的时候被设置,在这个时候同样会初始化一个prototype的bean,这样就会导致本来是prototype的bean成了singleton了,这种矛盾的解决方案有两个: 1.放弃使用依赖注入,在需要的时候主动向容器申请. 2.利用方法注入 第一种方法显然是不合理的--- 方法注入通常使用look

EJB通过ANT提高EJB应用的开发效率、开发具有本地接口的无状态bean、开发有状态bean

把jboss集成进eclipse 关闭Jboss控制台按Ctrl+c,在MyEclipse→Servers→Jboss里面可以配置JBoss. 通过ANT提高EJB应用的开发效率 在HelloWorld 加入ANT,右击项目新建xml:build.xml,右击打开方式选择Ant .. 对应用进行编译.打包.发布.解发布 <?xml version="1.0" encoding="UTF-8"?> <!--basedirz项目所在路径,.代表在bui

【Bean】 这才是bean,一直没仔细看

EJB是Enterprise Java Bean的缩写,一个Bean扮演着应用程序素材的角色.它包含有一个functional interface,一个life-cycle interface,以及一个实现它所支援的商业方法的类别. JavaBean是描述Java的软件组件模型,有点类似于Microsoft的COM组件概念.在Java模型中,通过JavaBean可以无限扩充Java程序的功能,通过JavaBean的组合可以快速的生成新的应用程序.对于程序员来说,最好的一点就是JavaBean可以

Spring 配置中 bean 的 class 使用工厂 Bean 的含义解析

太阳火神的美丽人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 转载请保留此句:太阳火神的美丽人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS.Android.Html5.Arduino.pcDuino,否则,出自本博客的文章拒绝转载或再转载,谢谢合作. <bean id="studentMapper" class="org.mybatis.spring.Mapp

当singleton Bean依赖propotype Bean,可以使用在配置Bean添加look-method来解决

在Spring里面,当一个singleton bean依赖一个prototype bean,因为singleton bean是单例的,因此prototype bean在singleton bean里面也会变成单例. 这个怎么解决呢???可以使用Spring提供的lookup-method来注入. 举例说明:先列出相关类:代码中的说明足够说明问题.user类:prototype bean package com.cn.pojo; import java.io.Serializable; publi

有状态bean和无状态的bean

有状态会话bean :每个用户有自己特有的一个实例,在用户的生存期内,bean保持了用户的信息,即"有状态":一旦用户灭亡(调用结束或实例结束),bean的生命期也告结束.即每个用户最初都会得到一个初始的bean. 无状态会话bean :bean一旦实例化就被加进会话池中,各个用户都可以共用.即使用户已经消亡,bean 的生命期也不一定结束,它可能依然存在于会话池中,供其他用户调用.由于没有特定的用户,那么也就不能保持某一用户的状态,所以叫无状态bean.但无状态会话bean 并非没有

Error creating bean with name &#39;shiroFilter&#39;: Requested bean is currently in creation: Is there an unresolvable circular reference?

ssm+shiro+maven整合时,Tomcat运行报错,原因是忘记在service层和dao层的实现类上面添加注解了,加上就行了.记录一下 Related cause: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'shiroFilter': Requested bean is currently in creation: Is there