Spring-Bean-配置bean-案例二:通过abstract属性创建一个模板bean
案例二:通过abstract属性创建一个模板bean
(1)编写一个类
public class Person { private String name; private Integer age; private String gender; public Person() { } public Person(String name, Integer age, String gender) { this.name = name; this.age = age; this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", gender='" + gender + '\'' + '}'; } }
(2)配置文件
<!-- abstract="true":这个bean的配置是一个抽象的,不能获取它的实例,只能被别人继承 --> <bean id="person" class="com.orz.spring.bean.Person" abstract="true"> <property name="name" value="李华"/> <property name="age" value="18"/> <property name="gender" value="男"/> </bean>
(3)测试
@Test public void test1() { ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml"); Person person = applicationContext.getBean("person", Person.class); System.out.println(person); }
(4)结果
org.springframework.beans.factory.BeanIsAbstractException: Error creating bean with name 'person': Bean definition is abstract