不使用注解

来源:互联网 发布:阿波罗20号 知乎 编辑:程序博客网 时间:2024/05/16 15:35
Boss类代码:
package xiao.lei.entity;

public class Boss {
    privateOffice office;
    private Carcar;
   
    publicOffice getOffice() {
       returnoffice;
    }

    public voidsetOffice(Office office) {
       this.office= office;
    }
   
    public CargetCar() {
       returncar;
    }

    public voidsetCar(Car car) {
       this.car =car;
    }

   @Override
    publicString toString() {
      return  "car:"+car+"/n"+"office:" + office;
    }

}
Car类代码:
package xiao.lei.entity;

public class Car {
    privateString brand;
    privateString  price;
   
    publicString getBrand() {
       returnbrand;
    }

    public voidsetBrand(String brand) {
       this.brand =brand;
    }

    publicString getPrice() {
       returnprice;
    }

    public voidsetPrice(String price) {
       this.price =price;
    }
   
}
Office代码:
package xiao.lei.entity;

public class Office {
    privateString officeNo;

    publicString getOfficeNo() {
       returnofficeNo;
    }

    public voidsetOfficeNo(String officeNo) {
      this.officeNo = officeNo;
    }
   
}
beans.xml代码:

<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="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="boss"class="xiao.lei.entity.Boss">
   <property name="car" ref="car"/>
   <property name="office" ref="office"/>
</bean>
<bean id="office"class="xiao.lei.entity.Office">
   <property name="officeNo"value="002"></property>
</bean>

<bean id="car" class="xiao.lei.entity.Car"scope="singleton">
   <property name="brand" value="红旗 CA72"/>
    <property name="price"value="20000" />
</bean>
</beans>

测试代码:
package xiao.lei.test;

import org.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;

import xiao.lei.entity.Boss;

public class AnnoloCTest {

    publicstatic void main(String[] args) {
       // TODOAuto-generated method stub
       String[]locations={"xiao/lei/entity/beans.xml"};
      ApplicationContext ctx=newClassPathXmlApplicationContext(locations);
       Bossboss=(Boss)ctx.getBean("boss");
      System.out.println(boss);
    }

}

0 0
原创粉丝点击