1.SpEL to achieve
The person class, whose attributes are as follows, does not write the Get,set,tostrong method.
Private String name; Private Car car; Private String city; // The city attribute refers to the attribute of city in address Private String info; // based on the price property of car to determine that Info,price is greater than 300,000, not greater than 300,000
Car class, whose properties are as follows, the Set,get,tostring method will not write
Private String brand; Private Double price ; Private Double tyreperimeter; // the circumference of the tyre
Address class, whose properties are as follows, the Set,get,tostring method will not be
Private String city; Private String street;
Build the spring bean configuration file: beans.xml
<!--spel Use #{...}--<bean id= "address"class= "com.atguigu.spring.beans.Address" > <property name= "city" value= "#{' Beijing '}" ></property> & Lt;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.Ma Th). PI * "></property> </bean> <bean id=" Person "class= "com.atguigu.spring.beans.Person" > <property name= "name" value= "#{' panpan '}" ></property> < ;p roperty name= "car" value= "#{car}" ></property> <property name= "city" value= "#{address.city}" ></p roperty> <property name= "info" value= "#{car.price>300000? ' Golden collar ': ' blue collar ' ></property> </bean>
The main class test method under the SRC directory;
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. Static factory mode, Spring Bean configuration method;
Establish the Staticcarfactory class:
packagecom.atguigu.spring.beans;Importjava.util.HashMap;Importjava.util.Map; public classStaticcarfactory {//static factory mode, Spring Bean Configuration Method Private Staticmap<string, car> cars=Newhashmap<string, car>(); Static{cars.put ("KKK",NewCar ("changan", 423423,432.43)); Cars.put ("PPP",NewCar ("fute", 42323,32.43)); }//in an XML file, you can set and get the Getcar method public StaticCar Getcar (String Name) {returnCars.get (name); }}
Configure the Bean in the Beans.xml file;
class= "com.atguigu.spring.beans.StaticCarFactory" factory-method= "getcar" >< Constructor-arg value= "PPP" ></constructor-arg></bean>
Test in the main class;
ApplicationContext app=New classpathxmlapplicationcontext ("beans.xml"); Car car1= (car) App.getbean ("car1"); System.out.println (car1);
-------------------------------------------------------------------------------------
3. The instance factory method, realizes the Bean configuration;
Build Class: Instancefactory
packagecom.atguigu.spring.beans;Importjava.util.HashMap;Importjava.util.Map; public classInstancefactory {Privatemap<string, car> cars=NULL; //method of the instance factory publicinstancefactory () {cars=Newhashmap<string, car>(); Cars.put ("KKK",NewCar ("luhu", 434233, 43.2)); Cars.put ("QQQ",NewCar ("fute", 656546, 45.4)); } publicCar Getcar (String brand) {returnCars.get (brand); }}
Configure the Bean in the Beans.xml file;
class= "com.atguigu.spring.beans.InstanceFactory" ></bean> <bean id= "car2" factory-bean= " Factory "factory-method=" getcar "> <constructor-arg value=" QQQ "></constructor-arg></bean >
In the class main, test:
ApplicationContext app=New classpathxmlapplicationcontext ("beans.xml"); Car car2= (car) App.getbean ("car2"); System.out.println (car2);
------------------------------------------------------------------------------------------
4.FactoryBean configuration method: inherit interface: factorybean, generic type is car;
packagecom.atguigu.spring.beans;Importorg.springframework.beans.factory.FactoryBean;//How to configure Factorybean public classCarfactorybeanImplementsFactorybean<car>{ PrivateString brand; public voidSetbrand (String Brand) { this. Brand =brand; } @Override//returns the Bean object, which is the car publicCar GetObject ()throwsException {return NewCar (brand, 200000, 1.2); } @Override//returns the type of Bean publicClass<?>getobjecttype () {returnCar.class; } @Override//is not but the strength of public BooleanIssingleton () {return true; } }
Configure the Bean in the Beans.xml file;
class= "com.atguigu.spring.beans.CarFactoryBean" > <property name= "brand" value= "panpan" > </property></bean>
In the class main, test:
ApplicationContext app=New classpathxmlapplicationcontext ("beans.xml"); Car Car3= (car) App.getbean ("car3"); System.out.println (car3);
Spring Framework Bean Configuration (2): spel: referencing beans, properties, and methods ...