Spring's charm "transferred from QCA"

xiaoxiao2021-03-06  119

Spring's philosophy is to join the Java object to the framework without affecting the design of the Java object.

The EJB framework uses an invasive method to design an object, which requires you to join the code that meets the EJB specification in your design. Some lightweight COP frameworks, such as Avalon, also require some specification, such as a serviceable interface, which is typical when designing object design.

Type 1 practice.

This design idea requires Spring to design a framework in a dynamic, flexible way. So Spring has a large number of reflections. First, Spring has a problem to be resolved how to manage beans. Because IOC's thinking requires that the bean cannot be called directly, it should be collapsed in a passive manner. So the management of beans is the core part of Spring.

Reflecting the problem at the level of the code, sometimes it can bring unexpected flexibility. But it is sometimes a philosophical problem, whether it is in ORM design or in AOP design - it is reflected, or use code generation.

In Spring, the core of processing this problem is in the org.springframework.beans package. The most core part is BeanWrapper. Beanwrapper, as the name suggests, is the BEAN's wrapper. So, its main work is to call any of the beans, including the settings and methods of the property (including embedded property). In BeanWrapper's default implementation class beanwrapperImpl, although the code is longer, the completed work is very concentrated.

In-depth study of BeanWrapper.

Let's take a look at how this beanwrapper works, suppose we have two beans:

Public class company {

PRIVATE STRING NAME;

Private Employee ManagingDirector;

Public string getname () {

Return this.name;

}

Public void setname (String name) {

THIS.NAME = Name;

}

Public Employee getManagingDirector () {

Return this.managingdirector;

}

Public void setmanagingdirector (EMPLOYEE ManagingDirector) {

THIS.ManagingDirector = managingdirector;

}

}

PUBLIC CLASS EMPLOYEEEE {

Private float salary;

Public float getsalary () {

Return Salary;

}

Public void setsalary (float salary) {

THIS.SALARY = SALARY;

}

}

Then we use BeanWrapper to call these two beans: Company c = new company ();

Beanwrapper bwcomp = beanwrapperImpl (c);

// setting the company name ...

BWcomp.SetPropertyValue ("Name", "Some Company Inc.");

// ... Can Also Be Done Like this:

PropertyValue V = New PropertyValue ("Name", "Some Company Inc.");

BWcomp.SetPropertyValue (V); // OK, Let's Create The Director and Tie It To The Company:

Employee Jim = New Employee ();

Beanwrapper bwjim = beanwrapperimpl (jim);

Bwjim.SetPropertyValue ("Name", "Jim Stravinsky);

BWcomp.SetPropertyValue ("ManagingDirector", Jim);

// Retrieving The Salary of The ManagingDirector Through The Company

FLOAT SALY = (FLOAT) BWcomp.getPropertyValue ("ManagingDirector.salary);

It seems to have a lot of trouble, but so Spring can use a unified way to manage the properties of Beans.

Bean manufacturing plant

With packaging of a single bean, you need to manage multiple beans. In Spring, beans are included in a core library for management. There are two ways to produce beans: one is a bean generates multiple examples, one is a bean only generates an example. If we are familiar with the design pattern, we will think that the former can use prototype, the latter can use Singleton.

Note that the use of reflection techniques makes we create objects like the original factory method model. Reflection can create an object based on the name of the class. So Spring only uses the two basic modes of Prototype and Singleton.

Spring is doing this, but we want users to maintain unified interfaces, without having to care about the current Bean's independent bean generated by Prototype, or a shared bean generated by Singleton. So, BeanFactory in the org.springframework.beans.factory package defines a unified GetBean method.

JDBC re-encapsulates JDBC elegant encapsulated underlying database, but JDBC still has many unchangements. You need to write a lot of code to complete the crud operation, and JDBC has throws a SQLException in any case, this approach is called incomplete information on an exception. Because the problem may be very complicated, maybe a database connection problem, perhaps the problem with concurrent control, maybe just the SQL statement error. There is no reason to get all the problems with a simple SQLException, which is somewhat irresponsible. In response to these two questions, Spring Framework proposes two solutions: First, provide a framework, all of which are more common in JDBC applications, allocation, etc., users only need to provide specific implementations. OK is OK. The specific details of the implementation use a template method. For example, in the org.springframework.jdbc.object package, the MappingsqlQuery class implements mapping the SQL query into a specific business object. The JavaDoc wrote: Reusable query in which concrete subclasses must implement the abstract mapRow (ResultSet, int) method to convert each row of the JDBC ResultSet into an object the user must implement mapRow method, which is a typical application template method. Let's take a specific example to see: class userquery extends mappingsqlQuery {public userQuery (DataSource Datasource) {

Super (DataSource, "SELECT * FROM PUB_USER_ADDRESS WHERE User_ID =?");

DeclareParameter (New SqlParameter (Types.Numeric);

COMPILE ();

}

// map a result set row to a java object

Protected Object MapRow (ResultSet RS, INT Rownum) throws sqlexception {

User User = new user ();

User.setId (rs.getlong ("user_id");

User.setForeName (RS.GetString ("forename");

Return User;

}

Public user finduser (long id) {

// use superclass convenience method method to provide strong type

Return (User) FindObject (ID);

}

}

Second is the second question, the most troublesome place should be said to be an exception of JDBC, then determine the type of exception, and re-throw an exception. The wrong problem can be obtained by connecting, so how trouble is how to intercept exceptions. The method used by Spring Framework is the callback, and the class has been called the Template in the Spring Framework.

JDBCTemplate Template = New JDBCTEMPLATE (Datasource);

Final List names = new linkedlist ();

Template.query ("Select User.name from User",

New rowcallbackhandler () {

Public Void ProcessRow (ResultSet RS) throws sqlexception {names.add (rs.getstring (1));

}

});

The callback function is an anonymous class, which also uses the template method, and the processing of exception is completed in the parent class.

Layer

A large number of MVC-based web containers have appeared in the open source field, but these containers are limited to the range of the web, which does not involve the connection of the rear of the web hierarchy. Spring as a holistic framework, define a web layer and the backend The connection method of the business layer, this idea is still moving the scope of MVC, but coupling is loose, does not depend on the specific integration level.

Public Class GooglesearchController

IMPLEMENTS Controller {

Private IgogleSearchport Google;

PRIVATE STRING GoogleKey;

Public void setgoogle (IgooglesearchPort Google) {

THIS.GOOGLE = Google;

}

Public void setgooglekey (string googlekey) {

THIS.GOOGLEKEY = GoogleKey;

}

Public ModlandView HandleRequest

HTTPSERVLETREQUEST REQUEST, HTTPSERVLETRESPONSE RESPONSE

Throws servletexception, ioException {

String query = Request.getParameter ("query");

Googlesearch Result RESULT =

// Google Property Definitions Omitted ...

// Use Google Business Object

Google.Dogooglesearch (this.googleKey, Query,

Start, MaxResults, Filter, Restrict,

Safesearch, LR, IE, OE;

Return New ModelandView ("GoogleResults", "Result", Result

}

}

The callback function is an anonymous class, which also uses the template method, and the processing of exception is completed in the parent class.

转载请注明原文地址:https://www.9cbs.com/read-126501.html

New Post(0)