Source: Internet
Author: User

Spring Expression Language

Spring expression language,spring Expressions language, referred to as Spel. Supports run-time queries and can manipulate object graphs.

Like the EL expression on a JSP page, the OGNL expression used in Struts2, Spel getXxx (),setxxx () according to JavaBean style method defines the properties of the Access object graph, fully conforms to our familiar operating habits .

Spel using #{ ... as a delimiter, all characters in the large frame number are considered to be spel expressions.

Integer: <property name= "Count" value= "#{5}"/>

Decimals: <property name= "Frequency" value= "#{89.7}"/>

Scientific notation: <property name= "Capacity" value= "#{1e4}"/>

String literals can use single or double quotation marks as the bounding symbol of a string

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

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

Boolean:<property name= "Enabled" value= "#{false}"/>

referencing other beans

<BeanID= "Emp04"class= "Com.soyoungboy.parent.bean.Employee">    < Propertyname= "EmpId"value= "1003"/>    < Propertyname= "EmpName"value= "Kate"/>    < Propertyname= "Age"value= "+"/>    < Propertyname= "Detp"value= "#{dept}"/></Bean>

Reference the property value of another bean as the value of one of its properties:

<BeanID= "EMP05"class= "Com.soyoungboy.parent.bean.Employee">    < Propertyname= "EmpId"value= "1003"/>    < Propertyname= "EmpName"value= "Kate"/>    < Propertyname= "Age"value= "+"/>    < Propertyname= "Deptname"value= "#{dept.deptname}"/></Bean>

Calling a static method

<BeanID= "Person6"class= "Com.soyoungboy.bean.Person">        < Propertyname= "FirstName"value= "#{person05.firstname}"></ Property>        < Propertyname= "Age"value= "#{18*88}"></ Property>        < Propertyname= "LastName"value= "#{t (Java.util.UUID). Randomuuid (). toString (). substring (0,5)}}"></ Property>    </Bean>

Calling a non-static method

<!--creates an object that calls the method of this object in an Spel expression -<BeanID= "Salarygenerator"class= "Com.soyoungboy.spel.bean.SalaryGenerator"/><BeanID= "Employee"class= "Com.soyoungboy.spel.bean.Employee">    <!--assign a value to a property by the return value of the object method -    < Propertyname= "Salayofyear"value= "#{salarygenerator.getsalaryofyear ()}"/></Bean>

Identifying components with annotations

① Normal components: @Component

Identify a component managed by the spring IOC container

② Persistence layer Components: @Respository

Identifies a persistence layer component managed by the spring IOC container

③ Business Logic Layer components: @Service

Identifies a business logic layer component managed by the spring IOC container

④ Presentation Layer Controller components: @Controller

Identifies a presentation layer controller component managed by the spring IOC container

⑤ Component Naming conventions

[1] Default: Use the component's simple class name to get the first letter lowercase string as the bean ID

[2] specifying the bean ID using the Value property of the component annotation

Note: In fact, Spring does not have the ability to identify whether a component is the type it is tagged in, even if the @respository annotation is used on a presentation layer controller component without any errors, so @respository, @Service, @ The controller notes are just for the developer to clarify the role that the current component plays.

Scan components:

<base-package= "Com.soyoungboy"></Context:component-scan  >

[The 1]base-package property specifies a base class package that needs to be scanned, and the spring container will scan all classes in the base class package and its child packages.
[2] When multiple packages need to be scanned, commas can be used.
[3] If you want to scan only specific classes, not all classes under the base package, you can use the Resource-pattern property to filter specific classes, for example:

< Context:component-scan      Base-package = "Com.soyoungboy.component"      resource-pattern= "Autowire/*.class"/>
Include and exclude

<context:include-filter> child nodes represent the target classes to be included

Note: It is often necessary to work with the Use-default-filters property to achieve the effect of "only some components". That is, by setting the Use-default-filters property to False, the default filter is disabled, and then only the components specified by the rules in Include-filter are scanned.

<context:exclude-filter> child nodes indicate the target class to exclude

Component-scan can have several include-filter and exclude-filter child nodes under a

Jar Package

One must be imported on the basis of the original JAR package combination: Spring-aop-4.0.0.release.jar

Component Assembly

Instances of service components are often needed in controller components, and instances of repository components are often used in service components. Spring can help us implement the assembly of attributes by annotating them.

The ② implementation is based on specifying the package to be scanned when the,<context:component-scan> element automatically registers a bean's post processor: an instance of Autowiredannotationbeanpostprocessor. The post processor can automatically assemble properties labeled @Autowired, @Resource, or @inject annotations.

③ @Autowired Annotations

[1] According to the type to achieve automatic assembly.

[2] constructors, normal fields (even non-public), all methods with parameters can be applied @autowired annotations

[3] By default, all properties that use @autowired annotations need to be set. When spring cannot find a matching Bean assembly property, an exception is thrown.

[4] If a property is allowed to not be set, you can set the required property of the @autowired annotation to False

[5] By default, when there are multiple type-compatible beans in the IOC container, Spring tries to match whether the Bean's ID value is the same as the variable name, and if the same is the case, assemble it. If the bean's ID value is not the same, automatic assembly by type will not work. The name of the bean can now be provided in the @qualifier annotation. Spring even allows @qualifiter annotations to be annotated on the method's formal parameters to specify the name of the injected bean.

[6] @Autowired annotations can also be applied to the properties of an array type, at which time spring will automatically assemble all matching beans.

[7] @Autowired annotations can also be applied on a collection property, when spring reads the collection's type information, and then automatically assembles all the beans that are compatible with it.

[8] @Autowired Note When used on JAVA.UTIL.MAP, if the map's key value is string, then spring will automatically assemble the value type-compatible bean as the value, and the bean's ID value as the key.

④ @Resource

@Resource annotations require a property of the bean name, and if the property is empty, the variable or method name at the label is automatically used as the name of the bean.

⑤ @Inject

@Inject and @autowired annotations also inject matching beans by type, but no reqired properties.

Generic Dependency Injection
 Public class Basedao<t> {        publicvoid  Daosave () {        System.out.println ("Save ..." );    }}
 Public class extends Basedao<book>{        publicvoid  Save () {        System.out.println ( "Bookdao...save ...");        Daosave ();    }}
 Public class Baseservice<t> {    @Autowired    basedao<T> Basedao;}
@Service  Public class extends Baseservice<book>{        publicvoid  Save () {        System.out.println ( "Bookservice ... Save: "+Basedao);}    }
 Public class Book {}

Configuration file:

<base-package= "Com.atguigu"></Context:component-scan  >

Test code:

 Public class ioctest {    new classpathxmlapplicationcontext ("Beans.xml");    @Test    publicvoid  Test () {        = Ioc.getbean (bookservice.  Class);        System.out.println ("====================");        Bookservice.save ();    }}


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.