Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to build the first Spring project

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to build the first Spring project, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Eight modules of Spring Framework

The idea of modularization is a very important idea in Spring.

The Spring framework is a hierarchical architecture, and each module can be used either individually or in conjunction with other modules.

Each "green box" corresponds to a module, with a total of 8 modules; "black package" indicates the jar package to implement this module.

Core Container, as we have just seen in the document, is the IoC container, which is the core. You can see that it depends on these four jar packages:

Beans

Core

Context

SpEL, spring express language

So here we know, if you want to use the IoC function, you need to import these four jar packages. Among them, the Core module is the core of Spring, all the functions of Spring depend on this jar package, and Core mainly implements the IoC function, so to speak, all the functions of Spring are realized by IoC.

The other modules have little to do with this article and will not be carried out here.

So when we want to build a Spring project, of course we can import all the jar packages, but can your computer stand it?. But the bigger the bag and the bigger the project, the more problems there will be, so try to choose as needed without hoarding.

Btw, there are many pictures on the Internet, but I can't find it on the latest version of reference doc. However, since it is in the old tutorials, it shows that it is in the old version of doc, so look for it in the introduction of the old version.

In the first picture of this article, Spring Framework-Documentation, we select 4.3.26 Reference Doc., and search for "Framework Modules". For specific links, please see Resources at the end of the article.

There's another way, and we'll talk about it later when we get to the contents of the jar package.

Now that you know which jar packages to import, find it.

First, the way to manually load jar packages 1. download

Download address:

If you want to ask me how to find it, just go into the 4.3.26 version of Reference Doc, and then there is a Distribution Zip Files at the beginning.

Curiosity took me to open it and found.

Found the warehouse address!

After opening it, I found that it was various versions of the jar package.

We search version 5.2.3, and it's at the bottom:

Then you can use it happily.

Dist.zip is the jar package

Docs.zip is a document

Don't worry about the rest for the time being.

After downloading, let's take a good look at this big gift bag that Spring gave us.

The remaining question above is answered here: where to find the Spring Framework framework diagram.

The answer is: download docs.zip → spring-framework-reference → images → spring-overview

Where is the jar package that we need to import into Intellij? Dist.zip → libs

Here you can see that each black box corresponds to 3 jar packages, and the one we want to import into Intellij is RELEASE.jar.

two。 Build a project without IoC

Let's new project, without the maven framework, create a new normal Java project, for example, I'll call it Spring_HelloWorld, and then use my usual class Rectangle example.

Then import the jar package corresponding to the four modules we just saw in the module diagram in External Libraries. The structure is as follows:

So you think you're done? Too young too simple ~

Let's run it:

There is an old friend: no class def found error, but I can't find this class.

We Google Maven common logging and download its jar package, and then add it to the project.

I have already added it in the picture above, so you will see a commons-logging-1.2.

Just run it again. Both of the files here have screenshots above.

So far we have manually used the set () method to set the object, so how do we use Spring IoC?

3. Spring IoC configuration file details

You also need a configuration file, but what does this file need to be configured and how to configure it?

It's all written for us on the official website:

The first paragraph is an introduction to some namespaces and their specifications.

The second paragraph is to assign a value to the property of bean.

Note here that the configuration in bean needs to be changed to the corresponding one for our project. What does id class mean here? There is also an explanation on the official website. I will briefly summarize it here:

Bean tag: tells Spring which object to create

Id: the unique identification of the object, just like everyone's ID card, which cannot be repeated.

Class: the fully qualified name of bean, that is, from package name to class name

Property: assign a value to the property, and the name of the name depends on the parameters following the set () method

In fact, you can also use constructor to assign values. The name of name depends on the parameter list. More uses of assigning values to complex data types can be found on the official website.

Of course, annotations are more commonly used at work. But there are often xml files to be used together, so you still need to understand.

My service file configuration is as follows:

4. Finally, let's take a look at how it works:

There is no direct new service, but the Spring container creates the object for us.

So how does Spring help us create objects?

ApplicationContext is the entrance to the IoC container, which is actually the entrance to the Spring program. I have just talked about its two specific implementation subclasses, and here I use the way to read data from class path.

Then the second line is to get the specific bean. There are many ways to use this, and you can see it when you use it:

Click in and find that it is defined in BeanFactory.class:

One of the more common ones is through

Id → needs cast

The type → of Bean can only be used in Singleton, otherwise you don't know which one to use.

Code example of the following figure for Id + type →

To get the object, the last two ways of variable parameters such as String and Class objects are rarely used.

To follow the picture of a cat and a tiger, my test file is changed as follows:

Run successfully ~ ~?

Follow up 1. Object is singleton by default in the container

Practice is the only criterion for testing:

Then use getBean () to get an object and test whether it is still the same one.

That is:

Public class MyTest {public void test myTest () {ApplicationContext context = new ClassPathXmlApplicationContext ("service.xml"); Rectangle rect = context.getBean ("rectangle", Rectangle.class); Rectangle rect2 = context.getBean ("rectangle", Rectangle.class); System.out.println (rect = = rect2);}

Return to True or False?

A: True

Because the default is singleton, if you want to change it, you need to change it in the configuration file.

As for the use of these tags, it is no longer extended here.

Follow up 2. When were the objects in the container created?

Practice is the only criterion for testing:

Define a constructor without parameters, print a sentence in it, and then only new ClassPathXmlApplicationContext, as shown below:

It is found that it can also be printed, so every time the container is started, all the objects in the container are already created. (of course, this doesn't apply when scope = "prototype", only when singleton. )

By the way, it's best to keep a constructor with no parameters all the time, because here the bean object is created through reflection

Clazz.newInstance () calls constructor with no parameters by default.

But now it's been discarded and replaced with this:

Clazz.getDeclaredConstructor () .newInstance ()

Build a project using Maven

Let's go back to the beginning of the construction project, I believe we all realize the complexity of manually importing jar packages. In fact, we can also use Maven to manage the jar packages in the project, which is also a common way in the company, eliminating the process of manually downloading jar packages.

1. New project

It's a lot easier to use Maven. First, let's create a Maven project, which is different from the previous process:

When New Project, choose to build from Maven, rather than a simple Java project.

After it is built, we will find that there are a lot more things than the Java project just now:

Unlike the previous empty project, there are main and test, where resources is the place to put the configuration file, that is, our service.xml should be put here. If it is not put in the right place, the code can not find it.

two。 Add the corresponding pom dependency, so you don't need to manually guide the jar package.

Warehouse address https://mvnrepository.com/

Search spring

Select Spring context → 5.2.3 release, and copy the configuration into pom.xml.

Finally, the required package will appear automatically in the external libraries on the left. Import with one click is not too convenient.

3. Write code ~ ~? Summary

Finally, let's take a look at the process of creating objects with Spring:

Through ApplicationContext, the entry of the IoC container, use its two specific implementation subclasses to read data from class path or file path and get the specific bean instance with getBean ().

So what exactly does using Spring omit from our work?

A: the new process. Leave the process of new to a third party to create and manage, this is "decoupling".

Spring also uses the set () method, which only provides a more complete set of implementation mechanisms.

In the final analysis, the underlying principle is not very complicated, but in order to improve scalability and compatibility, Spring provides rich support, so it is difficult to find the source code.

Because the framework is intended for a wide variety of users, they think more about extensibility. If we implement it, maybe three or five lines can be done, but what we achieve is not perfect, incomplete, not rigorous, in short, not high-end, so it writes 30 to 50 lines, makes the framework design as perfect as possible, and provides rich support. to meet the needs of different users in order to occupy a larger market.

Thank you for reading this article carefully. I hope the article "how to build the first Spring Project" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report