Spring 3.x企业应用开发实战(6)----BeanFactory

本文介绍了一个使用Spring框架的BeanFactory创建并初始化Bean的实例。通过定义一个Car类,并在beans.xml配置文件中指定其属性,最后通过BeanFactoryTest类演示了如何加载配置并获取实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

BeanFactory是一个类工厂,是类的通用工厂。不多说,直接进入BeanFactory的使用:

首先建一个Bean类,Car.java

 

[java]  view plain copy print ?
 
  1. package com.techman.reflect;  
  2.   
  3. public class Car   
  4. {  
  5.     private String brand;  
  6.     private String color;  
  7.     private int maxSpeed;  
  8.     public String getBrand() {  
  9.         return brand;  
  10.     }  
  11.     public void setBrand(String brand) {  
  12.         this.brand = brand;  
  13.     }  
  14.     public String getColor() {  
  15.         return color;  
  16.     }  
  17.     public void setColor(String color) {  
  18.         this.color = color;  
  19.     }  
  20.     public int getMaxSpeed() {  
  21.         return maxSpeed;  
  22.     }  
  23.     public void setMaxSpeed(int maxSpeed) {  
  24.         this.maxSpeed = maxSpeed;  
  25.     }  
  26.       
  27.     public Car(){  
  28.           
  29.     }  
  30.       
  31.     public Car(String brand,String color,int maxSpeed){  
  32.         this.brand=brand;  
  33.         this.color=color;  
  34.         this.maxSpeed=maxSpeed;       
  35.     }  
  36.       
  37.     public void introduce()  
  38.     {  
  39.         System.out.println("Brand is "+brand+" Color is "+color+" Max Speed is "+maxSpeed);  
  40.     }  
  41. }  

配置一个beans.xml文件来指明bean的位置。

 

 

[html]  view plain copy print ?
 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans     
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xmlns:context="http://www.springframework.org/schema/context"  
  7.       
  8.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  9.                         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
  10.                         http://www.springframework.org/schema/context   
  11.                         http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  12.     <bean id="car1" class="com.techman.reflect.Car" p:brand="宝马X7" p:color="白色" p:maxSpeed="350"/>                          
  13. </beans>  


然后在类中实现Bean的初始化和调用。

 

BeanFactoryTest.java

 

[java]  view plain copy print ?
 
  1. package com.techman.resource;  
  2.   
  3. import org.springframework.beans.factory.BeanFactory;  
  4. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  5. import org.springframework.core.io.Resource;  
  6. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;  
  7. import org.springframework.core.io.support.ResourcePatternResolver;  
  8.   
  9. import com.techman.reflect.Car;  
  10.   
  11. public class BeanFactoryTest   
  12. {  
  13.     public static void main(String []args)throws Throwable  
  14.     {  
  15.         ResourcePatternResolver resolver=new PathMatchingResourcePatternResolver();  
  16.         Resource res=resolver.getResource("classpath:com/techman/resource/beans.xml");  
  17.         BeanFactory bf=new XmlBeanFactory(res);  
  18.         System.out.println("Init BeanFactory.");  
  19.           
  20.         Car car=bf.getBean("car1",Car.class);  
  21.           
  22.         car.introduce();  
  23.           
  24.         System.out.println("Car Bean is ready for use!");  
  25.     }  
  26. }  


结果为:

 

Init BeanFactory.
Brand is 宝马X7 Color is 白色 Max Speed is 350
Car Bean is ready for use!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值