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

本文详细介绍了在SpringMVC框架下,如何在非Service或非Controller层正确调用Service层的方法。强调了需要使用@Component注解,并通过@Autowired和@PostConstruct注解初始化静态变量来实现跨层调用。

被折叠的 条评论
为什么被折叠?



