Spring中Bean的定义及做用域的注解实现

Classpath扫描与组件管理: java

从Spring3.0开始,Spring JavaConfig项目提供了不少特性,包括使用java而不是xml定义bean,指的是注解 正则表达式

@Configuration,@Bean ,@Import ,@DependsOn spring

@Component是一个通用注解,可用于任何bean express

@Repository:一般用于注解DAO类,即持久层 this

@Service:一般用于注解Service类,即服务层 spa

@Controller:一般用于Controller类,即控制层MVC .net

元注解(Meta-annotations) 代理

元注解即注解的注解,许多Spring提供的注解能够做为本身的代码,即"元数据注解",元注解是一个简单的注解,能够应用到另外一个注解 code

下列定义Service注解时,用@Component注解来修饰,@Service拥有@component注解的功能: component

@Target(ElementType.TYPE) 
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component  // Spring will see this and treat @Service in the same way as @Component
public @interface Service {
    // ....
}

咱们也能够本身定义注解.

类的自动检测及Bean的注册

Spring能够自动检测类并注册Bean到ApplicationContext中.

例:@Service,@Component,@Repository要注册到类上(类的注解),还有注册在方法上的注解像@Autowired,这些注解能够被自动检测到的

注册在类上的,则能够做为Bean自动注册到ApplicationContext中去

为了可以检测这些类并注册相应的Bean,须要在xml文件中配置下面内容:

<context:component-scan base-package="org.example" />

自动扫描org.example包下面的类

<context:component-scan>包含<context:annotation-config>,一般在使用前者后,就再也不使用后者.由于使用前者后,已经包含后者的所有功能.一般使用前者

使用过滤器进行自定义扫描

默认状况下,类被自动发现并注册bean的条件是:使用@Component,@Repository,@Service,@Controller注解或者使用@Component注解的自定义注解

能够经过过滤器修改上面的行为.

如:忽略全部的@Repository注解并用"Stub"代替

<beans>
    <context:component-scan base-package="org.example">  
        <context:include-filter type="regex" expression=".*Stub.*Repository" />
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />
    </context:component-scan>
</beans>

type类型:annotation基于注解,assignable基于类或接口,aspectj基于aspectj,regex基于正则表达式,custom基于自定义

还可以使用use-default-filters="false"禁用自动发现与注册

定义Bean

扫描过程当中组件被自动检测,那么Bean名称是由BeanNameGenerator生成的(@Component,@Repository,@Service,@Controller都会有个name属性用于显示设置Bean Name)

@Service("myMovieLister")
public class SimpleMovieLister {
    // ....
}

也能够本身生成Bean名称,Bean名称为类名的第一个字母小写.

也能够自定义bean命名策略,实现BeanNameGenetator接口,并必定要包含一个无参数构造器

<beans>
    <context:component-scan base-package="org.example" name-generator="org.example.MyNameGenerator" />
</beans>

name-generator="org.example.MyNameGenerator"
指定命名规则的实现

做用域

可用注解@Scope来指明做用域

也能够自定义scope策略,实现ScopeMetadataResolver接口并提供一个无参构造器

<beans>
    <context:component-scan base-package="org.example" scope-resolver="org.example.MyNameGenerator" />
</beans>

代理方式

可使用scoped-proxy属性指定代理,有三个值可选:no,interfaces,targetClass

<beans>
    <context:component-scan base-package="org.example" scoped-proxy="interfaces" />
</beans>
相关文章
相关标签/搜索