不选择依赖,直接创建。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
package com.lijw.springboottest;
import org.springframework.stereotype.Service;
@Service
public class UserService {
public String findAll(){
return "findAll";
}
}
“注意:要将 UserService 等类写在
package com.lijw.springboottest
下,如果写到其他包,例如:com.lijw.service ,那么默认 Bean 扫描不到。 ”
package com.lijw.springboottest;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest(classes = SpringbootTestApplication.class)
public class UserServiceTest {
@Autowired
UserService userService;
@Test
public void testFindAll(){
System.out.println(userService.findAll());
}
}