Spring uses Spel

Source: Internet
Author: User

1.car class

Package Com.hxzy;


public class Car {


Private String brand;
Private String address;
private int speed;
private double perimeter;

public void Setbrand (String brand) {
This.brand = brand;
}

public void setaddress (String address) {
this.address = address;
}

public void setspeed (int speed) {
This.speed = speed;
}

public void Setperimeter (double perimeter) {
This.perimeter = perimeter;
}

Public double Getperimeter () {
return perimeter;
}

Public car () {

}

Public car (string brand, string address, int speed) {
Super ();
This.brand = brand;
this.address = address;
This.speed = speed;
}


@Override
Public String toString () {
Return "car [brand=" + Brand +], address= "+ address +", speed= "
+ Speed + ", perimeter=" + perimeter + "]";
}


}

2.person class

Package Com.hxzy;


public class Person {


private String name;
Private car car;

Private String City;
Determine info According to the car's XX
Private String info;

public void SetName (String name) {
THIS.name = name;
}

public void Setcar (car car) {
This.car = car;
}

public void Setcity (String city) {
this.city = City;
}

public void SetInfo (String info) {
This.info = info;
}


Public person (string name, car car, String city, string info) {
Super ();
THIS.name = name;
This.car = car;
this.city = City;
This.info = info;
}


Public person () {
}


@Override
Public String toString () {
Return ' person [name=] + name + ', car= ' + car + ', city= ' + City
+ ", info=" + info + "]";
}


}

3.address class

Package Com.hxzy;


public class Address {


Private String City;
Private String Street;


Public String getcity () {
return to City;
}


public void Setcity (String city) {
this.city = City;
}


Public String Getstreet () {
return to Street;
}


public void Setstreet (String street) {
This.street = Street;
}


@Override
Public String toString () {
Return "Address [city=" + City + ", street=" + Street + "]";
}


Public Address (String city, String street) {
Super ();
this.city = City;
This.street = Street;
}


Public address () {
Super ();
}


}


4.bean Configuration Class

<?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= "Com.hxzy.Car" >
<!--use Spel assignment string-->
<property name= "brand" value= "#{' Chevrolet '}" ></property>
<property name= "Address" value= "SAIC" ></property>
<property name= "Speed" value= "#{' 210 '}" ></property>
<!--use Spel to refer to static properties of a class-->
<property name= "perimeter" value= "#{t" (Java.lang.Math). PI * "></property>
</bean>

<bean id= "Address" class= "com.hxzy.Address" >
<property name= "City" value= "#{' Zhengzhou '}" ></property>
<property name= "Street" value= "#{' Nanyang Road '" ></property>
</bean>

<bean id= "Person" class= "Com.hxzy.Person" >
<property name= "name" value= "Happy Man" ></property>
<property name= "Car" value= "#{car}" ></property>
<!--use Spel to refer to the properties of other beans #{car.address} usage errors, I do not know why the error. -->
<!--property Name= "City" value= "#{car.address}" ></property-->
<property name= "City" value= "#{address.city}" ></property>
<!--property Name= "City" value= "Hefei" ></property-->
<!--spel using operator-->

<!--prompts for errors when using the following caused By:org.springframework.expression.spel.SpelEvaluationException:EL1008E: (POS 4): Field or The property ' speed ' cannot is found on object of type ' Com.hxzy.Car ', for some reason. -->
<!--property Name= "Info" value= "#{car.speed > 200?" ' Good car ': ' Ordinary Car '} ' ></property-->

<!--correct--> when using the following methods
<property name= "Info" value= "#{address.street.equals" (' Nanyang Road ')? ' Joint venture ': ' Garbage Truck '} ' ></property>
<!--property Name= "Info" value= "Good Car" ></property-->
</bean>


</beans>

5. Test class

Abstractapplicationcontext CTX = new Classpathxmlapplicationcontext ("Spel.xml");

Car car = [car] ctx.getbean ("Car");
SYSTEM.OUT.PRINTLN (car);

Address: Ctx.getbean ("address");
SYSTEM.OUT.PRINTLN (address);

Person mans = (person) ctx.getbean ("person");
System.out.println (Mans);
Ctx.close ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.