Spring configuration file and spring

Source: Internet
Author: User

Spring configuration file and spring
Spring configuration file OverviewIntroduction

Spring configuration files are "drawings" used to guide Spring factories in Bean generation, dependency injection, and Bean sample distribution. They are XML documents of one or more bricks, j2EE programmers must learn to flexibly apply this "Drawing" to accurately express their "build intention ".

Sample Spring configuration file

General Structure of the Spring configuration file

Spring container high-level view

Basic conditions for Spring container startup:

Spring framework package

Bean configuration information

Bean metadata

LBean implementation class

LBean attributes

For example, the user name and password of the Data Source

LBean dependency

Spring completes Bean Assembly Based on dependency Configuration

LBean behavior Configuration

For example, the callback function of the lifecycle range and Lifecycle Process

LBean Creation Method

Indicates whether the Bean is created through the constructor or factory method.

Bean implementation class

 

XML-Based Configuration

Spring configuration files are in XML format. Spring1.0 adopts the DTD format and Spring2.0 and later adopts the Schema format. The latter gives different types of configurations their own namespace, the configuration file is more extensible.

XML Analysis

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3      xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 9 <bean id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>10 <aop:config>11 <aop:pointcut expression="execution(* com.sale.service.*.*(..))" id="mycut"/>12 </aop:config>13 </beans>

Xmlns = "http://www.springframework.org/schema/beans": represents the default life space for Spring Bean

Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance": represents the xsi standard namespace, used to specify the schema file for the custom namespace

Xmlns: aop = "http://www.springframework.org/schema/aop": represents a custom namespace, 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>

<Aop: pointcut expression = "execution (* com. sale. service. *. * (..)" id = "mycut"/>

</Aop: config>: configuration of the aop namespace

Usage of Schema files

The configuration Schema file segment of Spring3.0 is in each module class package. If the module has the corresponding Schema file, you can find a config directory in the module class package, the Schema file is used in the directory as follows for 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 Bean

2. Spring-aop-3.0.xsd: AOP Configuration

3. Spring-tx-3.0.xsd: Schema of declarative transaction Configuration

4. Spring-mvc-3.0.xsd: 3.0 new

5. Spring-utils-3.0.xsd: simplifies some complex standard configurations

6. Spring-jee-3.0.xsd: to simplify the configuration of ejb and jndi functions in jee

7. Spring-jdbc-3.0.xsd: is new in 3.0, configure 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, integrated with Dynamic Language Definition

10. Spring-oxm-3.0.xsd: Configure object xml ing 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

Spring Bean name

Each Bean can have one or more IDs. We convert the first id into an "identifier", and the other IDs are called id aliases. These IDS must be unique in the IoC container.

Bean id naming method

Fully Qualified class name configured, unique

<bean class="com.sale.entity.SaleProduct" ></bean>

Unique id

<bean  id="saleProduct"class="com.sale.entity.SaleProduct" ></bean>

Name, unique

<bean name="saleProduct" class="com.sale.entity.SaleProduct" ></bean>

Id and name, unique

<beanid="saleProduct"name="saleProduct"class="com.sale.entity.SaleProduct" ></bean>

Multiple Names, unique

<bean name="bean1;alias1;alias2" class="com.sale.entity.SaleProduct" ></bean>

Multiple IDs, unique

<bean id="bean1;alias1;alias2" class="com.sale.entity.SaleProduct" ></bean>

Alias, unique

<bean id="saleProduct" class="com.sale.entity.SaleProduct" ></bean><alias name="saleProduct" alias="alias1"/>
Bean id naming conventions

1,Follow xml Naming rules

2,Composed of letters, numbers, and underscores

3,The first letter of the first word is in lower case, and the first letter is in upper case from the second one.

Instantiation of Spring Bean

How does Spring IoC container instantiate Bean? Traditional applications can instantiate beans through new and reflection methods. 2. The Spring IoC container uses the reflection mechanism to create the Bean according to the configuration metadata defined by the Bean.

Spring IoC container creation Bean example Method

Instantiate Bean using the constructor

Default Construction

<bean id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>

There must be a construction without Parameters

Construction with Parameters

<Bean id = "saleProduct" class = "com. sale. entity. saleProduct "> <constructor-arg name =" prodName "value =" Haha "> </constructor-arg> </bean>

Parameters must be constructed.

Instantiate Bean using a static Factory

Required class attribute. The factory-method attribute specifies the method for instantiating the Bean, and the method parameter can be specified using the static factory method. The Spring IoC container will call the method specified by this attribute to obtain the Bean.

<bean factory-method="newInstance" id="saleProduct" class="com.sale.entity.SaleProduct" ><constructor-arg index="0" value="Hello" ></constructor-arg></bean>

Instantiate Bean using the instance factory Method

The class attribute cannot be specified. factory-bean is used to specify the factory Bean, factory-method is used to specify the method for instantiating Bean, and the parameter can also be specified using the instance factory method.

<! -- Define the instance factory Bean --> <bean id = "saleProduct1" class = "com. sale. entity. SaleProduct"> </bean> <! -- Use the instance factory Bean --> <bean id = "saleProduct2" factory-bean = "saleProduct1"> <constructor-arg index = "0" value = "Hello"> </constructor -arg> </bean>

Scope of Spring Bean

The scope mentioned in Spring Bean is "scope" in the configuration file ". Early Object-Oriented Programming generally refers to the visible range between objects or variables. In the Spring container, the Bean object created by the Spring container is the request range relative to other Bean objects.

Scope type of Spring Bean

Singleton

There is only one Bean instance in the Spring IoC container, and the Bean exists in the single-profit mode. The single-instance mode is one of the most important setting modes, which is exceeded in Spring, we can use the singleton mode for non-thread-safe objects (generally in the DAO Layer)


<bean scope="singleton" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>

Prototype

Every time a Bean is called from the container, a brand new instance is returned, that is, each call to getBea () is equivalent to executing the new Bean () operation. By default, the Spring container does not instantiate the propotype Bean at startup.

 

 

<bean scope="prototype" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>

When using Spring's WebApplicationConext, you can also use another three Bean scopes, namely request, session, and globleSession. When using Bean scopes related to the Web application environment, some additional configurations must be made in the Web container.

Web container configuration for earlier versions:

<filter> <filter-name>requestContextFilter</filter-name> <filter-class>org.springframework.web.RequestConextFilter</filter-class></filter><filter-mapping> <filter-name>requestContextFilter</filter-name> <servlet-name>/*</servlet-name></filter-mapping>

Advanced Web Container Configuration:

<listener> <listener-class>  org.springframework.web.context.request.RequestConextLinstener </listener-class></listener>

Request

Spring creates a new instance when an http request is initiated.

<bean scope="request" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>

Session

Current session

<bean scope="Session" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>

Global session

HttpSession

<bean scope="Global session" id="saleProduct" class="com.sale.entity.SaleProduct" ></bean>
Custom Scope

In spring 2.0, Spring's Bean scope mechanism can be expanded, which means that not only the predefined Bean scope provided by Spring can be used, but also the scope of its own can be defined, even restart to define the existing scope (do not advocate this and do not cover the built-in sinleton and prototype scopes)

Implement custom Scope class:

Org.springframework.bean.factory.config.scope

Register the custom Scope class:

ConfigurableBeanFactory.registerScope(String scopeName,Scope scope)

Use custom Scope:

 

Scope customScope = new ThreadScope();beanFactory.registerScope(“thread”,customScope);<bean id=”***” class=”***” scope=”scopeName”>

 

For original works, infringement is required!
Email: woo0nise@gamail.com
QQ group: 193306983

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.