首页 > 代码库 > spring中abstract bean的用法
spring中abstract bean的用法
什么是abstract bean?简单来说,就是在java中的继承时候,所要用到的父类。
案例文件结构:
其中Person类为父类,Student类为子类,其具体类为:
package com.test.mySpring; public class Person { public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } protected String name; protected int age; }
然后在Student类中增加了一个属性学号:
package com.test.mySpring; public class Student extends Person{ String no; public String getNo() { return no; } public void setNo(String no) { this.no = no; } }
测试类:
package com.test.mySpring; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import com.test.SpringGetSet.School; public class TestCase { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub BeanFactory factory=new XmlBeanFactory(new ClassPathResource("com/test/mySpring/beans.xml")); //Person p=(Person)factory.getBean("person"); //System.out.println(p.getName()+" "+p.getAge()); Student s=(Student)factory.getBean("student"); System.out.println(s.getName()+" "+s.getAge()+" "+s.getNo()); } }
beans.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?> <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"> <bean id="person" class="com.test.mySpring.Person" abstract="true"> <property name="name" value=http://www.mamicode.com/"李白" />>输出结果:
分析:我们可以看到,在beans.xml中,如果没有abstract="true",那么是会生成父类的,但是如果有了,就不会实例化父类。
spring中abstract bean的用法
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。