在spring mvc 模式中,Service层的类被非Service或者非Controller层的代码调用后,是不能直接使用的。比如在工具类或者配置类中如果需要调用Service层的代码,则需要在该类上添加@Component 注解。然后使用以下写法:
@Autowired
private XXService xxService;
private static xxService service;
public @PostConstruct void init(){
service= xxService;
}
然后在方法中直接使用静态变量service去调用类中的方法。