ComputersProgramming

Spring Java: basics and lessons for beginners

To date, almost all applications use the script programming method - Spring Java. The main advantage of this language is that the user is given the opportunity to develop the 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 a new functional and at the same time to control the existing structure. One of the most striking and simple examples is the management of transactions. Using Spring Java, the structure of the transaction is managed independently of the main database connectivity logic. Any changes in the design of logic will not affect the transactionality, that is, do not violate the main idea of the program. Spring has modularity.

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

Spring Java also makes it much easier to initialize and tweak the program elements, that is, you can easily configure the applications for yourself without having to do global reconfigurations with the main Spring code.

Today you can find in Java Spring textbooks in a huge number and in different languages (English, Russian, etc.). Therefore, you can master Javu yourself, and we will try to help you in this!

Spring from the inside

As the architecture of "Java Spring" is built from within, probably not everyone knows. Therefore, let's consider in more detail 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, it is necessary to use the appropriate mechanism - the parsing of application components.
  • Configure the created interfaces (views) - BeanDefinition.
  • Set custom objects with FactoryBean.
  • Creation of copy (rough) beans.
  • Configure the created beans.
  • Checking the functionality of written beans.

Spring: The Beginning

If you just decided to start learning about Java 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 in Java. The simplicity and transparency of the framework will allow you to see for yourself the errors in writing.

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

  • Controller - inversion of control. Ensure correct operability, are responsible for the configuration of application elements and regulate the functional cycle of Java objects.
  • Access to data - the functional is aimed at managing relational databases (DB) in Java. Thanks to this framework, you can solve a huge number of complex tasks based on Java environments.
  • An aspect-oriented configuration framework. Works with components that can not be implemented without any loss in Java.
  • Transaction management. The framework coordinates the AP and the toolkit with customized transaction management objects for Java elements.
  • Remote access and management framework. The configuration takes place locally.
  • Authentication and authorization. They support the configuration on many popular protocols through Spring Security.

Today for all frameworks in Spring there are a number of possibilities:

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

It is necessary to understand the purpose of all frameworks, since this is the basis of Java Spring. For beginners, it is recommended to start studying all the components in groups and try to use them on examples.

How to make a simple application in Java Spring (framework)

The application can be configured in Spring in several ways. One of the most common and fairly simple is taking out the configuration in an XML file. This type is traditionally used in many versions of Java Spring. For beginners, we recommend using tools like java (1.5), tomcat (6), ant (1.7), eclipse (3), junit 4x.

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

  • Create the application structure. To do this, you need to install on eclipse a special plug-in - Spring IDE. It is also necessary to create a project on Springapp and additionally add the folder war.
  • Create index.jsp. In the Java Spring web, add WEB-INF and put web.xml into it.
  • We'll put the program into tomcat. In order to deploy the application, it is necessary to use the ANT-script, which will contain a target for compiling, transferring and building the program.
  • After writing all the applications completely, make a check. Run 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 point, you can say that you hastily completed the configuration of the network environment. Next, you need to tweak the Spring Java config:

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

What did we do in Spring Java? For dummies, some of the above listed items will be incomprehensible, so try to describe more simply:

  1. We created a start page-index.jsp, which serves to check the correctness of the installation environment.
  2. Registered dispatcher with the appropriate description file springapp-servlet.xml.
  3. They created the controller and wrote the text for it.
  4. Set the appearance of the 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.

Key components of Spring Security:

  • SecurityContextHolder. Contains current security information, and includes detailed information about the user who launched the application. By default, the SecurityContextHolder saves the information in ThreadLocal, which means that the security context is available for all methods that are implemented in this thread.
  • SecurityContext. Contains all information about the security of the system that is associated with the user's request, and also the Authentication object.
  • Authentication. Recognizes the user from the program side.
  • GrantedAuthority. Displays the permissions that the user has received across the application.
  • UserDetails. Displays the necessary information to configure Authentication from other security sources. Contains data such as user nickname, password and flags.
  • UserDetailsService. Used to create UserDetails by implementing one method of this interface.

Java Integration

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

  • The routers.
  • Adapters for interacting with other systems.
  • Service activators.
  • Transformers and others.

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, there are several key ones. It:

  • Support for HBase-based configuration.
  • Professional support for Batch Spring.
  • Support for use with the integration of Java Spring.
  • Parameterization of Hadoop configuration.
  • Devices for template processing of connecting components with Hadoop.
  • Declarative support for Hadoop tools.
  • Programmable support for Hadoop components.

Java Hibernate

Virtually all web applications can not do without storing a lot of information. As a rule, various special programs are used to solve this problem - DBMS, etc. By structure of organization and frequency of database use can be divided into several subgroups, and so it turned out 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 it easier to write an application - recommends using ORM (Object-Relational Mapping). This method is implemented by the Hibernate library, which converts data from one type to another. In addition, with its help you can easily configure connections to DBMS files and manage transactions.

To transform one class to 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 interrelation with the DB and POJO.
  • Automatic generation of Primary Key components.
  • HQL.
  • Cache: Second level, Session Level and, of course, Query.
  • Good performance.
  • Sampling Outer Join.

Practice for beginners

One of the simplest textual projects in Java Spring is writing an application "hello." The platform does not require additional registration or any complex designs and forms on the bootstrap. We use simple html without dynamics. And let the basic form has:

  • The text field is "hello".
  • The button "create a record", when clicked on, our text will be sent for processing and stored in the database.
  • A list that will display the previously created record.

We do not need anything else. Let's move on to Java Spring. Practice:

  • Of course, install Java on your computer.
  • Set the maven.
  • Create a Spring-boot project.
  • Install the database.
  • Distribute the model classes. Best of all, create one.
  • Implement the operation with the components using create-read-update-delete.
  • Create the form on jsp.
  • Bind your form to the appropriate controller.

What is MVC, and how does it relate to Spring

MVC is Spring's own platform, which is focused on user requests. It provides a statistical view for all functions of the request-orienting systems. The task of each interface should be simple and concise, so that the user, if desired, can easily re-implement the applications without problems. This platform makes a direct connection to the front-end code. All interfaces are associated with the Servlet API object. Due to this relationship, the programming of the application is greatly simplified.

The most important interfaces are:

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

The most important interfaces are:

  • 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 changed without problems by 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", not just MVC (model-view-controlle).

Basic package

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

The DAO package provides a JDBC abstraction layer, which eliminates the need to make tedious JDBC encoding and parsing the provider's database of specific error codes. In addition, you can make software, as well as declarative transaction management, not only for classes that implement interfaces, but for all of your Pojos (simple Java objects).

The ORM package provides the integration of layers of popular APIs for displaying relational objects, including JDO, Hibernate and Ibatis. Using the OPM package, you can apply all O / R-cartographers in combination with other functions of the Spring-offer.

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

By applying metadata at the source level, you can include all kinds of behavioral information in code, for example NET-attributes.

The Spring Web package provides basic web integration functions, such as multi-part functionality, initializing contexts using the listener servlets and the context of Web-based applications. 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.

Requirements for domain objects

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

  • "Java" -bin or POJO.
  • The class must not be inherited (not final).
  • The designer is registered. It goes standard without any parameters.
  • Write Implements Serializable. This is necessary for the components to be stored in a cache in a special form.
  • Register the identification of all fields, that is, objects can not be stored in the database if they do not have a Primary key.
  • Presence of attributes.
  • In getters, write a specific reference to a collection of objects, not a copy of it. It is not advisable to use arrays, because when you need to return our Array, you need to register a copy of it. In addition, it is difficult to inherit from the array.
  • To become acquainted with the letter. Before you start working with Java Spring, you need to understand many of the terms and meanings of certain functions. Simply put, get at least the slightest idea what it is and what it is used for.
  • Try to write yourself one of the simple tasks. For example, the application _INVET_, "snake" or some other.
  • To become acquainted with WEB. Best of all, online courses help. If you do not want to, then try again for books and deal with new concepts yourself.
  • Try to complicate the simple problems of greeting more complex constructions. We also recommend trying to write a simple application that could be useful to you in real life. So it's easier for you to master this programming method.

Auxiliary groups of objects

There are several additional classes:

  • "Spring Tao." This class helps to work with databases (databases).
  • Transaction Confidentiality. Typically, when writing an application in all cases, the Transaction Manager is used. After the user has defined the methods, the transactions will be added, automatically created and terminated.
  • Springer Security. This class specifies the authorization of the user (login, password, etc.). Rationally create at least 10 binomials in order to achieve increased efficiency in the functionality of Security.

Decoding of the basic abbreviations in Java Spring

Getting acquainted with such a specific scripting method of programming, as "Java Spring", you can find different abbreviations. As a rule, not all sites or textbooks can find their decoding, so let's see what is Java Spring. Example:

  • Inversion of Control - IoC - control inversion or, more simply, the main application module.
  • AOP - aspect-oriented programming. Thanks to this module, Java can connect to any access point without problems 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, ensures the interaction of all parts in one system. Creates web objects.
  • The WEB class is intended to facilitate the development of the authorization of the framework, it is responsible for access to beans, and so on.
  • ORM- Object-Relational Mapping - connects databases (databases) with concepts of object-oriented programming methods, creating a new virtual database.
  • DAO is designed to work with databases in the structure of Spring.
  • Service Abstraction - specifies the interfaces (appearance) of the application, that is, it is responsible for abstracting the task.

Today you can find quite a lot of useful and interesting information on Java Spring. Lessons, tutorials, webinars will certainly help you to study this scripting programming language without problems. If you have any problems with writing the application, review the documentation on "Spring", in which the authors described in detail all the requirements for the Java structure, and also answered questions on how to fix frequently made errors.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.birmiss.com. Theme powered by WordPress.