spring 中 bean的初使用+bean的继承+bean调用前与调用后的调用
Posted broadencoder
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring 中 bean的初使用+bean的继承+bean调用前与调用后的调用相关的知识,希望对你有一定的参考价值。
新建Product 的bean:
package com.my;
public class Product {
private String aa;
private String cc;
private String ee;
public void setAa(String aa){
this.aa=aa;
}
public void setCc(String cc){
this.cc=cc;
}
public void setEe(String ee){
this.ee=ee;
}
public String getAa(){
return aa;
}
public String getCc(){
return cc;
}
public String getEe(){
return ee;
}
public void init(){
System.out.println("initialize");
}
public void destroy(){
System.out.println("destroy");
}
}
在src新建Bean.xml:
<bean id="product" class="com.my.Product" init-method="init" destroy-method="destroy"> //init-method: bean调用前调用 destroy-method:bean调用后调用
<property name="aa" value="bb"> </property>
<property name="cc" value="dd"></property>
<property name="ee" value="ff"></property>
</bean>
下一步:
新建Mymy:
package com.my;
import org.springframework.context.ApplicationContext;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import com.my.ExtendProduct;
public class Mymy {
public static void main(String[] args){ //main方法可以作为调试的方法
AbstractApplicationContext context=new ClassPathXmlApplicationContext( //调用bean.xml
"Bean.xml"
);
Product product= (Product) context.getBean("product");
System.out.println (product.getAa()); //调用Product bean中的方法
context.registerShutdownHook(); //调用bean调用前的方法和调用后的方法
}
}
关于bean中的继承:
Bean.xml:
<bean id="product" class="com.my.Product" init-method="init" destroy-method="destroy">
<property name="aa" value="bb"> </property>
<property name="cc" value="dd"></property>
<property name="ee" value="ff"></property>
</bean>
<bean id="extendproduct" class="com.my.ExtendProduct" init-method="init" destroy-method="destroy" parent="product"> //继承上一个bean
<property name="aa" value="ii"> </property>
<property name="cc" value="jj"></property>
<property name="ee" value="kk"></property>
<property name="gg" value="hh"></property>
</bean>
都要有对应的类:
package com.my;
public class ExtendProduct { //即为继承的类
private String aa;
private String cc;
private String ee;
private String gg;
public void setGg(String gg){
this.gg=gg;
}
public String getGg(){
return gg;
}
public void setAa(String aa){
this.aa=aa;
}
public void setCc(String cc){
this.cc=cc;
}
public void setEe(String ee){
this.ee=ee;
}
public String getAa(){
return aa;
}
public String getCc(){
return cc;
}
public String getEe(){
return ee;
}
public void init(){
System.out.println("initialize");
}
public void destroy(){
System.out.println("destroy");
}
}
调用:
package com.my;
import org.springframework.context.ApplicationContext;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import com.my.ExtendProduct;
public class Mymy {
public static void main(String[] args){
AbstractApplicationContext context=new ClassPathXmlApplicationContext(
"Bean.xml"
);
Product product= (Product) context.getBean("product");
System.out.println (product.getAa());
ExtendProduct extendProduct=(ExtendProduct)context.getBean("extendproduct"); //继承的bean的调用
System.out.println(extendProduct.getAa()+extendProduct.getCc()+extendProduct.getEe()+extendProduct.getGg());
// context.registerShutdownHook();
}
}
以上是关于spring 中 bean的初使用+bean的继承+bean调用前与调用后的调用的主要内容,如果未能解决你的问题,请参考以下文章