将这些架包放入在工程目录下建立的lib文件夹里,并解压

commons-logging-1.1.1

spring-aop-4.0.0.RELEASE

spring-beans-4.0.0.RELEASE

spring-context-4.0.0.RELEASE

spring-core-4.0.0.RELEASE

spring-expression-4.0.0.RELEASE

1.SpEL,实现

Person类,其属性如下,其get,set,tostrong方法就不写了

private String name;
private Car car;
private String city;//city属性是引用了Address中city的属性
private String info;//根据car的price属性来确定info,price大于30万,不大于30万

car类,其属性如下,set,get,tostring方法就不写了

private String brand;
private double price;
private double tyrePerimeter;//轮胎的周长

address类,其属性如下,set,get,tostring方法就不下了

private String city;
private String street;

建立spring bean configuration file文件:beans.xml

<!-- spEL的使用#{…} -->

    <bean id="address" class="com.atguigu.spring.beans.Address">
<property name="city" value="#{'BeiJing'}"></property>
<property name="street" value="jianglingjialu"></property>
</bean> <bean id="car" class="com.atguigu.spring.beans.Car">
<property name="brand" value="auti"></property>
<property name="price" value="345566"></property> <property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 30}"></property>
</bean> <bean id="person" class="com.atguigu.spring.beans.Person">
<property name="name" value="#{'panpan'}"></property>
<property name="car" value="#{car}"></property>
<property name="city" value="#{address.city}"></property> <property name="info" value="#{car.price>300000 ? '金领' : '蓝领' }"></property>
</bean>

在src目录下的Main类 测试方法;

ApplicationContext app=new ClassPathXmlApplicationContext("beans.xml");

Address address=(Address) app.getBean("address");
System.out.println(address); Car car=(Car) app.getBean("car");
System.out.println(car); Person person=(Person) app.getBean("person");
System.out.println(person);

----------------------------------------------------------------------------------

2.静态工厂方式,spring的bean的配置方法;

  建立StaticCarFactory类:

package com.atguigu.spring.beans;

import java.util.HashMap;
import java.util.Map; public class StaticCarFactory {
//静态工厂方式,spring的bean的配置方法
private static Map<String, Car> cars=new HashMap<String, Car>();
static{
cars.put("KKK", new Car("changan",423423,432.43));
cars.put("PPP", new Car("fute",42323,32.43));
}
//在xml文件中,可以设置和获取getCar方法
public static Car getCar(String name){
return cars.get(name);
}
}

在beans.xml 文件中配置bean;

<!-- 静态工厂方式 -->
<bean id="car1" class="com.atguigu.spring.beans.StaticCarFactory"
factory-method="getCar">
<constructor-arg value="PPP"></constructor-arg>
</bean>

在 Main类 中测试;

ApplicationContext app=new ClassPathXmlApplicationContext("beans.xml");
Car car1=(Car) app.getBean("car1");
System.out.println(car1);

-------------------------------------------------------------------------------------

3.实例工厂的方法,实现bean的配置;

  建立类:InstanceFactory

package com.atguigu.spring.beans;

import java.util.HashMap;
import java.util.Map; public class InstanceFactory {
private Map<String, Car> cars=null; //实例工厂的方法
public InstanceFactory() {
cars=new HashMap<String, Car>(); cars.put("KKK", new Car("luhu", 434233, 43.2));
cars.put("QQQ", new Car("fute", 656546, 45.4));
} public Car getCar(String brand){
return cars.get(brand);
}
}

在beans.xml 文件中配置bean;

<!-- 实例工厂的方式 -->
<bean id="factory" class="com.atguigu.spring.beans.InstanceFactory"></bean> <bean id="car2" factory-bean="factory" factory-method="getCar">
<constructor-arg value="QQQ"></constructor-arg>
</bean>

在类Main中,测试:

ApplicationContext app=new ClassPathXmlApplicationContext("beans.xml");
Car car2=(Car) app.getBean("car2");
System.out.println(car2);

------------------------------------------------------------------------------------------

4.FactoryBean的配置方法:继承接口:FactoryBean,泛型为Car;

package com.atguigu.spring.beans;

import org.springframework.beans.factory.FactoryBean;

//FactoryBean的配置方法
public class CarFactoryBean implements FactoryBean<Car>{ private String brand; public void setBrand(String brand) {
this.brand = brand;
} @Override
//返回bean的对象,即car
public Car getObject() throws Exception {
return new Car(brand, 200000, 1.2);
} @Override
//返回bean的类型
public Class<?> getObjectType() {
return Car.class;
} @Override
//是不是但实力的
public boolean isSingleton() {
return true;
} }

在beans.xml 文件中配置bean;

<!-- FactoryBean的配置方法 -->
<bean id="car3" class="com.atguigu.spring.beans.CarFactoryBean">
<property name="brand" value="panpan"></property>
</bean>

在类Main中,测试:

ApplicationContext app=new ClassPathXmlApplicationContext("beans.xml");
Car car3=(Car) app.getBean("car3");
System.out.println(car3);

Spring框架bean的配置(2):SpEL:引用 Bean、属性和方法。。。的更多相关文章

  1. Spring5源码解析-Spring框架中的单例和原型bean

    Spring5源码解析-Spring框架中的单例和原型bean 最近一直有问我单例和原型bean的一些原理性问题,这里就开一篇来说说的 通过Spring中的依赖注入极大方便了我们的开发.在xml通过& ...

  2. Bean XML 配置(2)- Bean作用域与生命周期回调方法配置

    系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of Contro ...

  3. Spring - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配1.1 使用:只需在<bean>中使用autowire元素<bean id="student" class="com.kej ...

  4. 《Java Spring框架》SpringXML配置详解

    Spring框架作为Bean的管理容器,其最经典最基础的Bean配置方式就是纯XML配置,这样做使得结构清晰明了,适合大型项目使用.Spring的XML配置虽然很繁琐,而且存在简洁的注解方式,但读懂X ...

  5. Spring4学习笔记 - 配置Bean - 自动装配 关系 作用域 引用外部属性文件

    1 Autowire自动装配 1.1 使用:只需在<bean>中使用autowire元素 <bean id="student" class="com.k ...

  6. 基于Spring框架的Shiro配置

    一.在web.xml中添加shiro过滤器  <!-- Shiro filter--> <filter> <filter-name>shiroFilter</ ...

  7. spring框架 事务 注解配置方式

    user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.Ora ...

  8. 基于Spring框架的Shiro配置(转发:http://kdboy.iteye.com/blog/1103794)

    一.在web.xml中添加shiro过滤器 <!-- Shiro filter--> <filter> <filter-name>shiroFilter</f ...

  9. spring框架 事务 xml配置方式

    user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.Ora ...

  10. Spring中使用@Value读取porperties文件中的属性值方法总结及注意事项

    本文为博主原创,转载请注明出处. 此前曾总结过使用工具类读取properties文件中的属性值,有兴趣的可以看一下. 如何快速获取properties中的配置属性值:https://www.cnblo ...

随机推荐

  1. Java基础之一组有用的类——使用Scanner对象(TryScanner)

    控制台程序. java.util.Scanner类定义的对象使用正则表达式来扫描来自各种源的字符输入,并把输入显示为各种基本类型的一系列标记或者显示为字符串. 默认情况下,Scanner对象读取标记时 ...

  2. js方法和prototype

    JS中的方法可以分为三类 1.对象方法 2.类方法 3.原型方法 例: function People(name) { this.name=name; //对象方法 this.Introduce=fu ...

  3. 20145207 《Java程序设计》第5周学习总结

    前言:先聊两句,上午电路实习,刚开始没多久就让电烙铁烫了,倒霉催的~晚上来这里接着弄代码,透心凉心飞扬~ 教材学习内容总结 一.异常处理 1.语法与继承结构 使用try.catch: Java中所有错 ...

  4. [原创]java WEB学习笔记90:Hibernate学习之路-- -HQL检索方式,分页查询,命名查询语句,投影查询,报表查询

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. Python学习总结11:获取当前运行类名和函数名

    一. 使用内置方法和修饰器方法获取类名.函数名 1. 外部获取 从外部的情况好获取,可以使用指向函数的对象,然后用__name__属性. def a(): pass a.__name__ 或者 get ...

  6. MyEclipse下如何安装和使用ibatis插件(网上的资料对于myeclipse8.5根本就是没有用的,所以我还是自己选择了装了一个eclipse,然后将插件装在了eclipse中)

    (1)myeclipse→help→Myeclipse configuration center:点击sofeware选项卡,在Browes Software 下有一个输入框,点击add site按钮 ...

  7. opengl& 颜色

    颜色 简介 颜色的显示模式分为两种: RGBA显示模式 颜色索引显示模式(使用颜色映射表,映射表提供了索引,可以混合基本的红,绿,蓝色值). RGBA模式可以选择的颜色数量多于颜色索引模式.一般而言, ...

  8. android 自定义view详解

    1.自定义View前首先要了解一下View的方法,虽然有些不一定要实现. 分类 方法 描述 创建 Constructors View中有两种类型的构造方法,一种是在代码中构建View,另一种是填充布局 ...

  9. android 项目学习随笔十二(ListView加脚布局)

    1.ListView加脚布局 头布局initHeaderView,在onTouchEvent事件中进行显示隐藏头布局切换 脚布局initFooterView,实现接口OnScrollListener, ...

  10. DELPHI出现无法加载dclite50.bpl的解决办法(转)

    现象: Borland Integrated Translation Environment 加载出错 解决办法: 我的电脑--->(鼠标右键)属性--->高级--->(性能)设置- ...