How to get ApplicationContext objects

Source: Internet
Author: User

Originally, this is a relatively simple question.

However, a problem arose in the previous event.

Problem Description: The number of database connections only increased, until the number of database links is full, reported a too many connection exception. The entire service is hung up and needs to be rewritten to start once before it can be used.

At first, I thought it was the DAO layer. The link was not released. The DAO layer code is checked again, but also from the Internet to search a lot about database connection pool, but also did not find the problem. So it's tangled up ~ ~

Backstage through a command of MySQL, status, found in the job candidate resume editing module, each edit resume, the database connection number will only increase, add three (this number is related to a database connection pool parameters, using the DBCP link pool), so then the content of the resume editing module checked again , and finally found the culprit.

At the time of the resume editing, Freemarker was used to regenerate the static page of the CV. However, the business layer of the Freemarker module does not use @resource, which is given to the spring container for management. There is a line of code in order to get to a DAO layer of beans, using the

ApplicationContext ac = new Classpathxmlapplicationcontext (file file);

Ac.getbean (Class clazz);

This code, believe that a lot of people, see this code has been roughly guessed the problem lies.

ApplicationContext ac = new Classpathxmlapplicationcontext (file file), this code is equivalent to re-initializing the spring container again. So ... '

The problem is solved, so summarize the lessons. How do you get to the bean correctly?

1, in general, we use the spring container to manage the bean, you can directly use the injected way to get to the bean

For example: Use @resource, @Autowire.

2, if a bean is not given to the spring container to manage, we should be in the Web project if we get to this bean?

Way one: is also the wrong way, is to use

ApplicationContext ac = new Classpathxmlapplicationcontext (file file);

Ac.getbean (Class clazz);

The drawbacks of this approach, as described above, are generally used for unit testing and cannot be used directly in Web projects.

Mode two: Use

ApplicationContext ac1 = webapplicationcontextutils.

Getrequiredwebapplicationcontext (ServletContext SC)
ApplicationContext AC2 = webapplicationcontextutils.

Getwebapplicationcontext (ServletContext SC)
Ac1.getbean (Beanid);
Ac2.getbean (Beanid); Description:

                                

                                Description: The difference between the above two tools is that the former throws an exception when it gets failed, and the latter returns null.

             These kinds of ways may make us more commonly used. For other ways, refer to

http://blog.sina.com.cn/s/blog_9c7ba64d0101evar.html The deep blog of the star cable.




This article is from the "Unruly Wind" blog, please be sure to keep this source http://fengcl.blog.51cto.com/9961331/1745777

How to get ApplicationContext objects

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.