SpringBoot在普通类中注入Bean
(2019-03-26 09:22:55)
标签:
springboot在普通类中注入bean |
分类: 软件 |
一:Spring Boot在普通类中注入Bean
1:在Controller Service 等中,直接@ Autowired注入bean就好。
但在普通类中这样注入会提示null。对于此类的bean的注入,可以implements ApplicationContextAware。
如下面:调用方式为:UserService userservice = (UserService) SpringContextUtils.getBean(UserService.class);
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextUtils implements ApplicationContextAware {
}