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

A tutorial on how to use Spring from 0 to 1

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Start the Spring method tutorial from 0 to 1. Aiming at this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Spring definition

Spring is an open source framework created to address the complexity of enterprise application development. One of the main advantages of the framework is its hierarchical architecture, which allows you to choose which component to use, while providing an integrated framework for J2EE application development. The seven modules of the Spring framework are shown in the following figure:

The functions of each module are as follows:

Spring core: the core container provides the basic functionality of the Spring framework. The main component of the core container is BeanFactory, which is the implementation of the factory pattern. BeanFactory uses the inversion of Control (IOC) mode to separate the application's configuration and dependency specifications from the actual application code.

The Spring Context:Spring context is a configuration file that provides context information to the Spring framework. The Spring context includes enterprise services such as JNDI, EJB, email, internationalization, checksum and scheduling capabilities.

Spring AOP: through the configuration management feature, the Spring AOP module directly integrates aspect-oriented programming functions into the Spring framework. Therefore, you can easily make the Spring framework manage any object that supports AOP. The Spring AOP module provides transaction management services for objects in Spring-based applications. By using Spring AOP, you can integrate declarative transaction management into your application without relying on components.

The Spring DAO:JDBC DAO abstraction layer provides a meaningful exception hierarchy that can be used to manage exception handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the amount of exception code that needs to be written (such as opening and closing connections). Spring DAO's JDBC-oriented exceptions follow the general DAO exception hierarchy.

The Spring ORM:Spring framework inserts several ORM frameworks, providing object-relational tools for ORM, including JDO, Hibernate, and iBatis SQL Map. All of this follows Spring's generic transaction and DAO exception hierarchy.

The Spring Web:Web context module is built on top of the application context module and provides context for Web-based applications. Therefore, the Spring framework supports integration with Jakarta Struts. The Web module also simplifies the work of handling multipart requests and binding request parameters to domain objects.

The Spring MVC:MVC framework is a full-featured MVC implementation for building Web applications. Through the policy interface, the MVC framework becomes highly configurable, and MVC hosts a number of view technologies, including JSP, Velocity, Tiles, iText, and POI

The most important of these are Spring Core (Control inversion Technology, or IOC) and Spring AOP, which I will introduce in detail in subsequent articles. Today we will focus on the environment building of Spring and the writing of Hello Spring Demo.

Environment building

The easiest way to create a maven project is as follows: pom.xml

Org.springframework spring-webmvc 5.2.5.RELEASE

Bean in Spring

The Spring container can be thought of as a large factory, and the Bean in the Spring container is equivalent to the products of that factory. If you want this large factory to be able to produce and manage Bean, you need to tell the container which Bean is needed and how the Bean needs to be assembled together.

Bean is a reusable component written in the JAVA language. To be written as JavaBean, the class must be concrete and public, and have a parameterless constructor. JavaBean exposes member properties of the internal domain by providing public methods that conform to consistent design patterns, which are obtained by the set and get methods. In short, JavaBean is a Java class that follows some specifications. It is well known that attribute names conform to this pattern, and other Java classes can discover and manipulate these JavaBean properties through reflection mechanisms.

The example code is as follows:

Private attribute encapsulated by the public class Person {/ / Person class / / name String type private String name; / / gender String type private String sex; / / Age int type private int age; / / Person class No-parameter constructor public Person () {} / / public method provided by the Person class for accessing private attributes public String getName () {return name;} public void setName (String name) {this.name = name } public String getSex () {return sex;} public void setSex (String sex) {this.sex = sex;} public int getAge () {return age;} public void setAge (int age) {this.age = age;}}

Configuration files in Spring

Spring configuration files are "drawings" used to guide Spring factories in Bean production, dependency injection (assembly) and Bean instance distribution. The Spring configuration file is one or more standard XML documents, and applicationContext.xml (which can also be defined as another name) is the default configuration file for Spring. When the container startup cannot find the specified configuration document, it will attempt to load this default configuration file. We can save the configuration file in the src/main/resources directory

Spring configuration files support two different formats, the XML file format and the Properties file format. Typically, Spring uses the XML file format as the configuration file for Spring, which registers and manages the dependencies between Bean through the XML file. The root element of the XML format configuration file is that it contains multiple child elements, each of which defines a Bean and describes how the Bean is assembled into the Spring container. The basic format of the configuration file is as follows:

Among them

Xmlns represents the namespaces that xml needs to introduce.

Bean= object

Id= variable name. Id is the identifier of bean. To be unique, it can be understood as an instance name.

Classes of class= new

Property is equivalent to setting values for properties in an object

Hello Spring

Write a class code

Public class HelloSpring {private String name= "hello"; HelloSpring () {System.out.println ("HelloSpring");} public String getName () {return this.name;} public void setName (String name) {this.name = name;}}

Add class configuration information for HelloSpring to the configuration file

Write a test class

Public class HelloTest {public static void main (String [] args) {/ / get the spring context object ApplicationContextcontext = new ClassPathXmlApplicationContext ("beans1.xml"); / / our objects can be managed in spring. If we want to use them, we can just take them out HelloSpringhello = (HelloSpring) context.getBean ("hellospring"); System.out.println (hello.getName ()) }}

Run the class HelloTest, and the result is as follows:

Hello Spring / / Constructor output value Hello / / getName method output value from 0 to 1 to start Spring method tutorial questions shared here, I hope the above content can be of some help to you, if you still have a lot of doubts unsolved, you can follow the industry information channel to learn more related knowledge.

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

Development

Wechat

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

12
Report