Assembly using Spring Expression Language

 

1.1 Injecting external values

In Spring, the easiest way to handle external values ​​is to declare a property source and retrieve the property through Spring's Environment. For example, Listing 3.7 shows a basic Spring configuration class that uses external properties to wire the BlankDisc bean.

Listing 3.7 Using the @PropertySource annotation and Environment

 

In this example, @PropertySource references a file called app.properties in the classpath. It will roughly look like this:

This properties file is loaded into Spring's Environment, from which properties can be retrieved later. At the same time, in the disc() method, a new BlankDisc is created, and its constructor parameters are obtained from the properties file, which is obtained by calling

Implemented with getProperty().

When we learn about Environment, we will find that the getProperty() method shown in Listing 3.7 is not the only way to obtain property values. The getProperty() method has four overloaded variants.

The first two forms of the getProperty() method return a value of type String. We have already seen how to use the first getProperty() method in Listing 3.7. However, you can modify the @Bean method slightly so that the specified property does not exist

When , a default value will be used:

The remaining two getProperty() methods are very similar to the previous two, but they do not treat all values ​​as String. For example, suppose the value you want to get represents the number of connections maintained in the connection pool. If we get a value of type String from the properties file, we also need to convert it to type Integer before using it. However, if you use the overloaded form of getProperty(), you can solve this problem very conveniently:

Retrieving properties directly from the Environment is very convenient, especially when wiring beans in Java configuration. However, Spring also provides a way to assemble properties via placeholders whose values ​​are derived from a property source.

Parse property placeholders

Spring has always supported defining properties into external property files and inserting them into Spring beans using placeholder values. In Spring assembly, placeholders take the form of property names wrapped with "${...}". As an example, we can do the following in XML

Way to parse the BlankDisc constructor parameters:

As you can see, the value given by the title constructor parameter is parsed from a property named disc.title. The artist parameter is assembled with the value of a property named disc.artist. In this way, the XML configuration does not use any hardcoded values, its value is parsed from a source other than the configuration file. (We 'll discuss how these properties are resolved later.)

 

If we rely on component scanning and autowiring to create and initialize application components, then there are no configuration files or classes that specify placeholders. In this case, we can use the @Value annotation, which is used very similarly to the @Autowired annotation. Compare

For example, in the BlankDisc class, the constructor could be changed to look like this:

 

In order to use placeholders, we must configure a PropertyPlaceholderConfigurer bean or PropertySourcesPlaceholderConfigurer bean. As of Spring 3.1, the because of its ability to resolve placeholders based on the Spring Environment and its property sources. The following @Bean method configures the PropertySourcesPlaceholderConfigurer in Java:

If you want to use XML configuration, the <context:propertyplaceholder> element in the Spring context namespace will generate

成PropertySourcesPlaceholderConfigurer bean:

 

1.2 Assembly using the Spring Expression Language

Spring 3 introduces the Spring Expression Language ( SpEL), which provides a powerful and concise way to wire values ​​into bean properties and constructor parameters, where the expressions used are executed value is calculated. With SpEL, you can achieve unimaginable rigging effects that are difficult (or even impossible) to achieve using other rigging techniques . SpEL has many features, including:

Use the bean's ID to refer to the bean;

calling methods and accessing properties of objects;

perform arithmetic, relational and logical operations on values;

Regular expression matching;
set operations.

 

As you'll see later in this book, SpEL can be used in other . For example, Spring Security supports the use of SpEL expressions to define security restriction rules . Additionally, if you use Thymeleaf templates as views in your Spring MVC application , those templates can reference model data using SpEL expressions. To get started, let's look at a few sample SpEL expressions and how to inject them into beans . Then we'll dive into some of SpEL's basic expressions, which can be combined

to form more powerful expressions.

 

The first thing you need to know is that SpEL expressions are placed inside "#{ ... }", which is somewhat similar to property placeholders, which need to be placed between "${ ... }" middle. The following is probably the simplest SpEL expression:

Of course, in real applications, we may not use such simple expressions . We might use more interesting expressions like:

Its final result is the milliseconds of the current time at the moment the expression was evaluated. The T() expression will treat java.lang.System as the corresponding type in Java, so its statically modified currentTimeMillis() method can be called.

SpEL expressions can also refer to other beans or properties of other beans. For example, the following expression will evaluate to the artist property of the bean with the ID sgtPeppers

 

When creating a bean via component scan, we can use the @Value annotation when injecting properties and constructor parameters, much like the property placeholders we saw earlier. However , here we are not using placeholder expressions, but SpEL expressions. For example , the following sample shows BlankDisc, which gets the album name

 

Reference beans, properties and methods

Another basic thing SpEL can do is to refer to other beans by ID. For example , you can use SpEL to wire a bean into a property of another bean, using the bean ID as the SpEL expression (in this case,

is sgtPeppers):

 

Now, suppose we want to reference the artist
property :

In addition to referencing properties of beans, we can also call methods on beans. For example, assuming there is another bean with the ID artistSelector, we can call the bean's selectArtist() method in the SpEL expression as follows:

 

Use types in expressions

To access class-scoped methods and constants in SpEL, rely on the key . For example, to express Java's Math class in SpEL, use the T() operator as follows :

The result of the T() operator shown here will be a Class object representing java.lang.Math. We can even wire it into a . But the real value of the T() operator is that it can

Access static methods and constants of the target type.

The expressions about spel will not be introduced in detail here. You can refer to the third chapter of spring combat, the spring advanced assembly chapter!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325089870&siteId=291194637