Building the SSM Framework Spring

Source: Internet
Author: User

As a graduate of the graduating students, the most important thing is the graduation design, the previous days just got the completion of the topic: "3D Network Diagram: Visual software design for network structure data", (⊙o⊙) ..., how to say, see the title is confused (previous years are not all students choose the course system, student education management system- _-| |, good routine that), looking at the topic, see in the company to learn the PeopleSoft, I go, nothing to do. In school for two years of Java, off-campus internship to pay more than 10,000 study Java EE, find a job to do peoplesoft-_-| |, BI set is a visual design, alas, is the world changeable, life impermanence ah.

Well, spit groove to spit groove, in order to eat, in order to graduate anything must slowly. Take a closer look at the topic: "This topic requires students to use WEBGL technology to develop a Web application for data visualization." The visualization of network structure data has always been the focus of the field of data visualization. Using WebGL technology, you can use modern browser directly on the Web page to render 3D effect, for network structure data visualization and interactive design provides new possibilities. "Suddenly think of the days in school on those web pages to render 3D effect code of fascination (also at that time to learn JS, and even decided after the choice of off-campus internship), recalled that time, see now do PeopleSoft, ideal and reality gap is really big O (╥﹏╥) O, Probably familiar with the topic after the chat with the teacher, the original set and my ideal is so close, then began to do, and to do well, people are not only for the reality and live, at least to their own ideals take a few steps.

First of all, the teacher starters set up a frame, to tell the truth whether it is an internship or in the university, the framework has not in-depth understanding (internship just learned JS, find work, also did not expect to do PeopleSoft), then start from the beginning, slowly!

Open a long time not moving Java data, opened a long time not to read Java video, learning to learn to suddenly have a desire to cry (do not ask me why, is very sad), the following is my study of spring process, will always be updated until the completion of the finished.

First I learned that spring is a community and a big project, which contains a lot of sub-projects, and I'm mainly using the spring Framework of the core project.

The Spring-framework framework is a hierarchical architecture that contains a series of functional elements and is divided into approximately 20 modules. These modules are divided into core Container, Data access/integration, Web, AOP (Aspect oriented programming), instrumentation, and test sections, One of the main things I learned was core container and AOP.

One, there are two important concepts in spring:

Ioc:inversion of Control inversion
The spring core container can also be called an IOC container, which is primarily responsible for managing the declaration cycle of objects (object creation, initialization, destruction, etc.)
The IOC container does not implement more functions, but it does not require much code, does not need to consider the complex coupling between objects to get the right object from the IOC container, and provides reliable management of various objects, greatly reducing the complexity of the development.

Summarize:
1.IOC means that the classes you have designed are given to the spring container to be controlled, rather than being controlled within the class itself.
2. After having an IOC container, the remaining work is only to accumulate wood in the IOC container.
3. The relationship between objects and objects in the previous project was written directly to death in the code, and the IOC container was used instead of writing objects and objects in the code, but rather the establishment of the relationship was given to the IOC container and was dynamically established when the code was running ( We can make any changes through the configuration file)
4.IOC is actually a thought that separates the caller from the callee.

Di:dependency Injection Dependency Injection
The concept of Di is proposed to replace the IOC, which means that the dependency of the calling class on an interface implementation class is injected by the container to remove the invocation class's dependency on an interface implementation class. (Consider the relationship between the servlet and service layer interfaces and the service Layer interface implementation Class)
The term dependency injection is clearly more straightforward and understandable than the inversion of control.

Second, after the understanding of this, download spring:

http://repo.springsource.org/libs-release-local/org/springframework/spring/3.2.4.RELEASE/ Spring-framework-3.2.4.release-dist.zip
Stick directly to the Address bar or download tool, each time there is an update as long as the version number can be changed.

Third, then is the spring configuration process and needs to understand the content:

Spring IOC container Core API (container will be an object in the future)
Beanfactory interface
Beanfactory is the core interface of the IOC container in spring, primarily for processing bean initialization and configuration, and for establishing dependencies between objects

The main methods in the interface are as follows:
Returns a bean instance based on the specified name
Object Getbean (String name)

Determine if the bean named name is a prototype, i.e. whether a new instance is always returned (not singleton)
Boolean isprototype (String name)

Determine if the bean named name is a singleton
Boolean Issingleton (String name)

Determines whether the container contains a bean instance of the given name
Boolean Containsbean (String name)

If a bean named name has an alias, it is returned so the alias
String[] Getaliases (String name)

ApplicationContext interface
This interface inherits from the Beanfactory, enhances the beanfactory, adds the transaction processing AOP, the internationalization, the event passing and so on function

So in code we typically use the ApplicationContext interface and the corresponding implementation class of this interface to create a spring container object
For example:
String Path = "Com/briup/ioc/set/set.xml";
ApplicationContext container =
New Classpathxmlapplicationcontext (path);

Student s = (Student) container.getbean ("Student");

Iv. Configuration Files
Spring instantiates, configures, and assembles project objects by reading the data in the configuration file, typically using an XML file as a configuration file

The basic structure of XML is 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:context= "Http://www.springframework.org/schema/context"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.2.xsd ">

</beans>

Precautions:
1. The header declaration of this file can be found in the sample downloaded in the spring document
2. Two schema files (. xsd files) are used here, respectively, Spring-beans-3.2.xsd and Spring-context-3.2.xsd, which can also be found in the downloaded spring document
3. After associating the XML file with the schema file in eclipse, there is a hint of the tag code in the XML (notice that the association is not configured incorrectly)
The 4.spring framework is modular, and when you use other modules, you can continue referencing the corresponding schema files in other modules in the root element of the XML, and then you can use the tag code in the new module.

Test examples:

Set mode injection (must depend on the Set method):

To import a jar package:

Create two test classes, such as student and teacher, where the teacher class contains student.

To write an XML file:

<?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:context= "Http://www.springframework.org/schema/context" xsi:schemalocation= "Http://www.springframework.org/schema/beans HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPR Ing-beans-3.2.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/ Context/spring-context-3.2.xsd "> <bean name=" stu "class=" com.briup.bean.Student "> <property name=" id "Value=" 1 "></property> <property name=" name "value=" Tom "></property> <property name=" Age "V alue= ></property> </bean> <bean name= "Teacher" class= "Com.briup.bean.Teacher" > <p Roperty name= "id" value= "1" ></property> <property name= "name" value= "Zhangsan" ></property> < Property name= "Student" ref= "Stu" ></property> </bean> 

Write test methods:

public void Ioc_set () {try {string[] path = {"Com/briup/ioc/set/set.xml"}; ApplicationContext container = new Classpathxmlapplicationcontext (path); Teacher t = (Teacher) container.getbean ("Teacher"); System.out.println (t); System.out.println (T.getid ()); System.out.println (T.getname ()); System.out.println (T.getstudent ()); System.out.println ("-------------"); System.out.println (T.getstudent (). GetId ()); System.out.println (T.getstudent (). GetName ()); System.out.println (T.getstudent (). Getage ());} catch (Exception e) {e.printstacktrace ();}}

This is an example of spring implementing control inversion, dependency injection.

Summary (Spring):

1. Reduce coupling (introduce other classes when running in one class, not direct new objects)
2. Remove duplicate code in some methods (such as log file, permission control, transaction processing (open transaction, COMMIT TRANSACTION, exception transaction)//need this code, but this code can not write directly here)
Spring can dynamically combine code during runtime
3. Managing the life cycle of objects
The objects are placed in the 4.spring core container, which are called beans (reusable components)
5.IOC: Reverse, active change passive
Di: Dependency (Explanation of dependency: for example, the Web layer relies on the service layer, the service layer relies on the DAO layer) injection, the concept of Di is proposed to replace the IOC's

Spring to build the SSM framework

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.