"SSH Advanced Path" Spring Introduction, build the spring environment-lightweight container Framework (i)

Source: Internet
Author: User
Tags to domain

after learning from struts and hibernate, our cold winter (hibernate) has finally ushered in a sunny spring (spring). At the beginning of the series blog "SSH Advanced path" Struts + Spring + Hibernate advanced Start (i), we introduce the basic concepts of Spring and the two most important core IOC and AOP, Basically introduced is relatively simple, one sentence has been brought. Starting with this blog, we begin to re-introduce the basic theory of the spring framework and build a simple spring development environment.

What is Spring?

Spring is a hodgepodge of integrated third-party frameworks with the core technology of IOC (control inversion, also called Dependency injection) and AOP (aspect-oriented programming), so spring is both an IOC container and an AOP framework. Knowing what spring is the beginning of learning, let's say why Spring is used.

Why use Spring?

We know that no spring,struts and hibernate can work well, and in the opening I call the architecture without spring called "the Plank Bridge", with spring architecture called "Sunshine Boulevard". To put it bluntly, spring solves the complexities of enterprise application development, using basic JavaBean to accomplish ejbs, and in terms of size and overhead, spring is lightweight. Spring has a number of advantages:

1, make our stratification better.

The SSH framework system is divided into four layers: the presentation layer, the business logic layer, the data persistence layer, and the Domain module layer (the entity layer).

From what we can see Spring is at the business logic level, acting as a link between struts and hibernate bridges. The entire hierarchy of the system can be at a glance.


2, the object management is better.

from that, we see spring putting transactions, session, and business layer services into the business logic layer to manage. The system becomes clearer, not only making the persistence layer more functional, but also improving the flexibility of the system.

3. AOP

For aspect programming, AOP lets developers create non-behavioral concerns and insert them into app Code Red. Public services, such as logs, persistence, transactions, and so on, can be decomposed into facets and applied to domain objects without increasing the complexity of the object model of the domain object.

4. IoC

IoC, in the vernacular, is the relationship between the container control program, rather than the traditional implementation, directly manipulated by the program code. This is the concept of the so-called "inversion of Control": control is transferred from the application code to the external container, and the transfer of control is called reversal.

Use

There are many uses for spring, but the core and the classic of spring can be simplified to three parts:

1. IOC containers can be controlled by spring to manage dependencies between objects.

2, AOP convenient for aspect-oriented programming, is the expansion of OOP, want to add what function directly Add.

3, can integrate a variety of excellent framework, struts, hibernate and so on, there are many, no longer one by one listed.

Architecture diagram

I believe you can see that this is the Spring architecture diagram, Spring contains a lot of features, in fact, are contained in six modules: Core, AOP, DAO, ORM, Jee and the web, we are not unfamiliar with them, when you learn or use spring, At least three parts of the content have been used: DAO, ORM (Object Relational mapping), Web. Let's look at the following:

1. DAO

The DAO framework encapsulates all database access operations, and Spring provides support for DAO, as well as the template JDBC, which further encapsulates JDBC. Whether spring JDBC or various ORM frameworks (Hibernate, IBatis, MyBatis), they all further encapsulate JDBC, making the data access layer more flexible and easy to use. Of course, they also have their own advantages and disadvantages, in the project technology selection is very important. At the same time, spring can also integrate third-party frameworks.

2. ORM

Spring provides support for ORM frameworks that can integrate most of the mainstream ORM frameworks. JPA is a specification for Java persistence Annotations, and Hibernate is an implementation of JPA. TopLink is an ORM framework for Oracle, and JDO is a specification of Sun Corporation. The OJB is Appach. Ibatis is not strictly orm,orm is generally a mapping of cloud data, describing classes and attributes, which are SQL-based mappers. thanks to the endless variety of ORM frameworks, you can check out which company they are and their pros and cons.

3. Web

Spring not only has its own presentation layer SPRINGMVC, but it also supports third-party presentation-layer framework struts,webwork, as well as some third-party tools.

In this blog post, we have the core three parts of spring, and the post I will cover in detail. Below we build a spring-added user's development environment:

Build the spring development environment


1. Download the springframework and unzip to the specified directory. I'm using Spring3.2.9, which version doesn't matter as long as the jar is added to the project.

2. Create a new Java project in the IDE and add the required jar of spring to the project, I use the IDE as MyEclipse.

3, Spring uses Apache common_logging, and unifies Apache log4j as the log output component. In order to observe spring's log output during debugging, create a new log4j.properties configuration file in Classpath with the following content:

<span style= "FONT-SIZE:12PX;" >log4j.rootlogger=info, stdoutlog4j.appender.stdout= org.apache.log4j.consoleappenderlog4j.appender.stdout.layout= org.apache.log4j.patternlayoutlog4j.appender.stdout.layout.conversionpattern=%d%p [%c]-%m%n</span>

4, provide spring configuration file applicationcontext.xml.

5. Write code

After the configuration is complete, the directory structure looks like this:

Userdao interface

Public interface Userdao {public void AddUser (String username,string password);}

The realization of Userdao

Userdao4mysqlimpl

public class Userdao4mysqlimpl implements Userdao {public void AddUser (String username,string password) { System.out.println ("Userdao4mysqlimpl.adduser ()"); System.out.println ("username=" +username); System.out.println ("password=" +password);}

Userdao4oracelmpl

<span style= "FONT-SIZE:12PX;" >public class Userdao4oracleimpl implements Userdao {public void AddUser (String username,string password) { System.out.println ("Userdao4oracleimpl.adduser ()"); System.out.println ("username=" +username); System.out.println ("password=" +password);} </span>

Usermanager interface

Public interface Usermanager {public void AddUser (String username,string password);}

The realization of Usermanager

Import Com.liang.spring.dao.userdao;public class Usermanagerimpl implements Usermanager {//define a member variable, use constructor to assign value to private Userdao userdao;public Usermanagerimpl (Userdao userdao) {This.userdao = Userdao;}  /** * Set injection * @param userdao *///public void Setuserdao (Userdao userdao) {//this.userdao = userdao;//} @Overridepublic void AddUser (string userName, string password) {userdao.adduser (userName, password);}}

Client

Import Org.springframework.beans.factory.beanfactory;import Org.springframework.context.support.classpathxmlapplicationcontext;import Com.liang.spring.manager.UserManager; public class Client {/** * @param args */public static void main (string[] args) {//By our application is responsible for service (object) positioning//This is an assembly process, usermanage R with Userdao, this relationship is described by the program, and is now described with a configuration file//usermanager Usermanager = new Usermanagerimpl (new Userdao4mysqlimpl ());// Usermanager.adduser ("Zhang San", "123");//beanfactory is an interface with different implementations, Classpathxmlapplicationcontext is the implementation of Beanfactory Read the applicationcontext.xml in beanfactory factory = new Classpathxmlapplicationcontext ("Applicationcontext.xml");// The ApplicationContext implements the Beanfactory interface//applicationcontext factory = new Classpathxmlapplicationcontext (" Applicationcontext.xml ");//getbean plus product identification, equivalent to the implementation of Usermanager, and then into the Usermanager interface Usermanager Usermanager = ( Usermanager) Factory.getbean ("Usermanager") Usermanager.adduser ("Jiuqiyuliang", "123456");}}

spirng configuration file: Applicationcontext.xml

<?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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xsi:schemalocation=" Http://www.springframework.org/schema/beans http:/ /www.springframework.org/schema/beans/spring-beans-2.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www. Springframework.org/schema/aop/spring-aop-2.0.xsd Http://www.springframework.org/schema/tx Http://www.springfram Ework.org/schema/tx/spring-tx-2.0.xsd "><!--ID is a unique identity and cannot be--><!--equivalent to a factory--><bean id=" Userdao4mysql "class=" Com.liang.spring.dao.UserDao4MySqlImpl "></bean><bean id=" Userdao4oracle "class= "Com.liang.spring.dao.UserDao4OracleImpl" ></bean><!--Usermanager using the Userdao interface, Userdao uses constructors to assign dependencies to usermanager--><!--Usermanager depends on which write DAO,IOC container is not only a factory, it provides the ability to manage dependencies. -->&lT;bean id= "Usermanager" class= "Com.liang.spring.manager.UserManagerImpl" ><!--uses a constructor that describes a kind of dependency relationship. Usermanager relies on the implementation of the MySQL container to provide the ability to find, after finding new good, then give Usermanager, put into the usermanager process for Di, dependency injection. (Active injection)--><constructor-arg ref= "Userdao4mysql" ></constructor-arg><!--or <constructor-arg ref= " Userdao4oracle "></constructor-arg>--> </bean></beans>

In the above configuration file can be free to switch the implementation of various databases, DAO layer of flexibility imaginable.

Test structure of the project:


People do not understand the place to see the comments in the code, I will provide you with two versions of the source:

Spring2 and Source code

Spring3.2.9 and Source code

Summarize

Finally, today, we have just begun to understand spring, just the tip of the iceberg. For spring, I prefer to use the following eight words to describe: The Sea of rivers, the capacity is large.

The next blog post introduces several ways to inject IOC, thank you for your attention.

"SSH Advanced Path" Spring Introduction, build the spring environment-lightweight container Framework (i)

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.