@configuration @Component @Repository @Controller

@configuration注解标注在类上,相当于把该类作为Spring的xml配置文件中的<beans>,使用注解来代替Spring中的bean配置

下面是@configuration定义:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {

   /**
    * Explicitly specify the name of the Spring bean definition associated
    * with this Configuration class.  If left unspecified (the common case),
    * a bean name will be automatically generated.
    * <p>The custom name applies only if the Configuration class is picked up via
    * component scanning or supplied directly to a {@link AnnotationConfigApplicationContext}.
    * If the Configuration class is registered as a traditional XML bean definition,
    * the name/id of the bean element will take precedence.
    * @return the specified bean name, if any
    * @see org.springframework.beans.factory.support.DefaultBeanNameGenerator
    */
   String value() default "";

}

@Configuration可理解为用spring的时候xml里面的<beans>标签

@Bean可理解为用spring的时候xml里面的<bean>标签

从上面@configuration定义看,@configuration也是@Component的扩展,

@Component :定义Spring管理Bean

@Repository 是@Component扩展,通过注解在Dao层的类

@Service @Component扩展,通常注解在Service层实现的类。

@Controller:是@Component扩展,通常注解在表现层web层。

这些注解的的作用:

为了避免使用大量Xml配置的方式来注入Bean,Spring有自动扫描注入的方式,上面注解的类会自动被纳入Spring容器管理中,

通过<context:component-scan base-package=”com.mmnc”>    扫描组件





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值