First, Spring introduction
1.Spring simplifies Java development
The Spring framework is an application framework that is generally semi-finished, and we can focus on the business logic without having to implement the architecture, infrastructure, and common functional components of each project on a framework basis. Therefore, learning the structure and principles of the spring framework in terms of architecture and patterns is of great help to our understanding of architecture and module level. The purpose of the Spring Framework (reference 1) is to simplify Java development, with the following main tools:
(1) Architecture decoupling: Managing Type Dependencies through Di (dependency injection), separating concerns through AOP, and reducing duplication of code.
(2) The design of a wide range of dip (dependency inversion) and ISP (interface isolation) and other principles and facade (appearance) and other modes: provide a simplified calling interface and encapsulate a number of excellent third-party components.
(3) Use annotations at the language level: Simplify application configuration with configuration files and annotation (refer to. NET Attribute).
Architecture and modules for the 2.Spring framework:
The schema of the Spring framework itself is typically loosely layered, with the outer layer referencing all the inner layers on demand, and the inner layer not referencing the outer layers. The basic components of spring are as follows:
As can be seen, the starting module only from Core/beans/aop/context Four components, later added Context-support "1.2" extension module, expression "3.0" extension Module and Beans-groovy "4.0" Extension module.
Based on the above modules, the spring is built-in and encapsulates a multitude of practical general-purpose components, the main components:
As you can see, SPRING-OXM, Spring-jdbc, and Spring-web are the cores of many module dependencies, and SPRING-OXM provides mapping support for object and XML.
Second, the basic knowledge
The 1.dip:dip (dependency inversion principle) is the core of Di (Dependency injection) (ref. 2).
(1) High-rise modules should not be dependent on low-level modules. Both should be dependent on abstraction.
(2) Abstraction should not depend on detail. The details should depend on abstraction.
The word is: the reference to the specific class is converted into a reference to its interface, the specific class only refers to the interface (reference = = Dependency, interface = = Interface or abstract class). In fact, when we invoke a specific class, we are only concerned with the API provided by it, not the implementation, and the dip is guaranteed to be decoupled by technical means during the design and refactoring phases.
2.di:di (Dependency injection) lets us not write factory code to manage interfaces and implement class mappings, object creation, and lifecycle management.
(1) Interface injection: must implement a specific interface to be able, intrusive too strong, now no one cares about and use.
(2) constructor injection: dependencies are reflected on the parameters of the constructor.
(3) Attribute injection: Dependency is reflected on the attribute.
Since the type can be registered as its own compatible type when implemented, dependency injection can replace the new instantiation object directly, so understanding and using the dependency Injection tool is better than not using or writing the factory. The dependency Injection tool will certainly be implemented as an object factory that supports different configurations and different lifecycles, but even without a set of APIs to add dependency inversion rules, it does not mean that we are replacing it with new alternatives. Just as the mapping tool can be arbitrarily mapped when implemented, but instead of replacing the assignment, it is used to deal with mappings between objects with actual correspondence, such as domain entities and view models.
(1) Dependency configuration: Dependency configuration is the basis for a dependency injection implementation. The Dependency Injection tool supports at least code configuration and file configuration. In Java, configuration can be simplified through annotation (. Net via attribute).
(2) Object factory: Returns one or more objects according to the configuration. This is the core feature.
(3) Lifecycle management: At least 4 levels of support are generally available: scopes, Singleton, threads, HTTP request scopes.
Most of the dependency injection tools, which support the dependency inversion principle, implement more functions on technical means, such as compatible conversions of types, naming dependencies, passing objects directly to the configuration, and so on.
Iii. key points of spring dependency injection
Beans in spring are pojo (. Net Poco).
The core of spring dependency injection is 3 types Beandefinition, Beanfactory, and ApplicationContext.
1.BeanFactory
Beanfactory is the core interface of spring-dependent injection, and its design mainly adopts ISP (interface isolation principle), which ensures the cohesion of single interface and ensures the simplicity of the whole system through multi-level interface inheritance. The core of our concern here is defaultlistablebeanfactory.
To see the Xmlbeanfactory code, you can see that xmlbeanfactory just loaded the beandefinition configuration through Xmlbeandefinitionreader, The Xmlbeandefinitionreader is responsible for parsing the configuration to beandefinition. Defaultlistablebeanfactory is a true implementation class that defines a beandefinitionmap list of type map<string and beandefinition> for storing dependent configurations.
2.BeanDefinition:
Beandefinition defines the configuration metadata, whether using Java code, XML, annotation, or groovy scripting, Different configuration methods are resolved to beandefinition by different beandefinitionreader.
3.ApplicationContext
The core of ApplicationContext is the implementation class defaultlistablebeanfactory that delegates the object factory functionality to Beanfactory. Note-based Annotationconfigapplicationcontext and Annotationconfigwebapplicationcontext are most commonly used today.
Iv. Spring Dependency Injection Quick Start
1. Use Java configuration instead of XML configuration
The core of the Java configuration is @configuration and @bean. Define the lifecycle using @scope, use @import when you need to introduce additional configuration files.
(1) @Configuration: Poco with @configuration annotations has become a configuration class. Equivalent to an XML configuration file.
(2) @Bean: The method that applies the @bean annotation in the configuration class becomes the configuration item. Equivalent to the bean node in XML.
Package Me.test.spring_ioc;import Org.springframework.context.annotation.AnnotationConfigApplicationContext; Import Org.springframework.context.annotation.bean;import org.springframework.context.annotation.Configuration; public class App {public static void main (string[] args) {annotationconfigapplicationcontext container = new A Nnotationconfigapplicationcontext (Appconfig.class); String message = Container.getbean (Iappservice.class). Test (); SYSTEM.OUT.PRINTLN (message); Container.close (); }} @Configurationclass AppConfig {@Bean public iappservice Iappservice () {return new Appservice (new Reposito Ry<simpleentity> ()); }}class simpleentity {private String name; Public String GetName () {return name; } public void SetName (String name) {this.name = name; }}interface Iappservice {String Test ();} Interface Irepository<t> {String Test ();} Class Appservice implements Iappservice {private IreposItory<simpleentity> _repo; Public Appservice (irepository<simpleentity> repo) {_repo = repo; @Override public String Test () {return This._repo. Test (); }}class Repository<t> implements irepository<t> {@Override public String Test () {return this.ge Tclass (). GetName (); }}
If you are a Web application, you should use Annotationconfigwebapplicationcontext to get ApplicationContext objects through webapplicationcontextutils in a JSP.
<% @page import= "SWP. Iappservice "%><% @page import=" Org.springframework.web.context.WebApplicationContext "%><% @page Import= "Org.springframework.web.context.support.WebApplicationContextUtils"%>
2. Automatic assembly based on annotation
Automatic assembly mainly uses @componentscan, @Component and @autowired.
(1) @ComponentScan: function on the configuration class, enable component scanning. Scans and registers the type labeled @component (@Controller/@Service/@Repository). @component annotations have been applied to the @Configuration.
(2) @Autowired: Automatically assembled by type. The effect of @Autowired and using @inject (JSR-330) or @resource (JSR-250) is similar. @Autowired and @inject are injected by type by default, @Resource injected by name by default.
Package Me.test.spring_ioc;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.context.annotation.annotationconfigapplicationcontext;import Org.springframework.context.annotation.bean;import Org.springframework.context.annotation.componentscan;import Org.springframework.context.annotation.configuration;import Org.springframework.stereotype.component;public Class App {public static void main (string[] args) {annotationconfigapplicationcontext container = new Annotati Onconfigapplicationcontext (Appconfig.class); String message = Container.getbean (Iappservice.class). Test (); SYSTEM.OUT.PRINTLN (message); Container.close (); }} @Configuration @componentscanclass AppConfig {/* @Bean public iappservice Iappservice () {return new Appserv Ice (new repository<simpleentity> ()); }*/}class simpleentity {private String name; Public String GetName () {return name; } public void SetName (String naMe) {this.name = name; }}interface Iappservice {String Test ();} Interface Irepository<t> {String Test ();} @Componentclass Appservice implements Iappservice {private irepository<simpleentity> _repo; @Autowired public Appservice (irepository<simpleentity> repo) {_repo = repo; @Override public String Test () {return This._repo. Test (); }} @Componentclass repository<t> implements irepository<t> {@Override public String Test () {Retu RN This.getclass (). GetName (); }}Summary:The least you should know:
(1) Beanfactory and beandefinition
(2) Defaultlistablebeanfactory and ApplicationContext
(3) Annotationconfigapplicationcontext and Annotationconfigwebapplicationcontext
(4) @Configuraton and @bean
Spring's dependency injection is configured to be object-based rather than type, and the first to support XML rather than annotations, and now there is no more convenient code configuration mechanism. Although it has now transitioned from XML to annotation, the base of object-based is still a disadvantage of many factors.
Reference1.http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/overview.html
2.https://en.wikipedia.org/wiki/dependency_inversion_principle
3.http://www.ibm.com/developerworks/cn/java/j-guice.html
4.https://www.ibm.com/developerworks/cn/webservices/ws-springjava/
5.http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-iocannt/
6.https://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/
7.http://www.ibm.com/developerworks/cn/java/j-lo-beanannotation/
8.http://www.ibm.com/developerworks/cn/java/j-guice.html
9.http://www.yiibai.com/spring/spring-dependency-checking-with-required-annotation.html
Essays in the article are dry, are to solve the actual problem, these problems I can not find a better solution on the Internet, will have you see this article essay, so if this blog content to you a little help or a bit of reference, please recommend it to help more people. If you have a better solution that offers practical possibilities, please recommend it to me.
Java Web series: Spring Dependency Injection Basics