Servlet calls the bean in the spring container. There are two methods: annotation and xml configuration.

Source: Internet
Author: User

Recently, due to the servlet calling Spring bean in the project, all the beans in the project are completed in Annotation mode, such as @ service, @ repository, and @ resource, however, spring container management does not recognize servlets and filters, so it cannot be referenced using annotations. After checking the information on the Internet, you can see the following code:
Method 1: instantiate the bean in the init method of servlet. After initialization, you can call the method in bean in servlet.

  1. Webapplicationcontext cont = webapplicationcontextutils. getrequiredwebapplicationcontext (config. getservletcontext ());
  2. TS = (testservice) cont. getbean ("testservice") // ts is the declared class variable. By default, the name in the brackets is the class name of the bean, and the first letter is lowercase. You can also set a unique name, for example, @ Service (value = "testservice ")

Copy code

Method 2: Get it directly in the servlet dopost method. The Code is as follows:

  1. Webapplicationcontext cont = webapplicationcontextutils. getrequiredwebapplicationcontext (request. getsession (). getservletcontext ());

Copy code

However, in my project, no bean can be obtained using any of the above methods, because the above two methods are only valid for the bean configured in XML, and the annotation bean cannot be obtained, finally, I saw an article on "Spring Processing source code analysis for annotation (annotation)-scanning and reading bean definitions" on the Internet, and finally solved this problem. The Code is as follows:
Code 1: Service Interface

  1. Package com. Test. Web. Service;

  2. Public interface itestservice {
  3. Public void test (); // Test Method
  4. }

Copy code

Code 2: implement interfaces and use annotations

  1. Package com. Test. Web. Service. impl;

  2. Import org. springframework. stereotype. Service;

  3. Import com. taokew.web. Test. itestservice;
  4. // The annotation section here provides a unique name, such as @ Service (value = "testserviceimpl"), which is equivalent to the bean ID in xml configuration.
  5. @ Service
  6. Public class testserviceimpl implements itestservice {

  7. @ Override
  8. Public void test (){
  9. System. Out. println ("test printing ");
  10. }

  11. }

Copy code

Code 3: Obtain the annotation bean in the servlet and call its test method

  1. Annotationconfigapplicationcontext CTX = new annotationconfigapplicationcontext ();
  2. CTX. Scan ("com. Test. Web. Service .*");
  3. CTX. Refresh ();
  4. Testserviceimpl service = CTX. getbean (testserviceimpl. Class); // you can also use CTX. getbean ("testserviceimpl") here ")
  5. Service. Test ();

Copy code

In this way, you can call Spring annotation bean in servlet or filter, in fact, the whole process is to simulate springmvc to scan the component package during initialization, complete the registration of all beans, and store them in the Management container. If you have a better solution, I hope you can give me some advice!

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.