src\dayday\Person.java
package dayday;
/**
* Created by I am master on 2016/11/28.
*/
public class Person {
private String name;
private int age;
private String city;
private Car car;
public void setName(String name){
this.name=name;
}
public void setAge(int age){
this.age=age;
}
public void setCity(String city){
this.city=city;
}
public void setCar(Car car){
this.car=car;
}
public void toprint(){
System.out.println("["+"name="+name+" age="+age+" car="+car+" city="+city+"]");
}
}
src\dayday\City.java
package dayday;
/**
* Created by I am master on 2016/11/28.
*/
public class City {
private String livecity;
private String street;
public void setLivecity(String livecity) {
this.livecity=livecity;
}
public void setStreet(String street){
this.street=street;
}
public String getLivecity(){
return livecity;
}
public String getStreet(){
return street;
}
public void printinfo(){
System.out.println("["+"city="+livecity+" street="+street +"]");
}
}
src\dayday\Car.java
package dayday;
/**
* Created by I am master on 2016/11/28.
*/
public class Car {
private String name;
private double tyrepremeter;
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setTyrepremeter(double tyrepremeter){
this.tyrepremeter=tyrepremeter;
}
public double getTyrepremeter(){
return tyrepremeter;
}
public String toString(){
return "["+"carname="+name+"]";
}
}
src\dayday\Main.java
package dayday;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by I am master on 2016/11/28.
*/
public class Main {
public static void main(String[] args){
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
Person person=ctx.getBean("person",Person.class);
person.toprint();
}
}
src\beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="car" class="dayday.Car">
<!--使用spel为属性附一个字面值-->
<property name="name" value="#{'Audi'}"></property>
<!--使用spel引用类的静态属性-->
<property name="tyrepremeter" value="#{T(java.lang.Math).PI*80}"></property>
</bean>
<bean name="city" class="dayday.City">
<property name="livecity" value="#{'shaoxing'}"></property>
<property name="street" value="#{'xinchang'}"></property>
</bean>
<bean id="person" class="dayday.Person">
<property name="name" value="dayday"></property>
<!--使用spel引用其他的bean-->
<property name="car" value="#{car}"></property>
<!--使用spel引用其他的bean的其他属性-->
<property name="city" value="#{city.livecity}"></property>
<property name="age" value="#{17}"></property>
</bean>
</beans>
运行结果
[name=dayday age=17 car=[carname=Audi] city=shaoxing]
SPEL的作用:
1:引用其他bean和ref作用差不多
2:引用其他bean的属性/方法
3:调用静态方法和静态属性
4:字面值