当前位置: 移动技术网 > IT编程>开发语言>Java > spring 中 bean的初使用+bean的继承+bean调用前与调用后的调用

spring 中 bean的初使用+bean的继承+bean调用前与调用后的调用

2020年03月14日  | 移动技术网IT编程  | 我要评论

安徽萧县车祸,网上电子 89168官方赌博公司,大堡小劳教

新建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();

}

}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网