This article brings you an introduction to the simplified usage of the Spring framework. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
As a Spring framework, its main function is to manage a bunch of classes that make the App (application) function. These classes, which are the cornerstone and backbone of the entire App, are called beans.
To manage beans, that is, a bunch of classes that perform business functions, you cannot directly new them, which lacks unified scheduling. Therefore, Spring uses .xml configuration files as a medium and IoC (Inversion of Control) as a tool to transfer these beans to the Spring container for unified management.
Based on this, to throw a bean to the container, at least two parts are required:
The definition of the class corresponding to the bean
Indirectly controlled .xml configuration file
The reason why two parts are needed is easy to understand. First, you must have a definition of the bean itself. Next, you have to tell the Spring container how to accept the bean. This is explained by the .xml file.
For example, the bean we want to manage is called HelloWorld, then its two parts are: applicationContext-src.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans> <bean> <property></property> </bean> </beans>
and HelloWorld.java:
public class HelloWorld { private String message; public void setMessage(String message) { this.message = message; } public void getMessage() { System.out.println("Your Message : " + message); } }
With these two parts, the Spring container can correctly receive the bean named HelloWorld.
Now, if you want to use this bean, of course you cannot touch the HelloWorld bean directly. Instead, you need to get the bean through the Spring container that manages its agent, and then use this bean to serve yourself.
For example, the class named MainApp.java now needs to use the service of the HelloWorld bean. We can do this:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-src.xml"); HelloWorld obj = (HelloWorld) context.getBean("helloWorld"); obj.getMessage(); } }
There are two parts here:
First, get the Spring container according to the location of the configuration .xml file, which is the Context here. It can be understood as the most famous spokesperson among several Spring containers.
After having this spokesperson, you can naturally ask the spokesperson for the required beans HelloWorld, so you can get the required beans through the context.getBean() method.
After you get the bean, you can use it directly.
It can be seen that the starting point of the Spring framework is intuitive. It is to act as a proxy for a bunch of functional classes (beans) and unify these beans into their own containers for management. Any other class that needs to use the bean must obtain it through its agent.
A question worth discussing is that the above is quite intuitive and easy to understand, but why are there not many declarations about ApplicationContext in general Java Web development?
The reason is that in normal J2EE development, the declaration of Spring Context is not completed directly in the user code, but is configured in web.xml:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
Here with the help of ContextLoaderListener registers ApplicationContext into our Web App.
Another more important consideration is:
There should be no direct use of ApplicationContext in your code, but the same use of configuration files and IoC to use Context.The above is the detailed content of Introduction to the simplified usage of Spring framework. For more information, please follow other related articles on the PHP Chinese website!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools