一、Spring中的SpEL(表达式语言)
package spel; public class Car { private String name; private double price; private double tp;//轮胎周长 public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public double getTp() { return tp; } public void setTp(double tp) { this.tp = tp; } @Override public String toString() { return "Car [name=" + name + ", price=" + price + ", tp=" + tp + "]"; } }
package spel; public class Address { private String city; private String home; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getHome() { return home; } public void setHome(String home) { this.home = home; } @Override public String toString() { return "Address [city=" + city + ", home=" + home + "]"; } }
package spel; public class Person { private String name; private Car cars; private String city; private String info; public String getName() { return name; } public void setName(String name) { this.name = name; } public Car getCars() { return cars; } public void setCars(Car cars) { this.cars = cars; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } @Override public String toString() { return "Person [name=" + name + ", cars=" + cars + ", city=" + city + ", info=" + info + "]"; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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.xsd"> <!-- 使用spring赋值 --> <bean id="address" class="spel.Address"> <property name="city" value="#{'beijing'}"></property> <property name="home" value="#{'mmm'}"></property> </bean> <bean id="car" class="spel.Car"> <property name="name" value="audi"></property> <property name="price" value="122"></property> <property name="tp" value="#{T(Math).PI*80}"></property> </bean> <bean id="person" class="spel.Person"> <!-- 使用spel引用bean --> <property name="name" value="小白"></property> <property name="cars" value="#{car}"></property> <property name="city" value="#{address.city}"></property> <property name="info" value="#{car.price>30 ? '金' :'白'}"></property> </bean> </beans>
package spel; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext c=new ClassPathXmlApplicationContext("spel.xml"); Address ad=(Address) c.getBean("address"); System.out.println(ad); Car car=(Car) c.getBean("car"); System.out.println(car); Person pe=(Person) c.getBean("person"); System.out.println(pe); } }
二、通过工厂方法配置bean
package factory; public class Car { private String name; private int price; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public String toString() { return "Car [name=" + name + ", price=" + price + "]"; } public Car(String name, int price) { super(); this.name = name; this.price = price; } }
package factory; import java.util.HashMap; import java.util.Map; //实例工厂的方法 public class Factory { private static Map<String, Car> cars = null; public Factory() { cars = new HashMap<String, Car>(); cars.put("baoma", new Car("baoma", 300)); cars.put("xxx", new Car("xxx", 200)); } public Car getCar(String name) { return cars.get(name); } }
package factory; import java.util.HashMap; import java.util.Map; public class StaticFactory { private static Map<String,Car>cars=new HashMap<String,Car>(); static { cars.put("baoma", new Car("baoma",300)); cars.put("xxx", new Car("xxx",200)); } public static Car getCar(String name){ return cars.get(name); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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.xsd"> <!-- 通过静态工厂方法配置bean --> <bean id="car" class="factory.StaticFactory" factory-method="getCar"> <constructor-arg value="baoma"></constructor-arg> </bean> <!-- 配置工厂的实例 --> <bean id="car1" class="factory.Factory"></bean> <!-- 通过实例工厂配置bean --> <bean id="car2" factory-bean="car1" factory-method="getCar"> <constructor-arg value="xxx"></constructor-arg> </bean> </beans>
package factory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext c=new ClassPathXmlApplicationContext("factory.xml"); Car car=(Car) c.getBean("car"); System.out.println(car); Car car2=(Car) c.getBean("car2"); System.out.println(car2); } }
三、通过factorybean配置bean
package factorybean; public class Car { private String name; private int price; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public String toString() { return "Car [name=" + name + ", price=" + price + "]"; } public Car(String name, int price) { super(); this.name = name; this.price = price; } }
package factorybean; import org.springframework.beans.factory.FactoryBean; import factorybean.Car; public class CarFactoryBean implements FactoryBean<Car> { private String name; public void setName(String name) { this.name=name; } //返回bean对象 @Override public Car getObject() throws Exception { // TODO 自动生成的方法存根 return new Car(name,55); } //返回bean类型 @Override public Class<?> getObjectType() { // TODO 自动生成的方法存根 return Car.class; } @Override public boolean isSingleton() { return true; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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.xsd"> <bean id="car" class="factorybean.CarFactoryBean"> <property name="name" value="baoma"></property> </bean> </beans>
package factorybean; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext c=new ClassPathXmlApplicationContext("cfactorybean.xml"); Car car=(Car) c.getBean("car"); System.out.println(car); } }