使用@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();
}