Spring Combat (third Edition) Tour (part one)

Source: Internet
Author: User


 A winter vacation bar SSM study, can not say is finished, can only say is over once, know the general process, the framework of the building, but the specific principle of implementation, why should this, when to do so, the concept is still very vague.

Anyway after the five framework of learning, the most important thing is the spring framework, so, the beginning of a book "spring combat", in the online word of mouth is still pretty high, so continue to study (review) under Spring this Framework.

So far, I've Read the 8th chapter, using spring Web Flow, and suddenly think of writing a blog record of the course of reading, can also be said to summarize? Notes? anyway, I was thinking about it when I was looking at this part of the Content.

Good to Go!

Chapter One Spring tour

The main contents of the chapter Are:

    • Explore the Spring Core module
    • Decoupling application objects
    • Using AOP to manage crosscutting concerns
    • Spring's Bean Container

This chapter has just begun to introduce the background of the next spring, praising how new and exciting the Java language has come out, and then in the development process developers encounter a very big problem: with the development of the project, the complexity of the more and more high, some complex applications such as transactions, security, distribution , these Java APIs are not given directly but to write on their own, then the JavaBean specification has not satisfied the needs of the time, so Sun company released an EJB specification (do not know what this is what the book is written, it is useless), Then this specification out of the javabean, but ah, people still think too simple, EJB or too garbage, and did not complete the original idea, seems to be too cumbersome. So we want to return to the JavaBean specification, spring is invented this time, this frame has become the Pojo lightweight, attention! Lightweight! The leader of the development framework, attention! Spring Dafa is good!

1.1 Simplifying Java development

I said a few lines of crap and told us what technology was written in which book told us to see (not go). The message I got from the previous paragraph was:

  The synonym for the bean is Pojo

the next step is to simplify development, reduce the complexity of Java development, and take the following four strategies:

1. pojo-based Lightweight and minimal intrusive programming

2. Loose coupling via dependency injection and interface-oriented

3. Declarative programming based on facets and conventions

4. Reduce boilerplate code through facets and molds

1.1.1 inspires the potential of pojo.

Here's a piece of code, which i'll take a different example to understand:

A class called Dog has inherited an interface called animal this animal there are many ways: fly (), eat (), sing () ....

Here I am the dog to implement the animal interface, and then realize the fly eat ... These methods, but I this pig will only eat ah, fly sing These methods I do not need, but also to implement an empty, so that the code seems very redundant (not good).

So why can't I just implement the Eat method and ignore the other methods? This is when Spring's superiority comes, and he doesn't need you to implement other methods just write a eat () {return "shit"} ok, simple, Graceful.

1.1.2 Dependency Injection

Depend on? Injection? This is what the ghost, although often listen to the word but still can not be particularly understanding, rely on what? Inject what?

The book also writes an example of the class class tightly coupled with an interface, meaning and the previous similar, and then listed the disadvantages of writing code like This:

1. Unit testing is difficult, because tight coupling, if you want to do unit testing, you also have to ensure that another class of methods are called

2. Tightly coupled code is difficult not only difficult to test, but also difficult to reuse, difficult to understand, who would like to see a large string of implementations, a large string of objects, a large number of methods of Declaration.

then spring solves the problem by using dependency injection (DI).

Do you think my class can be passed into a different class when building a function (the class implements the same interface), I'm going to pass whatever I want, my dog, I'm sick. I passed in a doctor when I was building it, and when I was hungry, I passed in a cook, I was bullied when the introduction of a host (the owner is the word ...) anyway, these incoming are inherited human this interface, I want what pass what, and not hard code directly in the above Declaration. This is the constructor injection in dependency injection (DI)

So the coupling degree is loose, of course, the problem of the unit test above is solved, The example of the book gave a mock implementation, this thing probably should be, to A. Class file, and then you can generate the class file of the Java file Object. specifically, not to delve into, not the Point.

One way to solve hard coding is to configure the XML file, and spring is also where you can assemble a bean in an XML file, which is a bit difficult to understand and comb under:

  In my understanding, spring is like a big cup, inside the bean is the object in the cup, each bean has its own properties, your ID is what name, which is the pojo, so that, in the construction of the project, I need, I directly in this cup to find , found this I want the bean, I can use it, I do not, just throw him away, so that the relationship between class and class from the previous, each other, and become the present, I want you to come over, I do not you you go a little farther.

  I will not write about how to get this bean later On. And then say, if you want to know more about dependency injection, what books do you read? It is estimated that the Author's advertising costs are not reduced.

1.1.3 Applied Facets

Facets are a magical thing, the shorthand for Cutting-plane programming is aop, and the function of this AOP is to make you more focused on your business, extracting things that are reusable (that is, repeating multiple times), such as logs, transactions, and Security. Anyway the book is written in this way, the code just write once, and then which way I would "cut the past."

The following are some concrete implementations of aop, and I use the examples in the book to Understand:

  the Book said that there is a knight, every day win-win, very good, so in order to praise the knight, there appeared a poet, every time the Knight win the method of winning the poet will sing it again, play before the praise once again, after the fight to come Again. As can be seen here, our main business is the Knight war, this praise and war on the wool thing, so I took the praise of this method extracted, configured in the Spring XML(cup), and in the cup, the rules, every time I call the Knight of this method you must sing Again. This is aop, he cut a business class into a lot of pieces, there are many nouns in the back, and then praise this method you can see where you can plug in, plug Where.

Specific configuration Slightly.

1.1.4 using templates to eliminate code

There is no substance in this verse, it is blowing spring so Well. And then draw the key to the next chapter

1.2 To accommodate your bean

This chapter repeats what I just said: spring is a container that can hold a lot of bean blabla ...

1.2.1 and Application Context official

Spring comes with several types of application contexts (tell me which spring container (cup) you are Using)

1.ClassPathXmlApplicationContext---load the context definition from the XML configuration file under the Classpath and use the application context definition as a class resource.

2.filesystemxmlapplicationcontext--reads the XML configuration file under the file path and loads the context Definition.

3.xmlwebapplicationcontext--reads the XML configuration file under the Web application and loads the context Definition.

And then said with what method how to load configuration file, slightly.

The life cycle of the 1.2.2 Bean

The traditional bean life cycle is simple, when the Java keyword New starts to instantiate the bean, and once the bean is unused, it is garbage collected by Java.

The life cycle of spring is more complex, as Follows:

1. Spring to instantiate the bean

2. Spring injects the value and the bean reference into the Bean's corresponding attribute (for example, into a method or attribute of a dependent Bean)

3. If the bean implements the Beannameaware interface, the Bean's ID will be passed into the Setbeanname () method

4. If the bean implements the Beanfactoryaware interface, a reference to the Bean's Beanfactory is passed into the Setbeanfactory () method

5. If the bean implements the Applicationcontextaware interface, spring invokes a reference to the context of the Setapplicationcontext () incoming bean

6. If the bean implements the Beanpostprocessor interface, spring will call the Bean's properties before initializing the Preprocessing method Postprocessbeforeinitialization () method

7. If the bean implements the Initializingbean interface, Spring will call the Afterpropertiesset () method. If the Bean declares the initialization method using init-method, The change method is also called

8. If the bean implements the Beanpostprocessor interface, spring will call postprocessafterinitialization

9. Now that the bean is ready to be used by the application, it will reside in the application context until the application context is destroyed

10. If the Bean implements the Disposablebean interface, spring will call his destroy () method, and the method will be called if Destroy-method is Used.

(these are transcription, these content involves a lot of source-related things, a time really can not digest)

1.3 Overlooking the spring landscape

This introduces Spring's jar package, which describes the other members of spring

1.3.1 Spring Module

Here are some of the Spring's modules, and here we start with a brief introduction

  Core Spring Container

  This is the core of spring, where he is responsible for the creation, configuration, and management of beans in spring, which also provides many enterprise-class services such as mail, Jndi access, EJB inheritance, and Scheduling.

so, all of Spring's modules are on that basis, and these classes are actually used in the configuration Application. The corresponding should be Spring's core package

  Spring's AOP Module

  is to use AOP to import the package, nothing to say

 Access and integration of data

  Do you have a feeling that when dealing with the database to write a lot of template code, such as to get a database connection, create statements, processing result set blabla,spring this magical thing and solve the problem, he put these template code are all packaged up, it is important! Also encapsulates a lot of exception information (here in the back of the detailed, now mention), anyway, is to emphasize the spring Dafa good!

 Web and Remote Calls

  This is an MVC design pattern, so I introduced some other frameworks what Strus2 ah jsf ah et cetera (i've only used Struts2 but seems to be eliminated), and that's always the way to integrate with many popular MVC frameworks to represent Spring's loose coupling.


  Spring provides some implementation of mock objects for testing this block (which is supposed to be the mock, pretending to have created an object, not knowing that it's Right)

1.3.2 Spring Portfolio

Don't know what this is .... I can't read it.

1.4 Spring New features

i'll just copy and paste it.

There is sping Portfollo new features not listed, because I wrote this blog when I do not know what this is .... I hope I can add it later.

1.5 Summary (pick the key Transcription)

The 1.Spring facilitates code loose coupling. The key to success is dependency injection and AOP

2. Dependency injection is a way of assembling an application object without needing to know where the dependency is coming from or how it is Dependent.

3. Unlike their own access to dependent objects, dependent objects give them the objects they have Been.

4. Dependent objects usually only understand the injected object through the interface, thus guaranteeing loose coupling.

5.AOP can help the app bring together the logic That's scattered everywhere-facets

6. Dependency Injection and AOP are the core parts of the Framework.


Spring Combat (third Edition) Tour (part one)

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.