`
leonzhx
  • 浏览: 769464 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

1.  In Spring, objects aren’t responsible for finding or creating the other objects that they need to do their jobs. Instead, they’re given references to the objects that they collaborate with by the container. The act of creating these associations between application objects is the essence of dependency injection (DI) and is commonly referred to as wiring.

 

2.  As of Spring 3.0, there are two ways to configure beans in the Spring container. Traditionally, Spring configuration is defined in one or more XML files. But Spring 3.0 also offers a Java-based configuration option.

 

3.  When declaring beans in XML, the root element of the Spring configuration file is the <beans> element from Spring’s beans schema:

 

<?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-3.0.xsd">
  <!-- Bean declarations go here -->
</beans>

 

 

Within the <beans> you can place all of your Spring configuration, including <bean> declarations.

 

4.  The core Spring Framework comes with ten configuration namespaces:

 

 

Namespace

Purpose

aop

Provides elements for declaring aspects and for automatically proxying @AspectJ-annotated classes as Spring aspects.

beans

The core primitive Spring namespace, enabling declaration of beans and how they should be wired.

context

Comes with elements for configuring the Spring application context, including the ability to autodetect and autowire beans and injection of objects not directly managed by Spring.

jee

Offers integration with Java EE APIs such as JNDI and EJB.

jms

Provides configuration elements for declaring message-driven POJOs.

lang

Enables declaration of beans that are implemented as Groovy, JRuby, or BeanShell scripts.

mvc

Enables Spring MVC capabilities such as annotation-oriented controllers, view controllers, and interceptors.

oxm

Supports configuration of Spring’s object-to-XML mapping facilities.

tx

Provides for declarative transaction configuration.

util

A miscellaneous selection of utility elements. Includes the ability to declare collections as beans and support for property placeholder elements.

 

5.  The <bean> element is the most basic configuration unit in Spring. It tells Spring to create an object for you. The id attribute gives the bean a name by which it’ll be referred to in the Spring container. Spring creates its beans using reflection.

 

6.  The <constructor-arg> element is used to give Spring additional information to use when constructing a bean. If no <constructor-arg>s are given within <bean>, the default constructor is used. You can use value attribute of <constructor-arg> to set the constructor argument of a primitive type. While the ref attribute is used to indicate that the value passed to the constructor should be a reference to the bean of id specified:

<bean id="poeticDuke" class="com.springinaction.springidol.PoeticJuggler">
    <constructor-arg value="15" />
    <constructor-arg ref="sonnet29" />
</bean>

 

7.  Spring is ready-made to wire factory-created beans through the <bean> element’s factory-method attribute. The factory-method attribute that lets you specify a static method to be invoked instead of the constructor to create an instance of a class:

<bean id="theStage" class="com.springinaction.springidol.Stage" factory-method="getInstance" />

 

8.  By default, all Spring beans are singletons. When the container dispenses a bean (either through wiring or as the result of a call to the container’s getBean() method) it’ll always hand out the exact same instance of the bean. You have the option of declaring a scope for a bean. To force Spring to produce a new bean instance each time one is needed, you should declare the bean’s scope attribute to be prototype.

 

9.  Spring’s bean scopes let you declare the scope under which beans are created without hard-coding the scoping rules in the bean class itself:

 

 

Scope

What it does

singleton

Scopes the bean definition to a single instance per Spring container (default).

prototype

Allows a bean to be instantiated any number of times (once per use).

request

Scopes a bean definition to an HTTP request. Only valid when used with a web-capable Spring context (such as with Spring MVC).

session

Scopes a bean definition to an HTTP session. Only valid when used with a web-capable Spring context (such as with Spring MVC).

global-session

Scopes a bean definition to a global HTTP session. Only valid when used in a portlet context.

 

10.  Spring’s notion of singletons is limited to the scope of the Spring context. Unlike true singletons, which guarantee only a single instance of a class per classloader, Spring’s singleton beans only guarantee a single instance of the bean definition per the application context.

 

11.  To define setup and teardown for a bean, simply declare the <bean> with init-method and/or destroy-method parameters. The init-method attribute specifies a method that is to be called on the bean immediately upon instantiation. Similarly, destroy-method specifies a method that is called just before a bean is removed from the container.

 

12.  An optional way to define init and destroy methods for a bean is to write the bean class to implement Spring’s InitializingBean and DisposableBean interfaces. The Spring container treats beans that implement these interfaces in a special way by allowing them to hook into the bean lifecycle. InitializingBean declares an afterPropertiesSet() method that serves as the init method. As for DisposableBean, it declares a destroy() method that gets called when a bean is removed from the application context.

 

13.  You can take advantage of the default-init-method and default-destroy-method attributes on the <beans> element. The default-init-method attribute sets an initialization method across all beans in a given context definition. Likewise, default-destroy-method sets a common destroy method for all beans in the context definition.

 

14.  Spring can take advantage of a property’s setter method to configure the property’s value through setter injection. Bean properties can be configured in Spring using the <property> element. Instead of injecting values through a constructor argument, <property> injects by calling a property’s setter method:

<bean id="kenny2" class="com.springinaction.springidol.Instrumentalist">
    <property name="song" value="Jingle Bells" />
    <property name="instrument" ref="saxophone" />
</bean>

 

15.  Inner beans are beans that are defined within the scope of another bean. An inner bean is defined by declaring a <bean> element directly as a child of the <property> element to which it’ll be injected:

<bean id="kenny" class="com.springinaction.springidol.Instrumentalist">
  <property name="song" value="Jingle Bells" />
  <property name="instrument">
    <bean class="org.springinaction.springidol.Saxophone" />
  </property>
</bean>

 

You may also wire inner beans into constructor arguments:

<bean id="duke" class="com.springinaction.springidol.PoeticJuggler">
  <constructor-arg value="15" />
  <constructor-arg>
    <bean class="com.springinaction.springidol.Sonnet29"/>
  </constructor-arg>
</bean>

 

Note that the inner beans don’t have an id attribute set. Though it’s perfectly legal to declare an ID for an inner bean, it’s not necessary because you’ll never refer to the inner bean by name. Inner beans are only useful for injection once and can’t be referred to by other beans.

 

16.  The p namespace has a schema URI of http://www.springframework.org/schema/p. To use it, simply add a declaration for it in the Spring XML configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:p="http://www.springframework.org/schema/p"
  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-3.0.xsd">

 

With it declared, you can now use p:-prefixed attributes of the <bean> element to wire properties:

<bean id="kenny" class="com.springinaction.springidol.Instrumentalist"
      p:song = "Jingle Bells"
      p:instrument-ref = "saxophone" />

 

17.  Spring allows for injecting several kinds of collections:

 

Collection element

Usage

<list>

Wiring a list of values, allowing duplicates

<set>

Wiring a set of values, ensuring no duplicates

<map>

Wiring a collection of name-value pairs where name and value can be of any type

<props>

Wiring a collection of name-value pairs where the name and value are both Strings

 

The <list> and <set> elements are useful when configuring properties that are either arrays or some implementation of java.util.Collection. Both elements can be used almost interchangeably with properties of any type of java.util.Collection.

 <map> and <props> correspond to collections that are java.util.Map and java.util.Properties respectively.

 

18.  The <list> element contains one or more values:

<bean id="hank" class="com.springinaction.springidol.OneManBand">
  <property name="instruments">
    <list>
      <ref bean="guitar" />
      <ref bean="cymbal" />
      <ref bean="harmonica" />
    </list>
  </property>
</bean>

 

<ref> elements are used to define the values as references to other beans in the Spring context. It’s also possible to use other value-setting Spring elements as the members of a <list>, including <value>, <bean>, and <null/>. In fact, a <list> may contain another <list> as a member for multidimensional lists. <list> may be used with properties that are of any implementation of java.util.Collection or an array. Either <list> or <set> can be used to wire any implementation of java.util.Collection or an array. Just because a property is a java.util.Set, that doesn’t mean that you must use <set> to do the wiring. Even though it may seem odd to configure a java.util.List property using <set>, it’s certainly possible. In doing so, you’ll be guaranteed that all members of the List will be unique.

 

19.  The <map> element declares a value of type java.util.Map. Each <entry> element defines a member of the Map. An <entry> in a <map> is made up of a key and a value, either of which can be a primitive value or a reference to another bean. The key/value attribute specifies the key/value of the map entry as a String. The key-ref/value-ref specifies the key of the map entry as a reference to a bean in the Spring context:

<bean id="hank" class="com.springinaction.springidol.OneManBand">
  <property name="instruments">
    <map>
      <entry key="GUITAR" value-ref="guitar" />
      <entry key="CYMBAL" value-ref="cymbal" />
      <entry key="HARMONICA" value-ref="harmonica" />
    </map>
  </property>
</bean>

 

20.  The java.util.Properties class serves roughly the same purpose as Map, but limits the keys and values to Strings. The <props> element constructs a java.util.Properties value where each member is defined by a <prop> element. Each <prop> element has a key attribute that defines the key of each Properties member, while the value is defined by the contents of the <prop> element:

<bean id="hank" class="com.springinaction.springidol.OneManBand">
  <property name="instruments">
    <props>
      <prop key="GUITAR">STRUM STRUM STRUM</prop>
      <prop key="CYMBAL">CRASH CRASH CRASH</prop>
      <prop key="HARMONICA">HUM HUM HUM</prop>
    </props>
  </property>
</bean>

 

21.  To set a property to null, you simply use the <null/> element:

<property name="someNonNullProperty"><null/></property>

 

22.  Spring 3 introduced the Spring Expression Language (SpEL), a powerful yet succinct way of wiring values into a bean’s properties or constructor arguments using expressions that are evaluated at runtime. SpEL has a lot of tricks up its sleeves, including

    1)  The ability to reference beans by their ID

    2)  Invoking methods and accessing properties on objects

    3)  Mathematical, relational, and logical operations on values

    4)  Regular expression matching

    5)  Collection manipulation

 

23.  The #{} markers are a clue to Spring that the content that they contain is a SpEL expression. They could be mixed with non-SpEL values as well:

<property name="message" value="The value is #{5}"/>

 

24.  Numbers can even be expressed in scientific notation:

<property name="capacity" value="#{1e4}"/>

 

25.  Literal String values can be expressed in SpEL with either single or double quote marks:

<property name="name" value="#{'Chuck'}"/>

 

26.  A couple of other literal values you may use are the Boolean true and false values:

<property name="enabled" value="#{false}"/>

 

27.  You could use SpEL to wire one bean into another bean’s property by using the bean ID as the SpEL expression:

<property name="instrument" value="#{saxophone}"/>

 

28.  You can refer to another bean’s property using the Spring Expression Language:

<bean id="carl" class="com.springinaction.springidol.Instrumentalist">
    <property name="song" value="#{kenny.song}" />
</bean>

 

The first part (the part before the period delimiter) refers to the kenny bean by its ID. The second part refers to the song attribute of the kenny bean. It’s effectively as if you programmatically performed the following Java code:

Instrumentalist carl = new Instrumentalist();
carl.setSong(kenny.getSong());

 

29.  You could also invoke a method of a bean:

<property name="song" value="#{songSelector.selectSong()}"/>

 

30.  ?. operator makes sure that the item to its left isn’t null before accessing the thing to its right:

<property name="song" value="#{songSelector.selectSong()?.toUpperCase()}"/>

 

If selectSong() were to return a null, then SpEL wouldn’t even try to invoke toUpperCase() on it.

 

31.  The key to working with class-scoped methods and constants in SpEL is to use the T() operator. The result of the T(java.lang.Math) is a Class object that represents java.lang.Math.

 

 

32.  You can reference the Math class’s PI constant by:

<property name="multiplier" value="#{T(java.lang.Math).PI}"/>

 

Similarly, static methods can also be invoked on the result of the T() operator:

<property name="randomNumber" value="#{T(java.lang.Math).random()}"/>

 

33.  SpEL includes several operators that you can use to manipulate the values of an expression:

  

Operation type

Operators

Arithmetic

+, -, *, /, %, ^

Relational

<, >, ==, <=, >=, It, gt, eq, le, ge

Logical

and, or, not, !

Conditional

?: (ternary), ?: (Elvis)

Regular expression

matches

 

34.  Unlike Java, SpEL also offers a power-of operator in the form of the carat:

<property name="area" value="#{T(java.lang.Math).PI * circle.radius ^ 2}"/>

 

35.  It is consistent with Java in that the + operator can be used to concatenate String values.

 

36.  The less-than and greater-than symbols pose a problem when using these expressions in Spring’s XML configuration, as they have special meaning in XML. So, when using SpEL in XML, it’s best to use SpEL’s textual alternatives to these operators:

 

Operation

Symbolic

Textual

Equals

==

eq

Less than

lt

Less than or equals

<=

le

Greater than

gt

Greater than or equals

>=

ge

 

 

37.  SpEL’s ternary operator works the same as Java’s ternary operator:

<property name="instrument" 
    value="#{songSelector.selectSong()=='Jingle Bells'?piano:saxophone}"/>

 

38.  SpEL offers a variant of the ternary operator that simplifies the following expression:

<property name="song" value="#{kenny.song != null ? kenny.song : 'Greensleeves'}"/>

which is Elvis operator:

<property name="song" value="#{kenny.song ?: 'Greensleeves'}"/>

 

39.  The matches operator attempts to apply a regular expression (given as its right-side argument) against a String value (given as the left-side argument). The result of a matches evaluation is a Boolean value: true if the value matches the regular expression, false otherwise:

<property name="validEmail" 
    value="#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.com'}"/>

 

40.  The <util:list> element comes from Spring’s util namespace. It effectively creates a bean of type java.util.List that contains all of the values or beans that it contains:

<util:list id="cities">
  <bean class="com.habuma.spel.cities.City"
     p:name="Chicago" p:state="IL" p:population="2853114"/>
  <bean class="com.habuma.spel.cities.City"
     p:name="Atlanta" p:state="GA" p:population="537958"/>
  ...
</util:list>

 

41.  The most basic thing we could do with a collection is extract a single element out of the list and wire it into a property:

<property name="chosenCity" 
    value="#{cities[T(java.lang.Math).random() * cities.size()]}"/>

 

42.  The [] operator is also good for retrieving a member of a java.util.Map collection:

<property name="chosenCity" value="#{cities['Dallas']}"/>

 

43.  To load a properties configuration file into Spring:

<util:properties id="settings" location="classpath:settings.properties"/>

Here the settings bean will be a java.util.Properties that contains all of the entries in the file named settings.properties. With SpEL, you can access a property from <util:properties> in the same way you access a member of a Map:

<property name="accessToken" value="#{settings['twitter.accessToken']}"/>

 

44.  Spring makes two special selections of properties available to SpEL: systemEnvironment and systemProperties. systemEnvironment contains all of the environment variables on the machine running the application:

<property name="homePath" value="#{systemEnvironment['HOME']}"/>

 

Meanwhile, systemProperties contains all of the properties that were set in Java as the application started (typically using the -D argument):

<property name="homePath" value="#{systemProperties['application.home']}"/>

 

45.  The [] operator can also be used on String values to retrieve a single character by its index within the String.

 

46.  The selection operator .?[] will create a new collection whose members include only those members from the original collection that meet the criteria expressed between the square braces:

<property name="bigCities" value="#{cities.?[population gt 100000]}"/>

 

SpEL also offers two other selection operators, .^[] and .$[], for selecting the first and last matching items (respectively) from a collection.

 

47.  Collection projection involves collecting a particular property from each of the members of a collection into a new collection. SpEL’s projection operator (.![]) can do exactly that:

<property name="cityNames" value="#{cities.![name]}"/>

 

But projection isn’t limited to projecting a single property:

<property name="cityNames" value="#{cities.![name + ', ' + state]}"/>

 

Now the cityNames property will be given a list containing values such as “Chicago, IL”, “Atlanta, GA”, and “Dallas, TX”.

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics