Spring Java: Basics and Tutorials for Beginners

Today, almost all applications use the scripting programming method - Spring Java. The main advantage of this language is that the user is given the opportunity to develop a program in the form of a simple set of loose-coupled elements that are not tied to each other.

Simply put, the smaller the application blocks are interconnected, the easier it is to register new functionality and at the same time control an existing structure. One of the most striking and simple examples is transaction management. With the help of Spring Java, the management of the transaction structure is carried out regardless of the main logic of database connectivity. Any changes in the design of the logic will not affect the transactionality, that is, they will not violate the main idea of โ€‹โ€‹the program. Spring is modular.

Various elements and structures can be removed or added in any places almost independently of each other. In fact, the program can be divided in such a way that it does not even understand that it is managed using Spring Java. In addition, this scripting programming language greatly simplifies unit testing, since an element developed for IoC content can be easily injected into false relationships and thereby verify the functionality and accuracy of its description.

spring java

Spring Java also significantly simplifies the initialization and fine-tuning of program elements, that is, you can easily and easily configure applications for yourself without doing global reconfigurations with the main Spring code.

Today you can find tutorials on Java Spring in a huge number and in various languages โ€‹โ€‹(English, Russian, etc.). Therefore, you can overpower "Java" yourself, and we will try to help you with this!

Spring inside

Probably not everyone knows how the architecture of Java Spring is built from the inside. Therefore, let's take a closer look at this construction:

  • Setting BeanDefinition and parsing the context configuration (Xml, JavaConfig, Groovy, and AnnotationConfigApplicationContext). At the initial stage, all interfaces are created - BeanDefinition. Depending on the choice of configuration, you must use the appropriate mechanism - parsing application components.
  • Configuring created interfaces (views) - BeanDefinition.
  • Install custom objects using FactoryBean.
  • Creating instance (draft) beans.
  • Customize the generated beans.
  • Checking the functionality of written beans.

Spring: the beginning

If you just decided to get acquainted with Java Spring programming, we recommend starting with Java ee Spring (framework). This is one of the simplest open source methods for this Java platform. The framework creates a consistent design that can be easily applied to most tasks that were previously described using Java. The simplicity and transparency of the framework will allow you to see spelling errors on your own.

java spring for beginners

Spring can be described as a combination of several independent frameworks, as well as more complex constructions (a framework in a framework). Most of these structures can function independently of each other, however, they demonstrate a huge amount of performance when running the application together. These components are divided into structural details of complex programs depending on their purpose:

  • Controller - inversion of control. They ensure correct operation, are responsible for the configuration of application elements and regulate the functional cycle of Java objects.
  • Access to data - the functionality is aimed at managing relational databases (DB) in Java. Thanks to this framework, a huge number of complex tasks based on Java environments are solved.
  • Aspect-Orientation Framework. It works with components that cannot be implemented without any loss in Java.
  • Transaction management. The framework coordinates the AP and toolkit with customized transaction management objects for Java elements.
  • Remote access and management framework. Configuration takes place locally.
  • Authentication and authorization. They support configuration on many popular protocols through Spring Security.

Today, for all the frameworks in Spring, a number of features are provided:

  • Automatic resource management.
  • Processing of excluded components when accessing the exception data of Spring itself.
  • Transparent transaction management during any data operations.
  • Unzipping resources.
  • Abstraction processing in many formats.

You need to understand the purpose of all frameworks, as this is the foundation of Java Spring. For beginners, it is recommended that you begin to study all the components in groups and try to use them with examples.

How to make a simple Java Spring application (framework)

There are several ways to configure an application in Spring. One of the most common and fairly simple is considered to be the removal of the configuration into an XML file. This type is traditionally used in many versions of Java Spring. For beginners, we recommend using tools such as java (1.5), tomcat (6), ant (1.7), eclipse (3), junit 4x.

java hibernate spring

In order to describe a simple task in Java Spring, you must adhere to this order:

  • Create the application structure itself. To do this, you will need to install a special plug-in on eclipse - Spring IDE. It is also necessary to create a project on Springapp and additionally add the war folder.
  • Create index.jsp. In Java Spring web, additionally register WEB-INF and put web.xml in it.
  • Deploy the program in tomcat. In order to deploy the application, you need to use the ANT script, which will contain the goal for compiling, transferring, and building the program.
  • Having written all the applications completely, do a check. Launch tomcat and open a web page with the address: localhost: 8080 / springapp /.
  • Then you will need to install the Spring Framework and unzip it.

At this stage, we can say that you hastily completed the setup of the network environment. Next up is the Spring Java config:

  • First you need to make some changes to web.xml, which you will find in the WEB-INF folder. Our goal is to register an application input dispatcher.
  • We dispatch input requests. We decided to send all requests from the form '.htm' to the servlet dispatcher.
  • Then copy the data libraries to WEB-INF / lib.
  • Create a controller in springapp.web and write a test.
  • Set the view and compile the applications on the server.
  • Try to launch applications.

What have we done in Spring Java? For dummies, some of the points listed above will be incomprehensible, so let's try to describe more simply:

  1. We created the start pageโ€“ index.jsp, which serves to verify the correct installation of the environment.
  2. Registered the dispatcher with the corresponding description file springapp-servlet.xml.
  3. We created the controller and registered the text for it.
  4. Set the type of application.

What is Java Spring Security and why is it needed?

Spring Security allows you to authorize and authenticate applications. In addition, this framework provides security for corporate tasks that are created using the Spring Framework.

spring java for dummies

Key components of Spring Security:

  • SecurityContextHolder. It contains current security information and also includes details about the user who launched the application. By default, SecurityContextHolder stores information in ThreadLocal, which means that the security context is available for all methods that are implemented in this thread.
  • SecurityContext. Contains all the security information related to the user's request, as well as the Authentication object.
  • Authentication Recognizes the user from the software side.
  • GrantedAuthority. Shows the permissions that the user has received across the entire application.
  • UserDetails. Displays the necessary information to configure Authentication from other security sources. Contains data such as user nickname, password and flags.
  • UserDetailsService. They are used to create UserDetails using the implementation of one method of this interface.

Java Integration

Integration Java Spring is a unique framework that includes customization of functions for sending messages. In addition, this object is responsible for building an efficiently orienting architecture:

  • Routers.
  • Adapters for interaction with other systems.
  • Service Activators.
  • Transformers, etc.

Spring hadoop

This is a new development of VMware, which connected two huge projects - Apache Hadoop and Spring. Among the many advantages of the new framework, it is worth highlighting several main ones. It:

  • HBase based configuration support.
  • Professional support for Batch Spring.
  • Usage support with Java Spring Integration.
  • Hadoop configuration parameterization.
  • Hook-up templating devices for connecting components.
  • Declarative support for Hadoop tools.
  • Programmable support for Hadoop components.

java spring example

Java hibernate

Almost all web applications cannot do without storing a lot of information. As a rule, various special programs are used to solve this problem - DBMS, etc. By the structure of the organization and the frequency of use of the database, it can be divided into several subgroups, and it so happens that relational ones are the most common. As a rule, the description of these structures is quite large, and it is not always rational to use them.

Java Spring, a technology to make writing an application easier, recommends using ORM (Object-Relational Mapping). The Hibernate library implements this method, which converts data from one view to another. In addition, with its help, you can easily configure connections to DBMS files and manage transactions.

To transform one class into another in Java Hibernate Spring, there are several special generators:

  • Increment.
  • Identity
  • Sequence.
  • Hilo.
  • Seqhilo.
  • Uuid.
  • Guid.
  • Native
  • Assigned
  • Foreign
  • sequence-identity.

Benefits of using Java Hibernate Spring:

  • A simple structure of the mechanisms of interconnection with the database and POJO.
  • Automatic generation of Primary Key components.
  • HQL.
  • Cache: Second level, Session Level and, of course, Query.
  • Good performance.
  • Sample Outer Join.

java ee spring

Practice for beginners

One of the simplest text projects in Java Spring is to write a hello application. The platform does not require additional registration or any complex structures and forms on bootstrap. We use simple html without dynamics. And let the main form have:

  • Hello text input field.
  • The "create record" key, when clicked, our text will be sent for processing and stored in the database.
  • A list that will display the previously created record.

We donโ€™t need anything else. We pass to Java Spring. Practice:

  • Of course, install Java on your computer.
  • Put the mawan.
  • Create a Spring-boot project.
  • Install the database.
  • Distribute model classes. Itโ€™s best to create one.
  • Implement a component operation using create-read-update-delete.
  • Create a form on jsp.
  • Bind your form to the appropriate controller.

What is MVC, and how is it related to Spring

MVC is Springโ€™s proprietary platform that focuses on user requests. It contains a statistical view for all the functions of query-orienting systems. The task of each interface should be simple and concise, so that the user, if desired, could easily re-implement applications. This platform provides a direct connection to the front-end code. All interfaces are associated with a Servlet API object. Thanks to this interconnection, application programming is greatly simplified.

The most important interfaces include:

  • Model - model, data.
  • View - display, appearance.
  • Controller - management, establishing the relationship between the first two concepts.

The most important interfaces include:

  • HandlerAdapter.
  • Controller
  • View
  • HandlerMapping.
  • ViewResolver.
  • HandlerInterceptor.
  • LocaleResolver.
  • MultipartResolver.

Java Spring MVC (model-view-controlle) opens up many additional features for the developer:

  • Clear and simple separation between requests.
  • Each interface is solely responsible for its part of the work.
  • The interface can be easily changed using an alternative implementation.
  • All interfaces are associated with the Servlet API.
  • Increased level of efficiency and functionality for web applications.
  • The ability to use different parts of Spring, and not just MVC (model-view-controlle).

Main package

The main package is the most fundamental part of the system and provides the ability to implement dependencies, allows you to control the bean functionality of the container. The main concept here is BeanFactory, which provides a factory template, eliminates the need for singleton software support and allows you to separate the configuration and specification of dependencies from the actual program logic.

The DAO package provides a JDBC level of abstraction, which eliminates the need for tedious JDBC coding and parsing the database of the code for specific error codes. In addition, you can do software as well as declarative transaction management not only for classes that implement interfaces, but also for all your Pojos (simple Java objects).

The ORM package integrates layers of popular APIs for displaying relational objects, including JDO, Hibernate, and Ibatis. Using the ORM package, you can use all O / R mappers in combination with other functions of the Spring offer.

The AOP package in Spring provides a compatible aspect of oriented programming, the implementation of the AOP Alliance, which allows you to define, for example, an interceptor method and untie the code for implementing functionality.

Using metadata at the source level, you can include all kinds of behavioral information in your code, such as NET attributes.

The Spring web package provides basic web integration features such as multi-part functionality, context initialization using listener servlets, and the context of web-based applications. The implementation in Spring MVC provides a clear separation between the domain code model and the web form, which allows you to use all the other features of the Spring Framework for validation.

java spring web

Domain Object Requirements

In order for the JPA application to function normally, it is necessary to monitor the correctness of the writing of such requirements:

  • "Java" bean or POJO.
  • A class must not be inherited (not be final).
  • The constructor is registered. It comes standard without any parameters.
  • Write Implements Serializable. This is necessary in order for the components to be stored in a cache in a special form.
  • Prescribe the identification of all fields, that is, objects cannot be stored in the database if they do not have a Primary key.
  • The presence of attributes.
  • In getters, write a specific link to a collection of objects, not a copy of it. It is undesirable to use arrays, since when you need to return our Array, you need to register a copy of it. In addition, it is quite difficult to inherit from the array.
  • Read the letter. Before you start working in Java Spring, you need to parse in many terms and meanings of certain functions. Simply put, get at least the slightest idea of โ€‹โ€‹what it is and what it is used for.
  • Try writing one of the simple tasks yourself. For example, the application _PRIVET_, "snake" or some other.
  • Check out WEB. Best of all, online courses will help. If you do not want, then grasp books again and deal with new concepts yourself.
  • Try to complicate simple hi-puzzles with more complex constructs. We also recommend trying to write a simple application that could be useful to you in real life. So it will be easier for you to master this programming method.

Helper Object Groups

There are several additional classes:

  • Spring Tao. This class helps to work with databases (databases).
  • Transaction Springing. As a rule, when writing an application, the Transaction Manager is used in all cases. After the user has defined methods, transactions will be added, automatically created and completed.
  • Spring Security. This class sets up user authorization (login, password, etc.). It is rational to create at least 10 binomes in order to achieve increased efficiency in the functionality of Security.

Deciphering abbreviations in Java Spring

Acquainted with such a specific scripting method of programming, such as "Java Spring", you can come across different abbreviations. As a rule, not all sites or textbooks can be decrypted, so let's see what Java Spring is. Example:

  • Inversion of Control - IoC - inversion of control or, more simply, the main module of the application.
  • AOP - aspect-oriented programming. Thanks to this module, Java can easily connect to any access point and register the necessary code there.
  • MVC - Model-view-controller is a structured template that defines and describes the way to build the design of the entire application, provides the interaction of all parts in one system. Creates web objects.
  • WEB , , . .
  • ORM- Object-Relational Mapping โ€“ ( ) - , .
  • DAO "".
  • Service Abstraction โ€“ ( ) , .

Java Spring. , , - . - , "", "", , .


All Articles