spring和springmvc注解的补充(@Autowired@Resource@Service,@repository,@Component)


注解和注释以及Spring和SpringMVC常用的注解

SpringMVC 常用注解

springmvc常用注解标签详解

 

使用注解都需要在xml配置中增加如下标签配置:

<!-- 可以扫描controller、service、...
    这里让扫描controller,指定controller的包
     ps: 该标签让指定包下面的类(拥有注解标识的类)注册到容器-->
    <context:component-scan base-package="cn.itcast.ssm.controller"></context:component-scan>    

 

Spring注解@Resource和@Autowired区别对比

Spring 注解配置(2)——@Autowired

@Resource和@Autowired区别

@Resource 默认按照名称方式进行bean匹配,
@Autowired默认按照类型方式进行bean匹配
@Resource(import javax.annotation.Resource;)是J2EE的注解,
@Autowired(import org.springframework.beans.factory.annotation.Autowired;)是Spring的注解

共同点

两者都可以写在字段和setter方法上。
两者如果都写在字段上,那么就不需要再写setter方法
(ps: 修饰属性都表示注入该属性对象)

 

SpringMVC常用注解@Controller,@Service,@repository,@Component一般都标记在类上

@Controller,@Service,@repository,@Component区别

(ps:修饰类都表示该类加入容器)

1、@controller 控制器(注入服务)
2、@service 服务(注入dao)
3、@repository dao(实现dao访问)
4、@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)

注意:除了1有特别的意义,234的注解主要mvc分层(1234都具有对象注册到spring容器的作用)

在Annotaion配置注解中用@Component来表示一个通用注释用于说明一个类是一个Spring容器管理的类。
即该类已经拉入到Spring的管理中了。
而@Controller, @Service, @Repository是@Component的细化,这三个注解比@Component带有更多的语义,
它们分别对应了控制层、服务层、持久层的类。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM