@Component// 1、将工具类声明为spring组件,这个必须不能忘

public class TestUtils {
//2、自动注入
@Autowired

private ItemService itemService;

 

// 3、静态初使化当前类

public static TestUtils testUtils;

 

// 4、在方法上加上注解@PostConstruct,这样方法就会在Bean初始化之后被Spring容器执行(注:Bean初始化包括,实例化Bean,并装配Bean的属性(依赖注入))。

@PostConstruct

public void init() {

testUtils = this;
testUtils.itemService = this.itemService;

}

 

// 5、utils工具类中使用service或mapper接口的方法例子,用"testUtils.xxx.方法" 就可以了

public static void test(Item record) {

// 6、调用service的方法

testUtils.itemService.insert(record);

}

}

 

相关文章:

  • 2021-09-15
  • 2022-02-03
  • 2022-12-23
  • 2021-06-03
  • 2022-12-23
  • 2021-11-28
猜你喜欢
  • 2021-10-10
  • 2021-07-29
  • 2022-03-09
  • 2022-01-09
  • 2022-03-06
  • 2022-12-23
  • 2021-05-28
相关资源
相似解决方案