1、@ServletComponentScan
- 在SpringBoot的main方法上使用@ServletComponentScan注解后,Servlet、Filter、Listener可以直接通过@WebServlet、@WebFilter、@WebListener注解自动注册,无需其他代码。
2、@ComponentScan
-
包扫描会扫描只要标注了@Controller,@Service,@Repository,@Component这四个注解都会被扫描到容器中。
-
注解包扫描会覆盖掉@SpringBootApplication的包扫描,SpringBootApplication默认扫描的是自己,如果使用@ComponentScan注解需要加入自己的包名,扫描范围更大
3、@Configuration
- 说明这是个配置文件,和原来xml配置是等效的,只不过现在用java代码进行配置了 加上一个@Configuration注解就行了
例如:
- @Configuration:提到@Configuration就要提到他的搭档@Bean。使用这两个注解就可以创建一个简单的spring配置类,可以用来替代相应的xml配置文件。
- @Configuration的注解类标识这个类可以使用Spring IoC容器作为bean定义的来源。
- @Bean注解告诉Spring,一个带有@Bean的注解方法将返回一个对象,该对象应该被注册为在Spring应用程序上下文中的bean。
@Configuration
public class Conf {
@Bean
public Car car() {
Car car = new Car();
car.setWheel(wheel());
return car;
}
@Bean
public Wheel wheel() {
return new Wheel();
}
}
4、@SpringBootApplication
- (默认属性) @Configuration + @EnableAutoConfiguration + @ComponentScan
网友评论