Spring in Action②

Source: Internet
Author: User

Initializing and destroying beans

Init-method && Destory-method

<id= "Auditorium"  class= "Test." Audirorium "  init-method=" Turnonlights "  destory-method=" Turnofflights " />

If many beans in the context have the same name as the initialization method and the Destroy method. Can be declared in the Default-init-method && Default-destory-method method of the beans element.

<xmlns=**********    default-init-method= "Init"     Default-destroy-method= "destory">   <Bean/> .... < Beans >

Using the <property> element to configure bean properties, you need to invoke the setter method of the property to inject.

Similar to the following:

<name= "Age"  value= "/> "

Name Space P

<*********    xmlns:p= "http://www.springframework.org/schema/p"> ....    <  = "/>"   <p:instrument-ref= "saxophone"  />

Internal Bean (inner bean): Two types can be injected using the setter and the constructor.

< Propertyname= "instrument">    <Beanclass= "Test. Saxophone "/></ Property>similar:<BeanID= "saxophone"class= "Test. Saxophone "/>...  < Propertyname= "instrument"ref= "saxophone"/>...

Assembly set: Various collections can be assembled, such as Java.util.List, Java.util.Set, Java.util.Collection, java.util.map,java.util.properties and so on. You can set properties and null values.

<list> Repeatable

<set> Non-repeatable

<map> Name value can be any type

<props> Name value must be of type string

< Propertyname= "Instruments">    <List>        <refBean= "Guitar"/>        <refBean= "Harmonica"/>    </List></ Property>*****************************************< Propertyname= "Instruments">    <List>        <refBean= "Guitar"/>        <refBean= "Guitar"/>    </List></ Property>*****************************************< Propertyname= "Instruments">    <Map>        <entryKey= "Guitar"Value-ref= "Guitar"/>        <entryKey= "Hamonica"Value-ref= "Harmonica"/>    </Map></ Property>***************************************key:entry key is a string type when the Key-ref:entry key is a different bean when value: Entry value is a string type when Value-ref:entry value is a different bean ***************************************< Propertyname= "Instruments">    <Props>        <propKey= "Guitar">Guitar hum hum hum</prop>        <propKey= "Hamonica"><NULL/></prop>    </Props></ Property>

Assembling with an expression

Spel (Spring Expression Language)

Literal value

< Propertyname= "Count"value= "#{5}"/>< Propertyname= "Count"value= "#{5.98}"/>< Propertyname= "Name"value= "#{' Chuck '}"/>< Propertyname= "Enable"value= "#{true}"/>

Reference Bean

<!--Reference Bean -< Propertyname= "instrument"value= "#{guitar}"/>< Propertyname= "instrument"ref= "Guitar"/><!--will call Kenny.getinstrument () -< Propertyname= "instrument"ref= "#{kenny.instrument}"/>< Propertyname= "Song"ref= "#{kenny.selectsong (). toUpperCase ()}"/><!--avoid null pointer exceptions, use the?. instead. Ensure that the left value is not null - < Propertyname= "Song"ref= "#{kenny.selectsong ()?". toUpperCase ()} "/>

Method of calling Class: T () operator

< ...     value = "#{t (java.lang.Math). PI} "/>

Using Spel for numeric operations

<...  value= "#{counter.total +"/><...  value= "#{t (java.lang.Math). PI * circle.radius^2} "/><!--Compare Values -<...  value= "#{circle.radius le"/><!--le less or equal -<!--eq equal -<!--LT Less -<!--GT Great -<!--ge Great or Equal -<!--logical expression and or not -<!--conditional expression? : -<!--Regular Expressions -<...  value= "#{admin.email matches ' [A-za-z0-9._%+-][email protected][a-za-z0-9.-]+\\.com '? "Right Email ":" Wrong Email "}"

Using Spel to filter collections

 Public class city{    private  String name;     Private String State;     Private Integer population;}

<util:listID= "Cities">    <Beanclass= "Test. City "P:name= "Chicago"p:state "IL" P:population= "232322"/>    <Beanclass= "Test. City "P:name= "Houston"p:state "TX" P:population= "235452"/></util:list><!--Accessing Collections -<... value= "#{cities[1]}"/>

Load Properties configuration file

<id= "Settings"  location= "Classpath:settings.properties" /> <  value= "#{settings[' Twitter.accesstoken '}"/>

Systemenvironment all environment variables on the machine

Systemproperties contains all the properties set at Java startup (usually with the-D parameter)

<name= "HomePath"  value= "#{systemenvironment[' HOME"} "/ ><name= "HomePath"  value= "#{systemproperties[" Application.home ']} "

Spel Query Collection

The query operator.? [] (all occurrences). ^[] (first occurrence). $[] Last Occurrence

Eg cities.? [Population GT 10000]---Collection

Projection operator.! []

Eg cities.! [Name] contains only the city name of the string type collection

cities.! [Name + ', ' + state] contains a collection of string types for city names and regions

Spring in Action②

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.