One of the Spring learning notes----XML-based spring IOC configuration

Source: Internet
Author: User

1. In the spring configuration file, if a property is directly assigned, you can use the <value> element, spring is responsible for converting the value to the type specified by the property, or you can use the value attribute to assign a value directly on the properties element ;

2. Constructor injection, you should use the <constructor-arg> element to assign a value, the element has three properties, you can specify to distinguish the constructor parameters of the assignment: type, index, or name, but when you use the Name property, You should place @constructorproperties annotation to the constructor to explicitly specify the constructor parameter name;

3. In spring, for a bean with the same name, the latter one overwrites the previous one;

4. The name of the specified bean can be specified by the id attribute of the <bean>, or by the Name property, and the id attribute can specify more than one name, and the name attribute may be separated by commas;

5. If the property is injected, we can add xmlns:p= "http://www.springframework.org/schema/p" to the schema, then we can add p directly to the bean element: Propertyname= "value" to be assigned, in the same vein, by constructor to inject, you must add xmlns:c= "http://www.springframework.org/schema/c" schema, and add C:argumentname= "value" to the bean element to assign the value;

6. We are loading a spring configuration file, we should use the Genericxmlapplicationcontext class, the default class from the Classpath to load the configuration file A constructor of the Genericxmlapplicationcontext class accepts multiple configuration files as an array of strings;

7. To refer to other beans, you can use the <ref bean= "bean name"/> element, the bean attribute in the ref element can refer to the bean in any other configuration file, and if you only want to reference the bean in the same XML file, we should use the <ref local= "bean name"/> we can also use the ref attribute on the property element or the Constructor-arg element to refer to other beans; For schema, we can use P: propertyname-ref= "Bean name" to refer to other beans;

8. The Autowire property of the <bean/> element, which is used by default only for the Autowire of all properties of this bean, contains 3 possible values:

ByName: A bean of the same name is found according to the Bean's attribute name;

Bytype: Finds the same type of bean based on the bean's attribute type;

Constructor: The Autowire for the Bean's constructor, which first finds the same type of bean based on the parameter type of the constructor, and then determines the best constructor;

When using Autowire Bytype, if there are multiple beans of the same type, you throw an exception, then you can add primary= "true" to the <bean> element of a bean, the bean is Autowire Bytype will be used as a priority;

For Autowire Bean Properties, spring sets a null value if spring cannot find a response bean, and may get a NullPointerException exception when the user uses the property So we can use the following two ways to circumvent:

A. Create a setter method for a property and use @required annotation;

b. Adding @autowired annotation to a property, implicitly including @required;

For some beans, we don't want it to be autowire by other beans, we can add autowire-candidate= "false" to this <bean> element;

9. We can use the <import> tag to refer to other configuration files in one configuration file;

Spring provides 3 elements to define collections:<list>, <set>, <map>;

For collections <list> and <set>, the item can be:<value>, <ref>, <bean>, <idref>, <null/>, and so on;

For <map>, where the item must be a key in the <entry> element,<entry> element must be placed under the <key> element, and value is placed directly below the <entry> element The value of either key or value can be any of the;<entry> in the <value>, <ref>, <bean>, <idref>, <null/> elements Elements also contain key, value, Key-ref, value-ref and other attributes;

Spring provides the <props> element for the Java.util.Properties collection, and each item in the element must be a <prop> element that contains the key attribute;

11. For the above mentioned elements used to create the collection, they cannot be used to create a separate bean, if you want to create a separate bean, we have to use the Util schema, we need to add xmlns:util= "http:// Www.springframework.org/schema/util "and add Http://www.springframework.org/schema/util/HTTP to Xsi:schemalocation Www.springframework.org/schema/util/spring-util-3.2.xsd; then we can use <util:list>, <util:set>, <util: Map> to create a collection bean, which all three elements provide the List-class, Set-class, and Map-class properties to specify the type of collection to create;

The <bean> element has a scope property that specifies the scope of the bean instance that is created, and the value may be:

Singleton: There is only one instance of this spring container;

Prototype: Each request will create a new bean instance;

Request: A new instance is created each time it is requested and is valid in the Web application;

Session: Each session creates a new instance that is valid in the Web application;

Global-session: Create a new instance for each global HTTP session, valid in Web application;

13. To load a property file into the spring container and make it available to other beans; Spring provides the Propertysourcesplaceholderconfigurer class, It is used to specify the location of the property file;

Then we can use the ${key:default_value} method in the configuration file to reference the property in the property file;

If we are going to read the contents of a file, Spring provides a resource type, you simply assign a string to a property of that type in the configuration file, and the spring container automatically converts it to an resource instance. This string can be based on the different location of the file to specify a different prefix, such as file system files, starting with file, the files in the Classpath start with classpath, if the real classpath in a special package, You can use this form to specify the Resource:classpath:com/apress/springrecipes/shop/banner.txt;

14. In order for spring to support multi-lingual, spring provides a way to read the message through a specific local go to properties file, which must be placed inside the properties file, such that the file is called resource Bundle, these files should be placed in the root directory of the classpath, and the file name must abide by the messages_<language_code>_<country_code>.properties such constraints, We can create a reloadablersourcebundlemessagesource bean to read the message, but because we have to let application context know that this is a bean that reads the message, So the name of the bean must be messagesource, the bean's Basenames attribute resource the file name of the bundle file (not including the language Code and Country Code section);

In spring context, if a bean needs to use these message, it needs to inject the messagesource bean into that bean;

The life cycle of beans in Spring container:

    • Create a bean instance from a constructor or factory method;
    • Sets the attribute value of the bean;
    • Invokes the initialization method of the bean;
    • Using Beans;
    • When the container is closed, the destructor method is called;

In the bean definition, both the initialization method and the destructor method are specified by the Init-method and detory-method of the <bean/> element;

16. In general, all beans in spring context are initialized as soon as spring container is started, and in order to avoid the creation of some beans that are consuming time and resources, we can do so in a <bean> The lazy-init= "true" attribute is set on the element.

17. At some point, some beans will have to rely on other beans to be created before they can be created (the relationship between the two may not be referenced); we can use the Depends-on property of the <bean/> element, which can specify one or more bean names, separated by commas;

18. We may want to perform some tasks before or after the initialization method of each bean in spring container is called, such as doing some property validation, and we can create a class and inherit it from the Beanpostprocessor interface;

and implements the Postprocessbeforeinitialization method and the Postprocessafterinitialization method in the interface, so that the bean's life cycle becomes:

    • Create a bean instance from a constructor or factory method;
    • Sets the attribute value of the bean;
    • Passing the bean instance to the Postprocessbeforeinitialization method of the Beanpostprocessor implementation class;
    • Invokes the initialization method of the bean;
    • Passing the bean instance to the Postprocessafterinitialization method of the Beanpostprocessor implementation class;
    • Using Beans;
    • When the container is closed, the destructor method is called;

The BEANPOSTPROCESSOR implementation class must be declared in the spring configuration file;

19. In addition to the spring container implementation of the initialization of the Bean instance, spring provides three other ways to create the bean:

    • static Factory method;
    • Instance factory method;
    • Spring Factorybean

If you want to use the static factory method to create a bean instance, you need to specify the type of the static class in the <bean/> node through the class attribute, specify the method name in the Factory-method property, and then pass the <constructor-arg/ > Sub-element incoming method parameters;

If you want to use the instance factory method, you need to specify the bean name of the factory method on the <bean/> node by Factory-bean, specify the method name in the Factory-method property, and then pass the < The constructor-arg/> element is passed into the method parameter;

If you want to use Factorybean to create a bean instance, you need to inherit the Abstractfactorybean abstract class, and the CreateInstance () method to create the target bean instance, In order for other beans to autowire the bean, it is necessary to implement the Getobjecttype () method to return the type of the target bean;

Each time spring container to request Factorybean, get is the target bean instance, to want Factorybean itself, need to add & symbol before the bean name;

20. If you want to give different initial values to the bean depending on the environment, you can create multiple beans with the same name, and then place them in different profiles, each with a <beans/> element designation, <beans/ The profile property of the > element describes the name of the profile, and each <beans/> element can contain multiple beans, or it can specify multiple names for its profile, separated by commas;

In order to specify which profile to load at Spring container startup, there are 3 ways you can do this:

    • Call the Getenvironment (). Setactiveprofiles ("", "") of the Genericxmlapplicationcontext class before loading the configuration file.
    • Before running, pass in the JVM parameter-dspring.profiles.active= "Xxx,xxx"
    • Pass in the spring.profiles.active initialization parameter for the servlet in the Web. xml file for the war package:
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>   Org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param>   <param-name >spring.profiles.active</param-name>   <param-value>XXX</param-value> </ Init-param></servlet>

Sometimes, in order to prevent users from forgetting to specify profiles, we can also specify Defaultprofiles:

    • Call the Getenvironment (). Setdefaultprofiles ("", "") of the Genericxmlapplicationcontext class.
    • Incoming JVM parameter-dspring.profiles.default= "Xxx,xxx"
    • Pass in the Spring.profiles.default initialization parameter for the servlet in the Web. xml file for the war package:
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>   Org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param>   <param-name >spring.profiles.default</param-name>   <param-value>winter</param-value> </ Init-param></servlet>

One of the Spring learning notes----XML-based spring IOC configuration

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.