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