In Spring annotations, ordinary classes get methods or bean objects marked with @Service

Using the Spring framework, we don't need to create objects of classes, all of them are created by the Spring container and injected through annotations. The principle of injection is that when the program starts, Spring scans the class according to the path configured in the xml. If it finds that there are similar @Service and @Controller above the class, it will locate the current class, and then mark the current class with Annotated properties are injected so that we can use the properties to call methods.

So how do ordinary classes use the methods marked with @Service?

1. If you want to use @autowired, the class itself should also be under the management of spring, that is, your UserLogUtil should also be marked as a component (or Service), so that spring knows to inject dependencies; 
2. If not If it is marked as @Component, the dependency cannot be injected through @autowired at this time, and the class marked as Service can only be obtained through ApplicationContext: 
UserLogService service = ApplicationContext.getBean(UserLogService.class);

So how to get the ApplicationContext in the web project

1.

ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc)

 2.

ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)

 Note: As for obtaining the ServletContext object, it can be obtained from request and session, they all have the getServletContext() method

3 Write a tool class to implement the ApplicationContextAware interface and add this to the spring container (recommended)

package com.ylcf.before.ylcf.base.sys.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
 * @author zhengyunfei
 * @create 2018-02-08 - 21:34pm
 **/
public class SpringContextUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static ApplicationContext  getApplicationContext(){
        return applicationContext;
    }

    public static Object getBean(String beanName){
        return applicationContext.getBean(beanName);
    }

    public static Object getBean(Class c){
        return applicationContext.getBean(c);
    }
}
 

Then register this bean in the spring container and add the following code to the spring configuration file

<bean id="springContextUtil" class="com.ncut.ssm.util.SpringContextUtil"></bean>

 

Finally, in a normal class, you can call it like this

ApplicationContext appCtx = SpringContextUtil.getApplicationContext();
UserService bean = (UserService)SpringContextUtil.getBean("UserService");

or

ApplicationContext appCtx = SpringContextUtil.getApplicationContext();
UserService bean = (UserService)SpringContextUtil.getBean(UserService.class);

The first case applies to the name of the bean annotated with @Service(""UserService" ")

@Service("UserService")
public class UserServiceImpl implements UserService {

The second case applies when there is nothing behind @Service

@Service
public class UserServiceImpl implements UserService {

In this case, UserServcieImpl.class cannot be used, but its interface class UserService.class should be used. Because UserServiceImpl has not been injected by other classes, it will report that this class cannot be found.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326245136&siteId=291194637
Recommended