Spring -- Introduction and environment setup: Hello and springhello

Source: Internet
Author: User
Tags apc to domain

Spring -- Introduction and environment setup: Hello and springhello
Spring

 

Spring is an open source code design framework. It solves the loose coupling problem between the business logic layer and other layers. Therefore, it runs interface-oriented programming ideas throughout the entire system application. It is created to solve the complexity of enterprise application development. One of the main advantages of the Framework is its layered architecture, which allows you to choose which component to use and provides an integrated framework for J2EE application development.

Spring framework

The Spring framework is a layered architecture consisting of seven well-defined modules. The Spring module is built on the core container. The core container defines how to create, configure, and manage beans.

Each module (or component) that makes up the Spring framework can exist independently or be implemented together with one or more modules. The functions of each module are as follows:

  • Core container: The Core container provides the basic functions of the Spring framework. The main component of the core container isBeanFactoryIt is the implementation of the factory model.BeanFactoryUseControl reversal(IOC) mode separates application configuration and dependency specifications from actual application code.
  • Spring Context: Spring context is a configuration file that provides context information to the Spring framework. Spring context includes enterprise services, such as JNDI, EJB, email, internationalization, checksum and scheduling.
  • Spring AOP: Through the configuration management feature, the Spring AOP module directly integrates Aspect-oriented programming functions into the Spring framework. Therefore, any objects managed by the Spring framework can easily support 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 applications without relying on EJB components.
  • Spring DAO: The jdbc dao abstraction layer provides a meaningful exception hierarchy for managing Exception Handling and error messages thrown by different database vendors. The exception hierarchy simplifies error handling and greatly reduces the number of Exception Code to be written (for example, opening and closing connections ). JDBC-oriented exceptions of Spring DAO follow the common DAO exception hierarchy.
  • Spring ORM: The Spring framework inserts several ORM frameworks to provide ORM Object Relational tools, including JDO, Hibernate, and iBatis SQL Map. All of these comply with the general transactions and DAO exception hierarchies of Spring.
  • Spring Web module: The Web context module is built on the application context module, providing context for Web-based applications. Therefore, the Spring framework supports integration with Jakarta Struts. The Web module also simplifies processing multiple requests and Binding Request Parameters to domain objects.
  • Spring MVC Framework: The MVC Framework is a full-featured MVC implementation for Building Web applications. Through the policy interface, the MVC framework becomes highly configurable. MVC supports a large number of view technologies, including JSP, Velocity, Tiles, iText, and POI.
IOC

 

The basic concept of the inversion mode (also known as dependency intervention) is to describe how to create objects without creating them. The Code does not directly connect to objects and services, but describes which component needs which service in the configuration file. Containers (IOC containers in the Spring framework) are responsible for connecting these together.

IOC implementation mode: 1. Allocate dependencies through attributes of JavaBean (such as the setter method)

2. The dependency is provided as a constructor and is not disclosed as a JavaBean attribute.

AOP

Aspect-oriented programming (AOP) is a programming technology that allows programmers to modularize the behavior of cross-concern or cross-common division of duties (such as log and transaction management. The core structure of AOP isAspectIt encapsulates actions that affect multiple classes into reusable modules.

AOP and IOC are complementary technologies that use modular methods to solve complex problems in enterprise application development. In a typical object-oriented development method, you may need to place the logging statement in all methods and Java classes to implement the logging function. In the AOP method, the log ServiceModular, And apply them to the components that require logs in a declarative manner. Of course, the advantage is that the Java class does not need to know the existence of the log service or consider the relevant code. Therefore, application code written with Spring AOP is loosely coupled.

Spring Environment Construction

 

Step 1:Go to the official spring website and find the jar of the spring framework. The latest version is available.

Step 2:See a Java project in your eclipse project. Create a lib folder, put your jar package into it, and introduce it. Create an xml file under your src file, it is about some spring configurations. Here the name can be defined by yourself. In the future, the specified name will be applicationContext. xml, you can also set this name now, project structure:

Step 3:First, create an interface and its implementation class.

Interface

Package com. spring. action;/***** @ author stick to your GL **/public interface HelloWorld {public String hello ();}

Implementation class

Package com. spring. action;/***** @ author stick to your GL **/public class HelloWorldImpl implements HelloWorld {@ Override public String hello () {// TODO Auto-generated method stub return "Hello World !!! ";}}

Step 4:Configure your xml as follows:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">      <bean id="helloWorld" class="com.spring.action.HelloWorldImpl"></bean>  

Briefly describe some of the attribute descriptions and some non-existent attribute descriptions.

<! -- Id is the bean identifier and must be unique. If no configuration id is configured, name is the identifier by default. If id and name are configured, multiple aliases can be set for name, the delimiter can be a space, a comma, and a semicolon. The class is the fully qualified name of the bean, that is, the package name and class name. If the id and name are not configured, you can use the applicationContext. getbean (Class) gets the object. scope: bean scope. Value: singleton. The entire container only produces one object. The default value is singleton prototype: prototype, each time a bean is obtained, a new object request is created. Each request creates a new object session. In a session range, only one object application is generated. In the application range, an object autowire is created: automatic Assembly is used to simplify spring configuration. Value: byname: searches for the corresponding bean Based on the name (based on the content after set in the set Method ). Load bytype: automatically assemble according to the type, no need to worry about the id, but only one bean of the same type can exist. f otherwise, the constructor is reported. When the bean is injected and instantiated through the constructor, assembly Construction Method --> <! -- You can read the xml of other configurations. If the file name is in the current directory, write the complete package name. If the file name is in another directory, separated by "/" --> <import resource = "beans1.xml"/>

Step 5: Create a test class

Package com. spring. action; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext;/***** @ author stick to your GL **/public class Test {public static void main (String [] args) {// TODO Auto-generated method stub ApplicationContext apc = new ClassPathXmlApplicationContext ("springXML/HelloWorld. xml "); HelloWorld hello = apc. getBean ("helloWorld", HelloWorld. class); System. out. println (hello. hello ());}}

It is the simplest way to write a Hello World.

 

[Version Declaration] This article is an original article by the blogger. For more information, see the source.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.