Note: This article from the network or the teacher's summary information, I only for excerpts, want to know more relevant information, you can pay attention to:
http://blog.csdn.net/pu_xubo565599455
What is the Spring framework?
Spring is an open source framework, an integrated framework that is created to address the complexities of enterprise application development. One of the main advantages of the framework is its layered architecture, which allows you to choose which components to use while providing an integrated framework for Java EE application development. Until 03, since the spring framework was released, the Pring framework has evolved into a very important framework in Java EE development. Although there are still companies that use their own way to deal with the problem of coupling between the code, the way they are used is still the basis of the spring framework, namely: Factory mode and service locator mode.
Spring is a lightweight solution for enterprise application development, including: core mechanisms based on dependency Injection , declarative transactions based on AOP (Lenovo programming transactions), integration of multiple persistence layer technologies, integration of many excellent web MVC frameworks, etc. Spring is a solution to every layer of Java EE application, not just a layer. Spring runs through the presentation layer, the business layer, the persistence layer, but spring does not want to replace the existing frameworks, but rather integrates them seamlessly with a high degree of exploitation.
Spring Framework Benefits?
1, low-intrusive design, code pollution is very low.
2. The spring container reduces the complexity of business object substitution and improves decoupling between components.
3. Spring's AOP support allows for centralized processing of some common tasks such as security, transactions, logs, and so on, providing better reuse.
4. Spring's ORM and DAO provide good integration with the third-party persistence framework and simplify the underlying database access.
5. Spring's high level of exploitation does not force the application to rely entirely on spring, and the developer is free to select part or all of the spring framework.
Spring--ioc
The IOC is inversion of control, which controls inversion or is called Dependency injection . IoC is not a technology, but an idea, an important object-oriented programming law that can guide us in designing loosely coupled, better programs. At work, the caller of the Java instance creates the callee's Java instance through the New keyword, the program is highly coupled, inefficient, and the real application is rarely used in this way, which is generally a beginner's favorite. With the IOC container, the control of creating and locating dependent objects is given to the container, which is injected by the container, so that the object is loosely coupled with the object, which facilitates testing, facilitates functional reuse, and, more importantly, makes the entire architecture of the program very flexible. Dependency injection is by far the best decoupling method, and dependency injection lets spring beans be organized together in configuration files, rather than being coupled in a hard-coded fashion.
Dependency Injection usually has the following two ways:
Set value injection: The IOC container uses the setter method of the property to inject the dependent instance.
Construct injection: The IOC container uses constructors to inject dependent instances.
Set Value injection :
SetPoint injection refers to an IOC container that uses a setter method of attributes to inject a dependent instance. This injection method is simple and intuitive, so it is used extensively in spring's dependency injection.
Exception:
Note: Spring recommends interface-oriented programming, which allows for better decoupling of specifications and implementations, and for a Java EE application, whether it is a DAO component or a business logic component, should first define an interface that defines the functionality that the component should implement, But the implementation of the feature is provided by its implementation class.
We define 2 interfaces first, such as:
Then, implement their implementation classes separately, such as:
Finally, we configured 2 JavaBean instances in the Applicationcontext.xml <beans></beans>, and configured their relationship management. Such as:
In a configuration file, a spring configuration bean instance typically specifies 2 properties:
ID: Specifies the bean's unique identity, and the program accesses the bean instance through the ID property value.
Class: Specifies the implementation class for the bean, where the interface is no longer available, the implementation class must be used, and the spring container reads the property value using the XML parser and uses reflection to create an instance of the implementation class.
From the example above, it can be analyzed that dependency injection is a coupling between configuration file management bean instances, and the coupling between bean instances is separated from the code hierarchy, and Dependency injection is an excellent decoupling method.
3 basic points of the SPRINGIOC container are analyzed:
1. The components of the application are interface-oriented programming. Interface-oriented programming can prompt the coupling between the components to the interface level, which facilitates the later development of the project.
2. The components of the application are no longer actively generated by the program, but are generated and initialized by the spring container.
3, Spring uses a configuration file, or annotation to manage the bean's implementation class, dependencies, and the spring container creates instances based on the configuration file, leverages reflection, and injects dependencies into them.
Construction Injection:
As we have described earlier, the way that the setter method injects dependencies into the target bean is called SetPoint injection: There is also an injection method that, when constructing an instance, has completed the initialization of the dependency. This method of using constructors to set dependencies is called construct injection.
Import Com.lovo.beans.CardInfoT;
Import Com.lovo.dao.ICardDao;
Import Com.lovo.service.ICardService;
public class Cardserviceimpl implements Icardservice {
Private Icarddao Carddaoimpl;
The default constructor, if you want to set a parameter construct, you need to display the Execute parameterless construct first
Public Cardserviceimpl () {
}
/**
* Construct the constructor with parameters required for injection
* @param Carddao
*/
Public Cardserviceimpl (Icarddao Carddao) {
This.carddaoimpl = Carddao;
}
public void Savecard (Cardinfot card) {
TODO auto-generated Method Stub
Carddaoimpl.savecard (card);
}
}
Add the configuration in Applicationcontext.xml to:
<!--Create instantiated objects from the spring container and manage relationships between objects--
<bean id="Carddaoimpl" class="Com.lovo.dao.impl.CardDaoImpl"></bean>
<!--spring construction Injection --
<bean id="Cardserviceimpl" class="Com.lovo.service.impl.CardServiceImpl">
<!--inject Carddaoimpl instance into the Cardserviceimpl instance using construct injection
<constructor-arg ref="Carddaoimpl"></constructor-arg>
</bean>
comparison of two injection methods:
Spring supports two kinds of dependency injection methods: Set injection and construct injection. There are no absolute good or bad points between the two injections, but the adaptation of the scene is different.
In contrast, setting injection has the following advantages:
1, with the traditional javabean more familiar with the writing, program developers more easily understand, accept, through setter way to set the dependency relationship appears more intuitive, natural.
2, for complex dependencies, if the use of construction injection, will cause the constructor is too bloated, difficult to read. When spring creates a bean instance, it needs to instantiate all of its dependent instances at the same time, resulting in performance degradation. Instead, you can avoid using set injection.
3, especially when some properties are optional, multi-parameter constructors are more cumbersome.
Of course, the construction injection is not absolutely inferior to the set value injection, some specific occasions, the construction is more suitable. Construction also has the following advantages:
1. Construction injection can determine the injection order of dependency in the constructor, and priority injection of priority depends on the injection of datasource. With construction injection, you can clearly determine the injection order in your code.
2. Construction injection is more useful for beans that do not need to be changed by dependency relationships. Because there is no setter method, all dependencies are set in the constructor, so there is no need to worry about subsequent code breaking the dependency.
3. Dependencies can only be set in the constructor, only the creator of the component can change the dependencies of the components. For the caller of the component, the dependencies inside the component are completely transparent and more consistent with the high cohesion principle.
It is suggested that the injection strategy should be injected as the main value and the structure injected as a supplement. As far as possible, it is necessary to use the construction injection for the non-change of the dependency relationship, while the other dependency injection will consider the value injection.
Spring Container
Spring provides two core interfaces: Beanfactory and ApplicationContext, where ApplicationContext is the sub-interface of Beanfactory. They can all represent the spring container, the spring container is the factory that generates the bean instance, and the bean in the container is managed. Beans are the basic unit of spring management, and in spring-based Java EE applications, all components are treated as beans, including data sources, Hibernate sessionfactory, transaction managers, and so on.
beanfactory:
The most basic interface of the spring container is beanfactory, which is responsible for configuring, creating, and managing the Bean, which has a sub-interface; Appcationcontext, also known as the spring context. The spring container is also responsible for managing the dependency between beans and beans.
The caller simply uses the Getbean () method to obtain a reference to the specified bean without caring about the bean's instantiation process.
Beanfactory has a common implementation class: The Org.springframework.beans.factory.xml.XmlBeanFactory class.
ApplicationContext is a sub-interface of Beanfactory, which is more convenient for most Java EE applications to use as a spring container. Its common implementation classes are: Filesystemxmlapplicationcontext, Classpathxmlapplicationcontext, and Annotataionconfigapplicationcontext. If you use a spring container in a web app, there are typically Xmlwebapplicationcontext, annotatioinconfigwebapplicationcontext, and two implementation classes.
Most Java EE applications can automatically load ApplicationContext instances when starting a web app, and the bean that accepts spring management does not need to know the existence of ApplicationContext, The same can be done with ApplicationContext management. For standalone applications, you can also instantiate beanfactory by using the following methods.
1.
Search for the Beans.xml file under the current file path to create the resource object
Inputstreamresource ISR = new Filesystemresource ("Beans.xml");
Create a Beanfactory object with the resource object as a parameter
Xmlbeanfactory factory = new Xmlbeanfactory (ISR);
or by:
2.
Search for the class load path to create the resource object in the Beans.xml file under the class load path
Classpathresource res = new Classpathresource ("Beans.xml");
Create a Beanfactory object with the resource object as a parameter
Xmlbeanfactory factory = new Xmlbeanfactory (res);
If you need to load multiple XML configuration files at the same time, you can use the following:
Search the classpath path to create service.xml files under classpath path Applicationcontext.xml, Bean.xml, ApplicationContext
ApplicationContext appContext = new Classpathxmlapplicaation (new string["Beans.xml", "Service.xml"]);
ApplicationContext:
Most of the time, we don't use the beanfactory instance as a spring container, we use the ApplicationContext instance as a container, so we also call the Spring container the spring context. ApplicationContext is a sub-interface of the Beanfactory interface, which enhances the functionality of the beanfactory.
ApplicationContext allows the container to be manipulated declaratively without having to create it manually. You can use support classes such as Contextloader to automatically create ApplicationContext when your Web app starts. Currently, you can also create ApplicationContext programmatically.
Beanfactory and ApplicationContext instantiate the bean in the container in a different time, which is created when the application requires a bean instance, and the latter initializes all the beans in the container when the container creates the ApplicationContext instance.
Because ApplicationContext is used as a spring container, creating a container creates all the singleton scope beans in the container at the same time, so more overhead may be required. Once created, however, the response is faster after the application, so for normal Java EE applications it is recommended to use ApplicationContext as the spring container.
In fact, Spring allows the bean singleton function to specify lazy-init= "true", which alters the default behavior of the singleton Bean instance, forcing the bean instance to be pre-initialized. The bean will not be pre-instantiated as ApplicationContext starts.
Java----Spring Framework