@Component// 将工具类声明为spring组件,这个必须不能忘
public class TestUtils {
@Autowired
private ItemService itemService;
@Autowired
// private ItemMapper itemMapper;
// 静态初使化当前类
public static TestUtils testUtils;
// 在方法上加上注解@PostConstruct,这样方法就会在Bean初始化之后被Spring容器执行(注:Bean初始化包括,实例化Bean,并装配Bean的属性(依赖注入))。
@PostConstruct
public void init() {
testUtils = this;
}
// utils工具类中使用service或mapper接口的方法例子,用"testUtils.xxx.方法" 就可以了
public static void test(Item record) {
// 调用service的方法
testUtils.itemService.insert(record);
}
}
七、Springboot项目中,非Controller使用@Autowired注解 service注入为null的问题
猜你喜欢
转载自blog.csdn.net/panchang199266/article/details/86651499
今日推荐
周排行