@Service 用于注解服务
@Component 用于注解元数据
@Controller 用来注解controller
@Repository 用来注解持久层 同时其能自动转换异常
元注解:一种可用于别的注解之上的注解,例如@Service注解是使用@Component元注解的
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component //元注解
public @interface Service {
String value() default "";
}
元注解可以进行组合,进而创建组合注解,例如 @RestController 注解是有 @Controller和@ResponseBody组成的。
自动探测并注册bean定义
Spring可以自动检测各代码中被注解的类,并使用ApplicationContext内注册相应的BeanDefinition例如,以下两个类就可以被自动探测
@Service
public class SimpleMovieLister {
private MovieFInder movieFInder;
@Autowired
public SimpleMovieLister(MovieFInder movieFInder) {
this.movieFInder = movieFInder;
}
}
但是想要自动检测这些类并注册相应的bean,需要在@Configuration配置中添加@ComponentScan注解,其中basePackages属性是两个类的父包路径(或者可以指定包含每个类的父包的逗号/分号/空格分隔符列表)
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
.....
}
在自定义扫描中使用过滤器
默认情况下,使用@Component @Repository @Service @Controller 注解的类或者注解为@Component的自定义注解类才能被检测为候选组件,但是,开发者可以通过应用自定义过滤器来修改和扩展此行为,将他们添加为@ComponentScan注解的includeFilters或excludeFilters参数,每个filter元素都需要包含type和expression属性,下班介绍了过滤选项
过滤类型 表达式例子 描述
annotation(默认) org.example.SomeAnnotation 目标组件类级别的注解
assignable org.example.SomeClass 目标组件继承或实现的类或接口
aspectj org.example..*Service+ 用于匹配目标组件的AspecJ类型表达式
regex org\.example\.Default.* 用于匹配目标组件类名的正则表达式
custom org.example.MyTypeFilter MyTypeFilter接口的自定义实现
以下示例显示了忽略所有@Repository注解并使用带有“Stub”以及"Repository"标记的类名
@Configuration
@ComponentScan(
basePackages = "com.example",
includeFilters = @Filter(type = FilterType.REGEX, pattern = ".*Stub.*Repository"),
excludeFilters = @Filter(Repository.class))
public class AppConfig {
.....
}
在组件中定义bean的元数据,
在@Component与@Configuration对于@Bean的处理是不一样的
在@Component中不会使用CGLIB增强去连接方法和属性调用。
在@Configuration注解的类中,@Bean注解创建的Bean对象会使用CGLIB代理对方法和属性进行调用,方法的调用不是常规的Java语法,而是通过容器来提供通用的生命周期管理和d代理Spring bean,甚至在通过编程的方式调用@Bean方法时也会产生对其它bean的引用,相比之下,在一个简单的@Component类中调用@Bean方法中的方法或字段具有标准的Java语义,没有用到特殊的CGLIB处理或者其他约束。
开发者可以将@Bean方法申明为静态的,并允许在不将其包含的配置类作为实例的情况下调用他们,这在定义后置处理器bean时是有特别的意义的。例如BeanFactoryPostProcessor,因为这类bean会在容器的生命周期前被初始化,而不会触发其他部门的配置。
对静态@Bean方法的调用永远不会被容器拦截,即使是在@Configuration类内部,这是因为CGLIB的子类代理只会重写非静态方法。
单个类,可以为同一个bean保存多个@Bean方法,例如根据运行时可用的依赖关系选择合适的工厂方法。
命令自动注册组件
默认情况下,各代码层注解所包含的name值,将会作为相应的bean定义的名字,比如SimpleMovieLister的默认名称为simpleMovieLister 注意点就是如果不想依赖默认的bean命名策略,那么可以提供一个自定义bean命名策略,首先,实现BeanNameGenerator接口,并确保包括一个默认的无参构造函数,然后在配置扫描程序时提供完全限定类名。如下:
public class MyNameProx implements BeanNameGenerator {
@Override
public String generateBeanName(BeanDefinition beanDefinition, BeanDefinitionRegistry beanDefinitionRegistry) {
System.out.println("s" + beanDefinition.getBeanClassName());
return "s" + beanDefinition.getBeanClassName();
}
}
指定命名策略
@SpringBootApplication
@ComponentScan(
basePackages = "com.test", nameGenerator = com.test.entity.MyNameProx.class
)
public class FeginApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(FeginApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(FeginApplication.class, args);
}
}
使用测试
@Component
public class ComplexBean {
private Integer age;
private String name;
private String birthday;
}
@Autowired
@Qualifier("scom.test.entity.ComplexBean")
private ComplexBean complexBean;
为component-scan组件提供作用域
与Spring管理的主键一样,默认的是最常见的作用域,单例(singleton)然而,有时会需要用到其它作用域,则可以通过@Scope注解来指定,只需要在注解中提供作用域的名称。
要想提供自定义作用域的解析策略,而不是依赖基于注解的方法,那么需要实现ScopeMetadataResolver接口,并确保包含一个默认的无参构造函数,然后在配置扫描程序时提供完全限定类名。
如下:
@Configuration
@ComponentScan(basePackages = "org.example",
scopeResolver = MyScopeResolver.class)
public class AppConfig {
}
当使用某个非单例作用域时,为作用域对象生成代理可能非常必要,出于这个目的 component-scan元素添加了scope-proxy属性,他的值有三个选项:no,interfaces和targetClass
例如下面的配置将会生成标准的JDK动态代理:
@Configuration
@ComponentScan(basePackages = "org.example",
scopedProxy = ScopedProxyMode.INTERFACES)
public class AppConfig {
}
为注解提供Qualifier元数据
当依靠类路径扫描并自动检测组件时,可以在候选类上提供具有类型级别注解注解的限定符元数据。
@Component
@Qualifier("Action")
public class AppConfig {
}
注意:与大多数基于注解的替代方法一样,注解元数据绑定到类定义本身,而在使用XML配置时,允许同一类型的beans在Qualifier元数据中提供变量,因为元数据是依据实例,而不是类来提供的。