使用@Resource 和@Autowired注入属性为null
在service层中注入其它的service接口或者mapper接口都是可以的
但是在封装的Utils工具类中或者非controller普通类中使用@[email protected]注解注入Service或者Mapper接口,直接注入会出现问题,因为Utils使用了静态的方法,我们是无法直接使用非静态接口的
@Autowired注解的方法:
@Component
public class TestUtils {
@Autowired
private ItemService itemService;
@Autowired
private ItemMapper itemMapper;
public static TestUtils testUtils;
@PostConstruct
public void init() {
testUtils = this;
}
}
//utils工具类中使用service和mapper接口的方法例子,用'testUtils.xxx.方法' 就可以了
public static void test(Item record){
testUtils.itemMapper.insert(record);
testUtils.itemService.queryAll();
}