In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the overview and simple application of Java Spring". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the overview and simple application of Java Spring.
Catalogue
Overview of Spring
Simple application of Spring
Basic use of the Spring framework
Project creation
Add dependency package
Create a Spring profile
Modify the configuration file
Modify the test class
Running result
Overview of Spring
Spring is created to solve the complexity of enterprise application development. As an open source middleware, it uses basic JavaBean to accomplish things that can only be done by EJB (Java Enterprise bean) before. Spring is independent of all kinds of application servers, and can provide the functions of application servers even without the support of application servers. At the same time, it provides an inherited framework for JavaEE application development, which is a "one-stop" choice for enterprise application development. The use of Spring is not limited to server development, any Java application can benefit from Spring.
Spring is an open source framework whose functions are extracted from actual development. One of its main advantages is the hierarchical architecture, which is composed of seven well-defined modules (components), all of which are built on top of the core container.
Spring Core: the core container provides the basic functions of the Spring framework, and its main component, BeanFactory, is the implementation of the factory pattern. It separates the application configuration and dependency specification from the actual program code through the IOC mechanism.
Spring Context: provide contextual information to the Spring framework, including enterprise services.
The Spring DAO:JDBC DAO abstraction layer provides a useful hierarchical structure for managing exception handling and error messages thrown by different database vendors.
Simple application of Spring
When creating a simple Java project, you are asked to output "Hello world!" on the console, and "Hello Beer!" if required. Wait, then you have to constantly modify the source code of the program. If the project is large, there is more than one statement involving this output in the program, and it is scattered in the source code of the entire project (often tens of thousands). It would be disastrous for the systematic maintenance of a software.
Package org.example;public class TestHello {private String message; public String getMessage () {return message;} public void setMessage (String message) {this.message = message;}} package org.example;public class Test {public static void main (String [] args) {TestHello testHello=new TestHello (); testHello.setMessage ("Hello world! …") ; System.out.println (testHello.getMessage ());}}
Let's use Spring to solve this problem. Use Spring to realize the flexible change of program output.
The Spring framework basically uses the project to create
Add dependency package
Add the following dependency to the red box.
Org.springframework spring-core 5.3.8 org.springframework spring-beans 5.3.8 org.springframework spring-context 5.3.8 org.springframework spring-context-support 5.2.12.RELEASE org.springframework spring-expression 5.3.8 commons-logging commons-logging 1.2
After joining, click this button, wait a moment, ideal will automatically help us download the dependency package.
Create a Spring profile
Modify the configuration file
Open ApplicationContest.xml and modify it:
The unique identifier of the id:bean, which is equivalent to the object name we learned.
The fully qualified name corresponding to the class:bean object: package name + type.
Property: is equivalent to setting a value for a property in an object.
Value: specific value, basic data type.
Hello World! Modify the test class package org.example;import javafx.application.Application;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {public static void main (String [] args) {/ / get the ApplicationContext object ApplicationContext application=new ClassPathXmlApplicationContext ("ApplicationContext.xml") / / the parameter in the TestHello object / / getBean () method obtained through ApplicationContext is the value of id of Bean in the configuration file TestHello testHello= (TestHello) application.getBean ("TestHello"); System.out.println (testHello.getMessage ());}} run result
To output "hello Beer!" You only need to modify the value value in the configuration file
Hello Beer!
This example does not see the advantages of Spring, when the size of the project is very large, and there are many such output statements in the source code, Spring is fully reflected. Because with Spring, you only need to modify the value in one place of the configuration file to make all the output change consistently, instead of modifying the program code one by one!
At this point, I believe you have a deeper understanding of the "overview and simple application of Java Spring". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.