Spring-framework

时间:2021-06-08 20:24:59

1、spring注解驱动开发 官方文档

  • @Configuration 告诉spring这是一个配置类,配置类=配置文件
  • @Bean 给容器中注入一个bean,类型为返回值类型,id默认用方法名作为id
@Bean
public Person person(){
       return new Person("Jack",22);
     }
  • 获取bean
ApplicationContext applicationContext = new  AnnotationConfigApplicationContext(MainConfig.class);
Person person = applicationContext.getBean(Person.class);
System.out.println("person:"+person);
  • 获取容器中bean数组:String[] names = applicationContext.getBeanDefinitionNames();
  • @ComponentScan(value = "com.hbut") 包扫描器,指定要扫描的包。配置在配置类上面。
  @ComponentScan(value = "com.hbut",excludeFilters = {@ComponentScan.Filter(type =FilterType.ANNOTATION,classes = {Controller.class, Service.class})})  //包扫描,按注解排除,排除Controller和service
  @ComponentScan(value = "com.hbut",includeFilters = {@ComponentScan.Filter(type =FilterType.ANNOTATION,classes = {Controller.class, Service.class})},useDefaultFilters =false) // 包扫描,只需要service和controller