Analysis of spring configuration file
Overview of spring configuration files
brief introduction
The spring configuration file is a "drawing" used to guide the spring factory in bean generation, dependency injection and bean sample distribution. It is an XML document of one or more standard bricks. J2EE programmers must learn to flexibly apply this "drawing" and accurately express their "generation intention".
Examples of spring configuration files
General structure of spring configuration files
Spring container high level view
Basic conditions for spring container startup:
Spring framework class package
Bean configuration information
Metadata information of bean
Implementation class of bean
Attribute information of bean
For example, the user name and password of the data source
Bean dependencies
Spring completes the assembly between beans according to the dependency configuration
Bean behavior configuration
For example: life cycle range, callback function of each process in life cycle
How to create a bean
Indicates whether the bean is created through a constructor or a factory method
Implementation class of bean
XML based configuration
Spring's configuration file is based on XML format, spring 1 0 is configured in DTD format, spring 2 After 0, the format of schema is used. The latter allows different types of configurations to have their own namespace, which makes the configuration file more extensible.
XML analysis
xmlns=" http://www.springframework.org/schema/beans ": indicates the default life space, which is used for spring bean definition
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance ": represents the XSI standard namespace and is used to specify the schema file of the custom namespace
xmlns:aop=" http://www.springframework.org/schema/aop ": represents a custom namespace, and AOP represents the abbreviation of the namespace
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">: used to specify a specific schema file for each namespace
< bean id = "saleproduct" class = "com. Sale. Entity. Saleproduct" > < / bean >: the configuration in the default namespace
< / AOP: config >: configure the AOP namespace
Purpose of schema file
Spring3. The configuration schema file of 0 is divided into each module class package. If the module has a corresponding schema file, you can find a config directory in the module class package, and the schema file is in this directory. The following are the purposes of these schema files:
Example description: spring-aop-3.0 xsd
Namespace: http://www.springframework.org/schema/aop
Schema file: http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
1. spring-beans. XSD: used to configure beans
2. Spring-aop-3.0. XSD: AOP configuration
3. Spring-tx-3.0. XSD: schema for declarative transaction configuration
4. Spring-mvc-3.0. XSD: new in 3.0
5. Spring-utils-3.0. XSD: simplifies some complex standard configurations
6. Spring-jee-3.0. XSD: to simplify the configuration of EJB, JNDI and other functions in Jee
7. Spring-jdbc-3.0. XSD: it is new in 3.0 and configures the schema provided by the spring internal database
8. Spring-jms-3.0. XSD: JMS configuration
9. Spring-lang-3.0. XSD: added support for dynamic language, integrating the definition of dynamic language
10. Spring-oxm-3.0. XSD: mapping configuration object XML to schema
11. Spring-task-3.0. XSD: schema of task scheduling
12. Spring-tool-3.0. XSD: schema defined by integrated spring useful tools
Naming of spring beans
Each bean can have one or more IDs. We call the first ID "identifier", and the other IDS are called ID aliases. These IDS must be unique in the IOC container.
How bean ID is named
Configure fully qualified class name, unique
Specify ID, unique
Specify name, unique
Specify ID and name, unique
Specify multiple names, unique
Specify multiple IDS, unique
Specify alias, unique
Naming convention for bean ID
1. Follow XML naming conventions
2. It consists of letters, numbers and underscores
3. Hump style, the first word is lowercase, and the first letter is capitalized from the second word
Instantiation of spring bean
How does the spring IOC container instantiate beans? Traditional applications can instantiate beans through new and reflection. Second, the spring IOC container needs to use reflection mechanism to create beans according to the configuration metadata defined by beans.
How spring IOC container creates bean examples
Instantiate a bean using a constructor
Default construction
A parameterless construct must exist
Parametric structure
The structure of parameters must be stored
Instantiate beans using static factories
The required class attribute. The factory method attribute specifies the method of instantiating the bean, and the static factory method also allows you to specify method parameters. The spring IOC container will call the method specified by this attribute to obtain the bean
Instantiate a bean using the instance factory method
Class attribute cannot be specified, factory bean specifies factory bean, factory method specifies method of instantiating bean, and parameter can also be specified by using instance factory method
Scope of spring bean
The scope mentioned in spring bean is "scope" in the configuration file. In early object-oriented programming, it generally refers to the visible range between objects or variables. In the spring container, it refers to the request range of the bean object it creates relative to other bean objects.
Scope type of spring bean
Singleton
There is only one bean instance in the spring IOC container. Beans exist in a simple way. The single instance mode is one of the most important setting modes. It goes beyond this in spring. You can adopt the single instance mode for those non thread safe objects (generally used in the Dao layer)
prototype
Every time you call Bean from the container, you return a completely new instance, which is equivalent to executing new Bean () when you call getBea () each time. By default, the spring container does not instantiate the bean of the prototype at startup.
When using spring's webapplicationconext, you can also use three other bean scopes, namely request, session and globlesession. When using the bean scope related to the web application environment, some additional configuration must be made in the web container
Low version web container configuration:
Advanced web container configuration:
request
When an HTTP request is initiated, spring creates a new instance
Session
Current session
Global session
Httpsession session
Custom scope
In spring 2.0, spring's bean scope mechanism can be extended, which means that you can not only use the predefined bean scope provided by spring, but also define your own scope, or even restart and define the existing scope (this is not advocated, and the built-in singleton and prototype scopes cannot be overridden)
Implement the custom scope class:
Org. springframework. bean. factory. config. scope
To register a custom scope class:
Configurablebeanfactory. registerScope(String scopeName,Scope scope)
Using a custom scope:
The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!