在写ssm整合的时候遇到的一个关于@Autowired小问题
在测试的时候发现BookServiceImpl 没有被注入,报错java.lang.NullpointerException错误
public class TestMapper {
@Autowired
private BookServiceImpl bookService;
@Test
public void test1() throws IOException {
Books bookById = bookService.getBookById(1);
System.out.println(bookById);
}
java.lang.NullPointerException
at com.yan.service.TestMapper.test1(TestMapper.java:44)
看了下idea提示,Autowired members must be defined in valid Spring bean (@Component|@Service|...),大概意思是必须是当前bean已经在IOC中,bean中的成员才能被填充
解决方案:
测试的时候把测试类放入IOC中,便能填充成功
@Component
public class TestMapper {
@Autowired
private BookServiceImpl bookService;
@Test
public void test1() throws IOException {
Books bookById = bookService.getBookById(1);
System.out.println(bookById);
}


