Three minutes easy to understand Spring framework basics

Source: Internet
Author: User
Tags aop

As a qualified Java developer, you must have heard of the spring framework, though it will be applied, but it may not be able to understand the principles of the framework, but just like everyone else, the general just stays on the understanding level. The mini-series will take you through the spring framework with a step-by-step look.


Struts:web layer, relatively simple (valuestack value stack, interceptor);

Hibernate:dao layer, knowledge point Miscellaneous;

Spring:service layer, important, how much to say how much.


The architecture in mind:



I. Overview of the Spring framework


1.1 What is Spring


Spring is an open source framework, and spring is a lightweight Java development framework that emerged in 2003 by Rod Johnson in his book expert one-on-one development and Some of the concepts and prototypes elaborated in design are derived. It is created to address the complexities of enterprise application development. One of the main advantages of the framework is its layered architecture, which allows the user to choose which component to use, while providing an integrated framework for Java EE application development. Spring uses basic JavaBean to accomplish things that were previously only possible by EJBS. However, the use of spring is not limited to server-side development. From the standpoint of simplicity, testability, and loose coupling, any Java application can benefit from spring. The core of spring is control inversion (IoC) and facet-oriented (AOP). In short, spring is a layered javase/ee full-stack (one-stop) lightweight open-source framework.


lightweight : Compared to EJB, it relies on less resources and destroys less resources.


tiering: A one-stop solution for every layer


Web tier : STRUTS,SPRING-MVC

Service Layer : Spring

DAO layer : Hibernate,mybatis, jdbctemplate–> Spring-data


1.2 Spring origin


Expert one-to-one-ee Design and development


Expert one-to-one EE development without EJB


1.3 Spring Core


The core of spring is control inversion (IoC) and aspect-oriented (AOP)


? 1.4 Spring Benefits


Easy decoupling, simplified development (cohesion poly-low coupling)



Spring is a large factory (container) that allows all object creation and dependency maintenance to be given to spring management


Spring Factory is used to generate beans


Support for AOP programming


Spring provides aspect-oriented programming, which can easily implement the functions of permission interception, operation monitoring, etc.


Support for declarative Transactions


The management of transactions can be done only through configuration, without the need for manual programming


Easy testing of programs


Spring support for JUNIT4, which can be easily tested with annotations for spring programs


Easy integration of a variety of excellent frameworks


Spring does not exclude a variety of excellent open source frameworks that provide direct support for a variety of excellent frameworks (e.g. Struts, Hibernate, MyBatis, quartz, etc.)


Reduce the difficulty of using Java EE APIs


Spring provides encapsulation for some of the most difficult APIs (JDBC, JavaMail, remote calls, and so on) in Java EE Development, making these API applications much less difficult to apply


? 1.5 Spring Architecture


The Spring framework is a layered architecture that eats a range of functional elements and is divided into about 20 modules. These modules are divided into Rore Container, Date access/integration, Web, AOP (Aspect oriented programming), instrumentation, and test sections, as shown in:



Core containers: beans, core, context, expression


Ii. Introductory Case: IoC "Mastery"


2.1 Importing JAR Packages


4 + 1:4 cores (beans, core, context, expression) + 1 dependencies (Commons-loggins...jar)



? 2.2 Target Class


1. Provide UserService interface and implementation class


2. Obtaining an instance of the UserService implementation class


In the previous development, a direct new object can be.

After learning spring, the object instance will be created by spring –> IoC controlled inversion (inverse of control).

When you need an instance object later, from the Spring Factory (container), you need to configure the fully qualified name of the implementation class into an XML file.


UserService Interface:


1 public interface UserService {


2 public void AddUser ();


9 ·




Userserviceimpl Implementation class:


1 public class Userserviceimpl implements UserService {


2 @Override


3 public void AddUser () {


4 System.out.println ("A_ico add User");


5}


6}






? 2.3 Configuration file


Location: arbitrary, under development in general under CLASSPATH (SRC)


Name: Any, common applicationcontext.xml in development


Content: Adding schema constraints


Constraint file location: spring-framework-3.2.0.release\docs\spring-framework-reference\html\ xsd-config.html


<?xml version= "1.0" encoding= "UTF-8"?>


<beans xmlns= "Http://www.springframework.org/schema/beans"


Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"


Xsi:schemalocation= "Http://www.springframework.org/schema/beans


Http://www.springframework.org/schema/beans/spring-beans.xsd ">


<!--Configuring service


<bean> Configure the objects you want to create


ID: Used for subsequent instance acquisition from the Spring container.


Class: The fully qualified class name of the instance that needs to be created


-


<bean id= "Userserviceid" class= "COM.ITHEIMA.A_IOC. Userserviceimpl "></bean>


</beans>


? 2.4 Test


@Test


public void demo02 () {


Obtained from the spring container


1 Getting the container


String Xmlpath = "Com/itheima/a_ioc/beans.xml";


ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext (Xmlpath);


2 Getting content-no need for yourself new, all from the spring container


UserService UserService = (userservice) applicationcontext.getbean ("Userserviceid");


Userservice.adduser ();


}




Iii. Introductory Case: DI "Mastery"


DI Dependency Injection, Dependency injection

is a: It is a, inherited.

Has a: There is a, member variable, dependent.


Class B {


Private a A; Class B relies on Class A


}


Dependency: One object needs to use another object


Inject: Another object instance setting through the setter method.


For example:


Class bookserviceimpl{


Previously developed: interface = Implementation Class (service and DAO coupling)


Private Bookdao Bookdao = new Bookdaoimpl ();


After spring (decoupling: the Service implementation class uses the DAO interface and does not know the specific implementation class)


Private Bookdao Bookdao;


Setter method


}


Simulating the spring execution process

1. Create service instance: Bookservice bookservice = new Bookserviceimpl () –>ioc <bean>

2. Create a DAO instance: Bookdao Bookdao = new Bookdaoimple () –>IOC

3. Set the DAO to Service:bookService.setBookDao (Bookdao); –>di<property>




3.1. Target class


· Creating Bookservice interfaces and implementing classes


· Creating Bookdao interfaces and implementing classes


· The DAO and service configuration XML files


· Using API Testing


3.1.1 DAO


Public interface Bookdao {


public void Addbook ();


}


public class Bookdaoimpl implements Bookdao {


@Override


public void Addbook () {


System.out.println ("di add book");


}


}


3.1.2 Service


Public interface Bookservice {


public abstract void Addbook ();


}


public class Bookserviceimpl implements Bookservice {


Mode 1: Before, interface = Implementation class


Private Bookdao Bookdao = new Bookdaoimpl ();


Mode 2: interface + Setter


Private Bookdao Bookdao;


public void Setbookdao (Bookdao Bookdao) {


This.bookdao = Bookdao;


}


@Override


public void Addbook () {


This.bookDao.addBook ();


}


}


3.2 Configuration Files


<beans xmlns= "Http://www.springframework.org/schema/beans"


Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"


Xsi:schemalocation= "Http://www.springframework.org/schema/beans


Http://www.springframework.org/schema/beans/spring-beans.xsd ">


<!--


Simulating the spring execution process


Create service instance: Bookservice bookservice = new Bookserviceimpl () IoC <bean>


To create a DAO instance: Bookdao Bookdao = new Bookdaoimpl () IoC


Set DAO to Service:bookService.setBookDao (Bookdao); DI <property>


<property> for attribute Injection


Name:bean property name, obtained by setter method


Setbookdao ##> Bookdao ##> Bookdao


Ref: reference to the ID value of another bean


-


<!--Create service--


<bean id= "Bookserviceid" class= "Com.itheima.b_di. Bookserviceimpl ">


<property name= "Bookdao" ref= "Bookdaoid" ></property>


</bean>


<!--creating a DAO instance--


<bean id= "bookdaoid" class= "Com.itheima.b_di. Bookdaoimpl "></bean>


</beans>




3.3 Testing


@Test


public void demo01 () {


Obtained from the spring container


String Xmlpath = "Com/itheima/b_di/beans.xml";


ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext (Xmlpath);


Bookservice Bookservice = (bookservice) applicationcontext.getbean ("Bookserviceid");


Bookservice.addbook ();


}



Three minutes easy to understand Spring framework basics

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.