Configure some of the bean's detail literals
If a special symbol is included, the direct write will error. You can use this <!. [cdata[]]> wrapped up .
For example, the value of the configuration property contains <> and other special symbols, the direct write will be error. You can use this <![. Cdata[]]> wrapped up.
class= "Com.spring.beans.Car" > <constructor-arg value= "Audi" index= "0" type= "java.lang.String" ></constructor-arg> <constructor-arg index= "1" type= "java.lang.String" > <value> <! [cdata[<shanghai^>]]></value> </constructor-arg> < Constructor-arg type= "Double" > <value>250</value> </constructor-arg> </bean>
referencing other beans
You can use the <ref> element or the property's ref attribute to establish a reference relationship between the beans
For example, there is a person class, there is a property is car class. When you configure a bean, you need to assign a value to the car property, which you can use with the ref attribute.
Person class:
PackageCom.spring.beans; Public classPerson {PrivateString name; Private intAge ; Privatecar Car; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicCar Getcar () {returncar; } Public voidSetcar (car car) { This. Car =car; } @Override PublicString toString () {return"Person [name=" + name + ", age=" + Age + ", car=" + Car + "]"; } }
Configuration: You can use the property's ref attribute to establish a reference relationship between beans
<!--If a special symbol is included, the direct write will cause an error. You can use this <!. [cdata[]]> wrapped up-<bean id="Car2" class= "Com.spring.beans.Car" > <constructor-arg value= "Audi" index= "0" type= "java.lang.String" ></constructor -arg> <constructor-arg index= "1" type= "java.lang.String" > <value><! [cdata[<shanghai^>]]></value> </constructor-arg> <constructor-arg type= "Double" > <value>250</value> </constructor-arg> </bean> <bean id= "Person"class= "Com.spring.beans.Person" > <property name= "name" value= "Tom" ></property> <property name= " Age "Value=" ></property><!--can use the property's ref attribute to establish a reference relationship between beans- -<property name= "Car"ref= "Car2"></property> </bean>
or use the <ref> element configuration:
class= "Com.spring.beans.Person" > <property name= "name" value= "Tom" ></property> <property name= "Age" value= "></property> <!--can use the property's ref attribute to establish a reference relationship between beans-- <!--<property name= "car" ref= "car2" ></property>-- <property name= "Car" > Bean= "Car2"></ref> </property> </bean>
Use this person class.
Car car= (CAR) ctx.getbean ("Car"); Car Car2= (car) ctx.getbean ("Car2"); SYSTEM.OUT.PRINTLN (car); System.out.println (CAR2); person person = (person) ctx.getbean ("person"); SYSTEM.OUT.PRINTLN (person);
Operation Result:
You can also include a bean's declaration in a property or constructor, which is called an internal bean.
class= "Com.spring.beans.Person" > <property name= "name" value= "Tom" ></property> <property name= "Age" value= "></property> <property name=" Car "> <bean class = "Com.spring.beans.Car" > <constructor-arg value= "Ford" ></constructor-arg> < Constructor-arg value= "Changan" ></constructor-arg> <constructor-arg value= "$" type= "double" ></constructor-arg> </bean> </property> </bean>
For example, inject a null value:
To assign a value to a cascading property:
Collection Properties
Use the list node to assign a value to a list-type property
Here's an example of <list> :
For example:
Packagecom.spring.beans.collections;Importjava.util.List;ImportCom.spring.beans.Car; Public classPerson {PrivateString name; Private intAge ; private list<car> cars; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicList<car>Getcars () {returncars; } Public void Setcars (list<car> cars) {this.cars = Cars; } @Override PublicString toString () {return"Person [name=" + name + ", age=" + Age + ", car=" + Cars + "]"; }}
Configuration:
<!--test Collection Properties-- class= "Com.spring.beans.collections.Person" > <property name= "name" Value= "Mike" ></property> <property name= "age" value= "></property>"
<!-- using the list node to assign values to a list-type property- <property name= "Cars" > <list> <ref bean= "Car" ></ref> <ref bean= "car2" ></ref> </list> </ property> </bean>
Run:
The use of set sets is almost similar to the list set, as is the use of the map collection:
Create a new Newperson class:
Packagecom.spring.beans.collections;ImportJava.util.Map;ImportCom.spring.beans.Car; Public classNewperson {PrivateString name; Private intAge ; Private map<string,car>cars; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } PublicMap<string,car>Getcars () {returncars; } Public void Setcars (map<string,car> cars) {this.cars = Cars; } @Override PublicString toString () {return"Person [name=" + name + ", age=" + Age + ", car=" + Cars + "]"; }}
Configuration:
<!--Test Map Collection Properties-- class= "Com.spring.beans.collections.NewPerson" > <property name= " Name "Rose" ></property> <property name= "age" value= "value=" ></property> < Property name= "Cars" > <map> <entry key= "AA" value-ref= "Car" ></entry> <entrykey= "BB"value-ref= "car2"></entry> </map > </property> </bean>
Configure the Properties property value
Using props and prop to assign values to property properties
Packagecom.spring.beans.collections;Importjava.util.Properties; Public classDataSource {Private Properties properties; PublicProperties getProperties () {returnproperties; } Public voidSetProperties (Properties properties) { This. Properties =properties; } @Override PublicString toString () {return"DataSource [properties=" + Properties + "]"; } }
Configuration:
<!--Configure the Properties property value--<bean id= "DataSource"class= "Com.spring.beans.collections.DataSource" > <!--using props and prop to assign values to property properties-< PropertyName= "Properties" > <Props> < prop key= "User" >root</prop> <prop key= "password" >911581</prop> <propKey= "Jdbcurl" >jdbc:mysql:// /sys</prop><prop key= "Driverclass" >com.mysql.jdbc.Driver</prop> </props> </property> < ;/bean>
Operation Result:
Configuring a singleton Collection Bean
Configure a singleton collection bean for use by multiple beans, and you need to import the Util namespace.
In order to import the Util namespace, you need to check the util here.
Then configure the following:
Take the collection Bean of the list collection as an example:
<!--Configure a singleton collection bean for reference by multiple beans, import the Util namespace- <util:list id= "CARS5" > < Ref bean= "Car1" ></ref> <ref bean= "car2" ></ref> </util:list >
For other types of collections, simply replace the list with the corresponding collection type name, and then the contents of the util are configured according to the above collection.
Use Direct with ref:
class= "Com.spring.beans.collections.NewPerson" > <property name= "name" value= "Rose" ></ property> <property name= "age" value= "></property> <property name=" Cars "ref=" CAR5 " ></property> </bean>
Assigning a value to a bean property through the P-namespace
By assigning a value to a bean's properties through the P namespace, you need to import the P-namespace first, which is more concise than the traditional configuration.
First import the P namespace
To configure the Bean:
<!--assign a value to a bean's property through the P namespace, you need to first import the P namespace-- class= "Com.spring.beans.collections.Person"
p:age= "P:name=" "Zhang San Feng" p:cars-ref= "Cars" ></bean>
Attention:
1, for the normal attribute assignment: P: Property name = "Value";
2, the assigned property needs to be referenced by other beans, such as P: Cars-ref, which refers to other beans, the notation is: the assignment of the property-ref.
---restore content ends---
02-spring Learning-Attribute configuration details