How Java backend code calls spring's @service bean

Source: Internet
Author: User

For example: In my project, there is a class compassindexoperationto:
@Service ("Compassindexoperation")

@Transactional

Method notifies Spring to create an instance of an implementation class;

and the file folder of the generated Bean is set up in the Spring configuration xml file , and my Project instance is:

< Context:component-scan base-package="com.ourfuture.compass.*"/>

Such When Project starts. A beanis generated from a class that is marked @Service , etc. under the development folder.

The way to invoke this bean in the background Java code is as follows:

ApplicationContext context = Webapplicationcontextutils.getwebapplicationcontext

(Servletactioncontext.getservletcontext ());

Compassindexoperation compassindexoperation = (compassindexoperation) context.

Getbean ("compassindexoperation");

This gives you an instance of the Compassindexoperation bean in spring, which is, of course, the bean in my project. For different project, to get a different bean instance,

Just change the position of the above compassindexoperation to the name of the corresponding bean.

Among the packages that need to be imported are:

Import Org.springframework.context.ApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;

How Java backend code calls spring's @service bean

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.