@Configuration :标记当前类是配置类
@Bean:将当前方法创建类 并放入容器中,相当与bean.xml种的<bean>
通常:@Component 等价于 @Configuration + @EnableConfigurationProperties
在springboot 将类放置在容器中
1.常用的注解@Component @Service ..
2.通过 @Configuration & @Bean
@Configuration// 标记当前类 是配置类 相当于 bean.xml
public class Config {
@Bean// 将当前方法创建类 放入到容器中 相当于 bean.xml 中的 <bean>
public StudentService getStudentService(){
return new StudentServiceImpl();
}
}
@RunWith(SpringRunner.class)
@SpringBootTest //启动一个springboot测试
public class StudentTest {
@Autowired
public StudentService studentService;
@Test
public void studentServiceTest(){
System.out.println(studentService.findAllStudent());
}
}