Spring learning (a, Spring basic application)

What is Spring

Spring is a lightweight inversion of control (IoC) and the facing section (AOP) of the container frame.

The advantage of spring framework

1. Easy decoupling, simplify development: Spring is a large factory, you can create and maintain dependence Spring managed to get all the objects.

2. AOP programming support: Spring provides aspect-oriented programming, you can easily implement a program permission to intercept, operation monitoring and other functions.

3. Support declarative transactions: only through configuration management to complete the transaction without the need for manual programming.

4. easy test program: Spring for JUnit4 support, you can easily test the program by Spring comment.

The easy integration of various excellent framework: Spring does not exclude a variety of excellent open source framework, which provides a variety of excellent internal frame (struts2, hibernate, mybatis, quartz, etc.).

6. reduce the difficulty of using JavaEE API's: Spring for some API JavaEE development is very difficult to use (jdbc, javaMail, remote calls, etc.), provides packaging and greatly reduce the difficulty of using these API applications.

spring architecture

Spring Framework using a layered architecture comprising a series of functional elements, modules are divided into about 20, as shown:

 

1. Core Container (core container)

Spring's core container is the basis for other modules built by Beans, Core (core modules), Context (context module), SpEL (expression language modules).

(1) Beans module: Provides BeanFactory, is a classic factory pattern was achieved, Spring will manage objects called Bean.

(2) Core Core Module: provides basic components have the Spring framework, including IOC and DI function.

(3) Context context module: based on the core and Beans obtained based on the module, which is defined and configured to access any available media objects. ApplicationContext interface is too focus context module.

(4) SpEL modules: query and manipulate objects drawing was so powerful expression language runtime.

2. Web module

Spring Web obtained layer comprises a web, Servlet, Struts, Portlet assembly.

(1) Web module: provides integrated features have basic web development, such as: multi-file upload function, the use of Servlet listener too IOC container initialization and Web application context.

(2) Servlet module: Spring Model - View - Controller (MVC) implement web application.

(3) Struts module: contains the application have Spring support classes, have integrated classic Struts web layer.

(4) Portlet Module: provides the use of MVC implemented in a portlet environment, similar to the Web-Servlet module functions.

3. Data Access / Integration (Data Access / Integration)

Data Access / integration layer includes a JDBC, ORM, OXM, JMS and Transactions module.

(. 1) JDBC module: provides a JDBC abstraction layer, greatly reducing the operation of the database code during development.

(2) ORM module: Mapping API comprises JPA, JDO, Hibernate and iBatis integration layer provides a popular object-relational.

(3) OXM module: it provides a support object / xml mapping achieve JAXB, Castor, using XMLBeans, JiBX and XStream abstraction layer.

(4) JMS Module: refers to the Java Message Service, it contains functions for the production and consumption of information.

(5) Transactions Transaction module: support for programming and declarative transaction management to achieve special interface class, and are applicable to all POJO.

4. Other Module

(1) AOP module: Aspect-oriented programming is provided, the method allows the definition and interceptors starting point, the code separated by function, to reduce the coupling.

(2) Aspects Module: provides AspectJ integration, is a powerful and sophisticated aspect-oriented programming (AOP) framework.

(3) Instrumentation module: provides support for implementation of the class and class loader tool can be used in a specific application server.

(4) test module: _Spring_ support assembly, use or test _jUnit_ _TestNG_ frame.

Spring's IOC container

1. BeanFactory

BeanFactory is the basis of the type of container IOC, the IOC provides a complete service support. Bean BeanFactory is a management project, which is mainly responsible for initiating all kinds of Bean, and call their life cycle approach. BeanFactory interface has multiple implementations, the most common is to assemble Bean based on XML configuration file definitions. When you create a BeanFactory instance, you need to provide detailed configuration information managed by Spring container, such information usually in the form of an XML file to manage information load the configuration syntax is as follows :( This method development are rare here, as you can understand)

BeanFactory beanfactory = new XmlBeanFactory(new FileSystemResource("F:/applicationContext.xml"));

2. ApplicationContext

ApplicationContext BeanFactory is sub-interface, also known as the application context. It not only provides all the functions of BeanFactory, but also in a more framework-oriented approach enhances BeanFactory functionality. Context mainly in the context classes and packages using the hierarchical inheritance relationship, as follows:

(1) MessageSource, providing access to i18n message.

(2) access to resources, such as files and url.

(3) event delivery to achieve the bean ApplicationListener interface.

(4) loading a plurality of (hierarchical) context classes, such that each focused on a context of a particular class level, such as the application layer of the web.

Create ApplicationContext interface instance, usually using two methods, as follows:

(1) ClassPathXmlApplicationContext: class loading context information from the path defined xml file, as the context of the class definition file path resources.

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

(2) FileSystemXmlApplicationContext: xml file from the file system (the specified path) loading the context definition information.

ApplicationContext applicationContext = new FileSystemXmlApplicationContext("F:\\workspaces\\src\\applicationContext.xml");

After BeanFactory and ApplicationContext are loaded via XML bean configuration files, by contrast, ApplicationContext provides more extensions, the main advantage of lazy loading, if one does not attribute Bean injection is used BeanFactory loaded, in the first call getBean () method throws an exception, while the ApplicationContext in the initialization self-test, which will help to check whether the dependency property injection. Therefore, in the actual development, the system usually choose to use ApplicationContext, but only when there are fewer system resources before considering the use of BeanFactory.

Dependency Injection

Can be understood: If an object A object B is required to achieve a certain function, then you can say that the object A depends on B object, A Spring container when you create an object, the object will automatically need to have B A target injection A subject to this process is the dependency injection. Dependency injection effect is obtained when the object is created with the Spring framework, it depends dynamically obtained Bean component is injected into the subject.

Dependency injection the three ways

(. 1) setter injection: means for setter method using IOC container instance is dependent injection. By calling the constructor with no arguments or no-argument static factory method instantiated Bean, Bean of the setter method is called, the setter can be realized based on DI.

(2) Constructor injection: The method refers to the IOC configured to use a container instance is dependent injection. Constructor-based DI is achieved by calling the constructor with parameters, each representing a dependency.

(3) interface injection: Spring container does not support the interface injection.

 

 

 

 

 

 

 

reference:

1. SSH framework to integrate combat tutorial

Continually updated! ! !

Guess you like

Origin www.cnblogs.com/flyinghome/p/12550331.html