Java framework (iv) -- spring

Source: Internet
Author: User

Spring is an open-source framework created by Rod Johnson. It is created to solve the complexity of enterprise application development. Spring uses the basic Javabean to complete the previous tasks that may only be completed by EJB. However, spring is not only used for server-side development. From the perspective of simplicity, testability, and loose coupling, any Java application can benefit from spring.


In short, spring is a lightweightIOC -- inversionof Control)AndAspect-oriented (AOP -- aspectoriented programming)Container framework. The so-called control inversion is the Dependency inversion principle we learned in the design model. It does not depend on the specifics and must rely on abstraction. The aspect-oriented approach is to extract the aspect from the business processing process. It is faced with a certain step or stage in the processing process, in order to obtain the isolation effect of low coupling between different parts in the logical process.


The core of spring design is the org. springframework. Beans package, which is designed to be used with the JavaBean Component. This package is usually not directly used by users, but is used by the server as the underlying intermediary for most other functions. The next highest level abstraction is the beanfactory interface, which is an implementation of the factory design pattern and allows the creation and retrieval of objects by name. Beanfactory can also manage relationships between objects.


In the SSH box, spring acts as the management container. We all know that hibernate is used as a persistence layer, because it encapsulates JDBC well. programmers do not need to write a large number of SQL statements when interacting with the database. Struts is used for the application layer. It is responsible for calling the Serivce layer of the business logic. Therefore, the SSH Framework process is roughly: JSP page ---- struts ------ Service (business logic processing class) --- hibernate (left to right) struts is responsible for controlling service (business logic processing class ), thus, the Service lifecycle is controlled. In this way, the dependency between layers is strong and is coupled. In this case, the Spring framework is used to control the action objects (in strus) and service classes. The relationship between the two is loose, and the ioc mechanism of spring (control inversion and dependency injection) it is used here. To put it bluntly, spring serves as a container to manage the work between struts and hibernate. It is actually a bridge or manager in the program. The whole program is managed by spring. How can I manage it? The most common is IOC.

 

The following is a simple Login Demo to share with you, which combines the use of structs and hibernate:

1. The configuration information of applicationcontext. 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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="dao" class="dao.CustomerDao"></bean><bean name="/login" class="prj16.action.LoginAction"><property name="icdao"><ref local="dao"/></property></bean></beans>

2. The following is the actionservlet of the object in the IOC container:

Package prj16.action; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import idao. icustomerdao; import Org. apache. struts. action. action; import Org. apache. struts. action. actionform; import Org. apache. struts. action. actionforward; import Org. apache. struts. action. actionmapping; import Po. customer; import prj16.form. loginform; public class loginaction extends action {PR Ivate icustomerdao icdao; Public icustomerdao geticdao () {return icdao;} public void seticdao (icustomerdao icdao) {This. icdao = icdao ;}@ overridepublic actionforward execute (actionmapping mapping, actionform form, httpservletrequest request, httpservletresponse response) throws exception {loginform = (loginform) form; string account = loginform. getaccount (); string Password = loginform. getPassword ();/ /Call daocustomer Cus = icdao. getcustomerbyaccount (account); If (cuz! = NULL & Cus. getPassword () = PASSWORD) {request. setattribute ("MSG", "welcome" + Cus. getcname () + "Logon successful");} else {request. setattribute ("MSG", "Logon Failed");} return New actionforward ("/login. JSP ");}}

The above code is only a part. The code of the entire project has been uploaded. Download the code without points

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.