Use spring on the kitchen, roof, and grass

Source: Internet
Author: User
Tags constructor tomcat
Spring is more powerful and has to face the problem of coming--because spring is not a weblogic, tomcat-like top-level container, and the servlet and EJB objects are not created by it, so it must descend to the WebLogic, Tomcat-side.

Beginners generally do not have to tube so much, according to Spring+hibernate+struts, such as sample to do, but slowly, perhaps will start in the Jsp+javabean system, Home-made framework, Singleton class and other environments under the use of spring.

The 3rd chapter of the Professional Java Development with the Spring Framework says "Managing the Containe" section. Generally can be divided into direct call system and IOC fashion two categories.

1. Direct summoning of the application context of the Department--singleton

The simplest, as in unittest, directly constructs the application context:

ApplicationContext CTX = new Classpathxmlapplicationcontext ("Applicationcontext.xml");


In the Web environment, the servlet context is pressed after using Contextloader constructs ApplicationContext.

Done by Contextloaderlistener or Contextloaderservlet when the Web application is started.

Then in Jsp/servelet, the Applicationcontext:applicationcontext context can be obtained through the servlet Webapplicationcontextutils.getwebapplicationcontext (application);

However, like the Singleton class or EJB, there is no servlet context available.

If all were constructed directly like UnitTest, the speed would be unbearable. Naturally, it is thought of making applicationcontext into a single case.

Spring provides objects such as Contextsingletonbeanfactorylocator.

Start with a beanreffactory.xml that writes all the applcationcontext-*.xml filenames and names the context as "Default-context":



applicationcontext.xml



Then let Loactor to find it, but the code is a little long:

Beanfactoryreference BFR = Defaultlocatorfactory.getinstance (). Usebeanfactory ("Default-context");
Beanfactory factory = Bfr.getfactory ();
MyService MyService = Factory.getbean ("MyService");
Bfr.release ();
Now use MyService


The code above is too flexible and too cumbersome.

You might as well implement a simple singleton, extend the Contextloaderlistener class, and press Singleton when the Web system starts.

The new Contextloaderlistener class overload is as follows, ContextUtil contains a static ApplicationContext variable:

public void contextinitialized (Servletcontextevent event)
{
Super.contextinitialized (event);

ServletContext context = Event.getservletcontext ();
ApplicationContext CTX = webapplicationcontextutils.getrequiredwebapplicationcontext (context);
Contextutil.setcontext (CTX);
}


Use home can be directly used:

ApplicationContext context = Contextutil.getcontext ();


2.IoC Fashion

If all the places use the direct summoning system, it's the slap in the face of Rod. Because he's always opposed to the code being deeply coupled with the framework.

So, the better approach is to write some glue code, base class to complete spring, without the application code perceiving the presence of the spring application context.

However, because the structure of each frame is different, rod is not able to speak a common method of integration, so we recommend that you try to learn the various integrated frameworks, such as spring MVC, struts of various ways, write their own simple integration code.

Only the uncertain invocation of certain singleton classes is not suitable for the premature IOC, and the direct summoning system can be used.

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.