SpringMvc三层架构注解详解@Controller@Service和@Repository

Posted 虎子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringMvc三层架构注解详解@Controller@Service和@Repository相关的知识,希望对你有一定的参考价值。

@Autowired依赖注入源码分析

1. @Controller控制层

@Controller用于标记在一个类上,使用它标记的类就是一个SpringMvc Controller对象,分发处理器会扫描使用该注解的类的方法,并检测该方法是否使用了@RequestMapping注解。
@Controller只是定义了一个控制器类,而使用@RequestMapping注解的方法才是处理请求的处理器。
@Controller标记在一个类上还不能真正意义上说它就是SpringMvc的控制器,应为这个时候Spring还不认识它,这个时候需要把这个控制器交给Spring来管理。有两种方式可以管理:
<span style="color:#000000"><span style="background-color:#f3f4f5"><code class="language-xml"><span style="color:#969896"><!--基于注解的装配--></span>
<span style="color:#969896"><!--方式一--></span>
<span style="color:#63a35c"><span style="color:#63a35c"><span style="color:#969896"><</span>bean</span> <span style="color:#795da3">class</span><span style="color:#df5000"><span style="color:#969896">=</span><span style="color:#969896">"</span>com.HelloWorld<span style="color:#969896">"</span></span><span style="color:#969896">/></span></span>
<span style="color:#969896"><!--方式二--></span>
<span style="color:#969896"><!--路径写到controller的上一层--></span>
<span style="color:#63a35c"><span style="color:#63a35c"><span style="color:#969896"><</span>context:component-scan</span> <span style="color:#795da3">base-package</span><span style="color:#df5000"><span style="color:#969896">=</span><span style="color:#969896">"</span>com<span style="color:#969896">"</span></span><span style="color:#969896">/></span></span>
</code></span></span>

Action层:

<span style="color:#000000"><span style="background-color:#f3f4f5"><code class="language-java"><span style="color:#a71d5d">package</span> com<span style="color:#969896">;</span>
<span style="color:#969896">@Controller</span>
<span style="color:#a71d5d">public</span> <span style="color:#a71d5d">class</span> HelloWorld<span style="color:#969896"></span>
	
	<span style="color:#969896">@RequestMapping</span><span style="color:#969896">(</span>value<span style="color:#df5000">=</span><span style="color:#df5000">"/showRegUser"</span><span style="color:#969896">)</span>
	<span style="color:#a71d5d">public</span> String <span style="color:#795da3">printHello</span><span style="color:#969896">(</span><span style="color:#969896">)</span> <span style="color:#969896"></span>
		<span style="color:#a71d5d">return</span> <span style="color:#df5000">"hello"</span><span style="color:#969896">;</span>
	<span style="color:#969896"></span>

    <span style="color:#969896">@Autowried</span>
    <span style="color:#a71d5d">private</span> IocSerevce service<span style="color:#969896">;</span>
    <span style="color:#a71d5d">public</span> <span style="color:#a71d5d">void</span> <span style="color:#795da3">add</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896"></span>
        service<span style="color:#969896">.</span><span style="color:#795da3">add</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
    <span style="color:#969896"></span>
<span style="color:#969896"></span>
</code></span></span>
component-scan默认扫描的注解类型是@Component,不过,在@Component的语义基础之上细化为@Reposity,@Service,@Controller.
有一个use-defaultbao'i-filters属性,属性默认是true,表示会扫描抱下所有的标有@Component的类,并注册为bean,也就是@Component的子注解@Service,@reposity等
如果只想扫描包下的@Controller或其他内容,则设置use-default-filters属性为false,表示不再按照scan指定的包进行扫描,而是按照指定包进行扫描
<span style="color:#000000"><span style="background-color:#f3f4f5"><code class="language-xml"><span style="color:#63a35c"><span style="color:#63a35c"><span style="color:#969896"><</span>context:component-scan</span> <span style="color:#795da3">base-package</span><span style="color:#df5000"><span style="color:#969896">=</span><span style="color:#969896">"</span>com<span style="color:#969896">"</span></span> <span style="color:#795da3">user-default-filters</span><span style="color:#df5000"><span style="color:#969896">=</span><span style="color:#969896">"</span>false<span style="color:#969896">"</span></span><span style="color:#969896">></span></span>
    <span style="color:#63a35c"><span style="color:#63a35c"><span style="color:#969896"><</span>context:include-filter</span> <span style="color:#795da3">type</span><span style="color:#df5000"><span style="color:#969896">=</span><span style="color:#969896">"</span>regex<span style="color:#969896">"</span></span> <span style="color:#795da3">expression</span><span style="color:#df5000"><span style="color:#969896">=</span><span style="color:#969896">"</span>com.tan.*<span style="color:#969896">"</span></span><span style="color:#969896">/></span></span>
<span style="color:#63a35c"><span style="color:#63a35c"><span style="color:#969896"></</span>context:component-scan</span><span style="color:#969896">></span></span>
</code></span></span>

当没有设置use-default-filters属性或属性为true时,表示基于base-package包下指定扫描的具体路径。

2. @Service()


此注注解属于业务逻辑层,service或者manager层
默认按照名称进行装配,如果名称可以通过name属性指定,如果没有name属性,注解写在字段上时,默认去字段名进行查找,如果注解写在setter方法上,默认按照方法属性名称进行装配。当找不到匹配的bean时,才按照类型进行装配,如果name名称一旦指定就会按照名称进行装配

Service层:

<span style="color:#000000"><span style="background-color:#f3f4f5"><code class="language-java"><span style="color:#969896">@Service</span><span style="color:#969896">(</span><span style="color:#969896">)</span>
<span style="color:#a71d5d">public</span> <span style="color:#a71d5d">class</span> IocService<span style="color:#969896"></span>

    <span style="color:#969896">@Resource</span>
    <span style="color:#a71d5d">private</span> IIocDao iiocDao<span style="color:#969896">;</span>
    <span style="color:#a71d5d">public</span> <span style="color:#a71d5d">void</span> <span style="color:#795da3">add</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896"></span>
        iiocDao<span style="color:#969896">.</span><span style="color:#795da3">add</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
    <span style="color:#969896"></span>
<span style="color:#969896"></span>
</code></span></span>

3. @Repository持久层

此注解式持久层组件,用于标注数据访问组件,即DAO组件
DAO层
先定义一个接口

<span style="color:#000000"><span style="background-color:#f3f4f5"><code class="language-java"><span style="color:#a71d5d">public</span> <span style="color:#a71d5d">interface</span> IIocDao<span style="color:#969896"></span>
    <span style="color:#a71d5d">public</span> <span style="color:#a71d5d">void</span> <span style="color:#795da3">add</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
<span style="color:#969896"></span>
</code></span></span>

然后实现类

<span style="color:#000000"><span style="background-color:#f3f4f5"><code class="language-java"><span style="color:#969896">//Dao层中定义了一些接口</span>
<span style="color:#969896">//表示将Dao类声明为bean</span>
<span style="color:#969896">@Repository</span>
<span style="color:#a71d5d">public</span> <span style="color:#a71d5d">class</span> IocDao <span style="color:#a71d5d">implements</span> IIocDao<span style="color:#969896"></span>
    <span style="color:#a71d5d">public</span> <span style="color:#a71d5d">void</span> <span style="color:#795da3">add</span><span style="color:#969896">(</span><span style="color:#969896">)</span><span style="color:#969896"></span>
        System<span style="color:#969896">.</span>out<span style="color:#969896">.</span><span style="color:#795da3">println</span><span style="color:#969896">(</span><span style="color:#df5000">"调用了Dao"</span><span style="color:#969896">)</span><span style="color:#969896">;</span>
    <span style="color:#969896"></span>
<span style="color:#969896"></span>
</code></span></span>
另外一种解释: @Repository对应数据访问层Bean
<span style="color:#000000"><span style="background-color:#f3f4f5"><code class="language-java"><span style="color:#969896">@Repository</span><span style="color:#969896">(</span>value<span style="color:#df5000">=</span><span style="color:#df5000">"userDao"</span><span style="color:#969896">)</span>
<span style="color:#a71d5d">public</span> <span style="color:#a71d5d">class</span> UserDaoImpl <span style="color:#a71d5d">extends</span> BeansDaoImpl<span style="color:#795da3"><span style="color:#969896"><</span>User<span style="color:#969896">></span></span><span style="color:#969896"></span>
    <span style="color:#969896">.</span><span style="color:#969896">.</span><span style="color:#969896">.</span><span style="color:#969896">.</span><span style="color:#969896">.</span><span style="color:#969896">.</span>
<span style="color:#969896"></span>
</code></span></span>

@Repository(value=“userDao”)注解告诉Spring ,让Spring创建一个名字叫做"userDao"的UserDapImpl实例。
当Service需要使用Spring创建的名字叫“userDao”的UserDaoImpl实例时,就可以使用@Resource(name=“userDao”)注解告诉Spring,Spring把创建好的userDao注入给Service即可。

<span style="color:#000000"><span style="background-color:#f3f4f5"><code class="language-java"> <span style="color:#969896">//注入userDao,从数据库中根据用户Id取出指定用户时需要用到</span>
 <span style="color:#969896">@Resource</span><span style="color:#969896">(</span>name <span style="color:#df5000">=</span> <span style="color:#df5000">"userDao"</span><span style="color:#969896">)</span>
 <span style="color:#a71d5d">private</span> BaseDao<span style="color:#795da3"><span style="color:#969896"><</span>User<span style="color:#969896">></span></span> userDao<span style="color:#969896">;</span>
</code></span></span>

以上是关于SpringMvc三层架构注解详解@Controller@Service和@Repository的主要内容,如果未能解决你的问题,请参考以下文章

springMVC

springMVC

springMVC

[SSM]SpringMVC 详解 @RequestMapping 注解获取请求参数和域数据共享

JavaEE框架——Springmvc入门(注解URL核心类详解文件上传和下载)和Spring的整合

浅析mvvm模式和mvc模式的区别和联系