Spring框架bean的配置(2):SpEL:引用 Bean、属性和方法。。。

时间:2021-04-27 15:04:33

将这些架包放入在工程目录下建立的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);