Spring Bean Configuration 2

Source: Internet
Author: User
Tags arithmetic operators dateformat instance method

Spring expression language: SpelSpring Expression Language(abbreviationSpel): Is apowerful expression language supporting run-time querying and manipulating object graphs。 •syntax similar to El:spel using #{...} as delimiters, all characters in the large frame number will be considered Spel•Spel provides the convenience for dynamic assignment of bean properties• by Spel: – References to beans through the bean's ID – calling methods and referencing properties in objects – evaluates the value of an expression

– Matching of regular expressions

Spel: Literal

• The representation of the literal:

– Integer: <property name= "Count" value= "#{5}"/>– decimal: <property name=" Frequency "value="#{89.7}"/>– scientific notation: <property name=" Capacity "value="#{1e4}"/>–string can use single or double quotation marks as the bounding symbol of a string: <property name= "name" value= "#{' Chuck '}"/> or <property name= ' name ' value= '#{"Chuck"}'/>–boolean:<property name= ' enabled "value="#{false}"/>spel: Referencing Beans, properties, and methods

• Referencing other objects:

  

• Referencing properties of other objects

  

• Call other methods, and you can also chain-operate

    

• Call a static method or static property: By calling a static method of a class by T (), it returns a class Object and then calls the appropriate method or property:

Spel supported op-Symbols

• Arithmetic operators: +,-, *,/,%, ^:

  

• The plus sign can also be used as a string connection:

  

• comparison operator: <,,, = =, <=, >=, lt, GT, eq, le, ge

    

• Logical operation Symbols: and, or, not, |

  

if-else operator:?: (Ternary),?: (Elvis)

  

Variants of the If-else

  

• Regular Expressions: Matches

The life-cycle method of the Bean in the IOC container

The Spring IOC container manages the bean lifecycle, and Spring allows custom tasks to be performed at specific points in the bean lifecycle.

The process by which the Spring IOC container manages the life cycle of the Bean:

– Creating a bean instance from a constructor or factory method – setting values for bean properties and references to other beans – invoking the Bean's initialization method –bean can be used – when the container is closed, call the Bean's destruction method • Set ini in the bean's declaration The T-method and Destroy-method properties specify initialization and destruction methods for the bean. Creating a bean Post processor Bean The Post processor allows additional processing of the bean before and after the initialization method is called. Bean back processor to all the IOC containers Bean instances are processed one by one rather than a single instance. The typical application is to check the integrity of the bean properties or change the Bean's properties according to specific criteria. • For the Bean post processor, the Beanpostprocessor interface needs to be implemented. Before and after the initialization method is called, Spring will pass each Bean instance to the following two methods of the above interface: public object Postprocessafterinitialization (object arg0, String arg1)  ; public Object Postprocessbeforeinitialization (object arg0, String arg1). Also configure in spring xml:
<!--Configuring the Bean Post Processor: No configuration id attribute is required, the IOC container recognizes that he is a bean post processor and calls its method--><bean class= "Com.atguigu.spring.ref.MyBeanP Ostprocessor "></bean>
The bean's life cycle after adding the bean back processor

The process by which the Spring IOC container manages the life cycle of the Bean:

– Create a bean instance from a constructor or factory method – Set a value for the Bean's property and a reference to another bean – pass the bean instance to the Postprocessbeforeinitialization method of the bean's post processor – call the Bean Initialization method – Passing a bean instance to the Postprocessafterinitialization method of the bean's post processor –bean can be used – when the container is closed, the method of destroying the bean is called by calling the static factory method to create the bean

• Calling the static factory method to create the bean is the process of encapsulating the object creation into a static method . When a client needs an object, it simply calls the static method, but differs from the details of the object being created.

• To declare a bean created through a static method, you need the Bean'sclass in the class attribute that specifies the method that owns the factory, while atname of the factory method specified in the factory-method attribute. Finally, usethe <constrctor-arg> element passes method parameters for this method. Creating a Bean by invoking an instance factory method

• Instance Factory method: encapsulates the creation of an object into a method of another object instance . When a client needs to request an object, it simply calls the instance method without needing to care about the object's creation details.

• To declare a Bean created through the instance factory method

– In The Bean'sThe Bean that owns the factory method is specified in the Factory-bean property– InFactory-methodproperty to specify the name of the factory method – Use theConstrutor-argElement implements the Factorybean interface for the factory method pass method parameter configuration Bean in Spring IOC container

There are two types of beans in spring, one is a normal bean and the other is a factory bean, or Factorybean.

• Unlike a normal bean, the factory bean returns an object that is not an instance of the specified class, and returns the object returned by the factory Bean's GetObject method. Implements the Factorybean interface.
1<!--Configure Beans by factory Method--2<!--1. Through the static factory method: There is a static method in a class that can return an instance of a class (understand)-3<!--inclassSpecifies the full class name of the static factory method in Factory-method, specifying the method name of the static factory Method---4<bean id= "DateFormat"class= "Java.text.DateFormat" factory-method= "Getdateinstance" >5<!--can specify parameters for static factory methods by Constructor-arg child nodes--6<constructor-arg value= "2" ></constructor-arg>7</bean>8     9<!--2. Instance Factory method: You first need to create a factory object and then call the factory's non-static method to return the instance (understanding)-Ten<!--①. Create a factory-corresponding bean-- One<bean id= "SimpleDateFormat"class= "Java.text.SimpleDateFormat" > A<constructor-arg value= "Yyyy-mm-dd hh:mm:ss" ></constructor-arg> -</bean> -      the<!--②. There is an instance factory method to create a bean instance-- -<!--Factory-bean point to Factory bean, Factory-method Specify factory Method (understanding)- -<bean id= "datetime" factory-bean= "SimpleDateFormat" factory-method= "Parse" > -<!--calling the factory method through Constructor-arg requires an incoming parameter-- +<constructor-arg value= "1990-12-12 12:12:12" ></constructor-arg> -</bean> +      A<!--configuration to create an instance of the bean by Factroybean (understanding)- at<bean id= "User"class= "Com.atguigu.spring.ref.UserBean" ></bean>
View CodeAnnotation-based configuration of beans (based on annotation configuration beans; Assembly of bean attributes based on annotations) scan components in Classpath

• Component scanning (component scanning): Spring is able to automatically scan, detect and instantiate components with specific annotations from the classpath.

• Specific components include:

– @Component: Basic annotations that identify a Spring-managed component – @Respository: Identify the persistence layer component – @Service: Identify the service layer (business layer) component – @Controller: Identify the presentation layer component

• For scanned components, Spring has a default naming policy : Use unqualified class name, first letter lowercase. You can also identify the name of a component in the annotation with the Value property values

• After you have used a specific annotation on the component class, you also need to declare <context:component-scan> in the Spring configuration file:

–The 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.–When you need to scan multiple packages, you can use commas to separate. – 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:include-filter> child nodes represent the target classes to be included–<context:exclude-filter> child nodes indicate the target class to exclude–<context:component-scan> can have a number of <context:include-filter> and <context:exclude-filter> sub-nodes < Context:include-filter> and <context:exclude-filter> sub-nodes support multiple types of filter Expressions: Component assembly <context:component-scan> The Autowiredannotationbeanpostprocessor element also automatically registers the instance, which can automatically assemble a @Autowired and @Resource, @InjectThe properties of the annotation.

Automatically assemble beans using @Autowired

• Automatic assembly of @Autowired annotationshas a compatible typeThe single Bean attribute –constructors, normal fields (even non-public), all methods with parameters can be applied @authwired annotations–by default, all properties that use @Authwired annotations need to be set. When Spring cannot find a matching Bean assembly property, an exception is thrown. If a property is allowed to not be set, the required property of the @Authwired annotation can be set to False– By default, automatic assembly by type will not work when there are multiple type-compatible beans in the IOC container. At this point you can@QualifierThe name of the Bean is provided in the note.Spring allows the parameter callout of a method @Qualifiter the name of the injected Bean specified– @Authwired annotations can also be applied toArray Typeattribute, Spring will automatically assemble all matching beans. – @Authwired annotations can also be applied toCollection Properties, Spring reads the type information for the collection, and then automatically assembles all the beans that are compatible with it. – @Authwired annotations are used in java.util.Map, if the map has a key value of String, Spring automatically assembles a bean that is compatible with the map value type, at which point the bean's name is used as the key value

Automatically assemble beans using @Resource or @Inject

Spring also supports @Resource and @Inject annotations, which are similar to the functions of @Autowired annotations ·@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 bean's name• @Inject and @Autowired annotations are beans injected by type matching, but without reqired properties ·suggested use of @Autowired annotationsGeneric Dependency Injection Spring 4.x a reference to a member variable of a generic type that can inject subclasses into subclasses consolidates multiple configuration files

Spring allows the integration of profiles by introducing multiple profiles into a file through <import>. This allows you to only specify the merged configuration file when you start the Spring container.

The resource property of the import element supports Spring's standard path resource

Spring Bean Configuration 2

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.