This article has participated in the weekend learning program, click the link to see more details: juejin.cn/post/696572…
Ten minutes on the stage, ten years off the stage, accumulated.
preface
Learning Java development, you can’t escape learning the Spring framework. So this article will do a learning summary of the Spring framework, if there is not perfect or inappropriate place, also ask you to correct.
Introduction to the
The Spring Framework was developed by Rod Johnson, who released the first version of the Spring framework in 2004. Spring is a framework extracted from actual development, so it does a lot of the common steps of development, leaving developers with only the parts that are relevant to specific applications, making enterprise application development much more efficient.
advantages
1. Adopt the concept of low intrusive design to reduce the code pollution to a very low level; 2. Independent of various application servers and based on the Spring framework, applications can truly realize the promise of Write Once and Run Anywhere; 3. The IoC container in the Spring framework reduces the complexity of business object replacement and improves the decoupling between components; 4. The Spring framework’s AOP support allows for centralized management of common tasks for better reuse; 5. The Spring framework is highly open and does not force applications to rely entirely on the framework, leaving developers free to choose part or all of the Spring framework.
The core mechanism
Manage beans
The ApplicationContext interface is the most commonly used interface in the Spring container. The ApplicationContext interface has two implementation classes: FileSystemXmlApplicationContext and ClassPathXmlApplicationContext.
- FileSystemXmlApplicationContext: from the file system of the relative or absolute paths down search configuration files, and according to the configuration file to create the Spring container.
- ClassPathXmlApplicationContext: from class loading path search configuration files, and according to the configuration file to create the Spring container.
Example:
public class BeanDemo{ public static void main(String args[]) throws Exception{ ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Children c = ctx.getBean("Children", Children.class); c.say(); }}Copy the code
2. Eclipse uses Spring
In an IDE such as Eclipse, you can build your own User Library and put all Spring jars in it. You can also put jars directly in your project’s /WEB-INF/lib directory, but using the User Library, The Jar files referenced by the User Library need to be distributed with the application when the project is published. That is, the Jar files used by the User Library need to be copied to the/web-INF /lib directory. This is because for a WEB application, Eclipse does not copy the Jar files of the user library to /WEB-INF/lib when deploying Web applications. You need to manually copy the Jar files.
Dependency injection
The Spring framework has two core functions: ① The Spring container manages the dependencies between beans in the container. The Spring framework uses a “dependency injection” approach to manage the dependencies between beans. ② The Spring container, as the superfactory, is responsible for creating and managing all the Java objects, which are called beans.
4. How the Bean is created
There are three ways to create beans: by creating a Bean instance through a constructor, by creating a Bean using a static factory method, or by calling an instance factory method.
conclusion
Above is all content of this article, the content of this article only some basic knowledge of the Spring framework to do simple sums up, but the Spring framework still has a lot of content, just because of the space without comprehensive summary list one by one, the reader with me and give me some feedbacks, as Java study of new students, still please various bosses more guidance, Talk to each other!