`

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

 
阅读更多

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

首先建一个Bean类,Car.java

 

  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的位置。

 

 

  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

 

  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!

28
12
分享到:
评论

相关推荐

    Spring.3.x企业应用开发实战(完整版).part2

     《Spring3.x企业应用开发实战》是在《精通Spring2.x——企业应用开发详解》的基础上,经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练...

    Spring3.x企业应用开发实战(完整版) part1

     《Spring3.x企业应用开发实战》是在《精通Spring2.x——企业应用开发详解》的基础上,经过历时一年的重大调整改版而成的,本书延续了上一版本追求深度,注重原理,不停留在技术表面的写作风格,力求使读者在熟练...

    tiny-spring-step-4-config-beanfactory-with-xml.zip

    对于spring的ioc的简单通过xml配置bean完成初始化工作的实现。

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    Spring中ApplicationContext和beanfactory区别.rar

    Spring中ApplicationContext和beanfactory区别.rar

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    Spring学习

    Spring学习,简单理解,帮助大家...核心容器:BeanFactory---Bean 2.应用上下文模块:配置文件 3.AOP模块: 4.DAO模块:简化了JDBC重复代码 5.ORM模块:Hibernate、iBatis 6.Web模块:Struts2…… 7.SpringMVC:

    Spring API

    7.10.3. 原型目标源 7.10.4. ThreadLocal目标源 7.11. 定义新的Advice类型 7.12. 更多资源 8. 测试 8.1. 简介 8.2. 单元测试 8.2.1. Mock对象 8.2.2. 单元测试支持类 8.3. 集成测试 8.3.1. 概览 8.3.2. ...

    Spring中文帮助文档

    7.10.3. 原型目标源 7.10.4. ThreadLocal目标源 7.11. 定义新的Advice类型 7.12. 更多资源 8. 测试 8.1. 简介 8.2. 单元测试 8.2.1. Mock对象 8.2.2. 单元测试支持类 8.3. 集成测试 8.3.1. 概览 8.3.2. ...

    spring学习笔记

    Spring 的开发包:..............................................................................................................6 1.3.2 创建 web 工程引入相应 jar 包:........................................

    Spring框架.ppt

    提供Spring框架的基本功能,其主要组件是BeanFactory,是工厂模式的实现。 Spring 上下文。向Spring 框架提供上下文信息,包括企业服务,如 JNDI、EJB、电子邮件、国际化、校验和调度等。 Spring AOP。通过配置管理...

    spring-03-ydm .rar

    使用Spring的IOC完成保存客户的操作 。 案例需求 相关知识点 案例代码. Spring 整合 WEB 项目 引入 spring-web.jar 包 Spring 中的工厂(容器) BeanFactory(过时) Spring 配置文件中提示的配置

    简单模拟Spring的beanFactory

    NULL 博文链接:https://lgd-java2eye.iteye.com/blog/756599

    Spring入门.docx

    (3)BeanFactory(bean工厂)与ApplicationContext(容器)对比: 所有在xml里配置的Bean标签最后都会转换为BeanDefinition对象存放在beanFactory里的beanDefinitionMap中。实例化完成的Bean对象存放在...

    Spring高级之注解驱动开发视频教程

    同时,在3.x版本之后,它开始之初Rest风格的请求URL,为开发者提供了开发基于Restful访问规则的项目提供了帮助。 SpringData是一组技术合集。里面包含了JDBC,Data JPA,Data Redis,Data Mongodb,Data Rabbit,...

    基于java的企业级应用开发:Spring AOP简介.ppt

    它主要由Beans模块、Core模块、Context模块、Context-support模块和SpEL(Spring Expression Language,Spring表达式语言)模块组成,具体介绍如下: Beans模块:提供了BeanFactory,是工厂模式的经典实现,Spring将...

    spring-04-源代码.rar

    使用Spring的IOC完成保存客户的操作: 。 案例需求 相关知识点 案例代码. Spring 整合 WEB 项目 引入 spring-web.jar 包: Spring 中的工厂(容器): BeanFactory(过时): Spring 配置文件中提示的配置

    spring-02-源代码.rar

    使用Spring的IOC完成保存客户的操作: 。 案例需求 相关知识点 案例代码. Spring 整合 WEB 项目 引入 spring-web.jar 包: Spring 中的工厂(容器): BeanFactory(过时): Spring 配置文件中提示的配置

    spring-01-源代码.rar

    使用Spring的IOC完成保存客户的操作: 。 案例需求 相关知识点 案例代码. Spring 整合 WEB 项目 引入 spring-web.jar 包: Spring 中的工厂(容器): BeanFactory(过时): Spring 配置文件中提示的配置

Global site tag (gtag.js) - Google Analytics