zoukankan      html  css  js  c++  java
  • Spring自动装配与扫描注解

    1 javabean的自动装配

    自动注入,减少xml文件的配置信息。

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!-- 到入xml文件的约束 -->
     3 <beans xmlns="http://www.springframework.org/schema/beans"
     4     xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans
     6      http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
     7     <!-- 1 实例化Dao对象 id:完成对象的引用 class:指定需要创建的对象对应的类的完全限定名 -->
     8     <bean id="usersDao" class="org.guangsoft.dao.impl.UsersDaoImpl">
     9     </bean>
    10     <!-- 2实例化service autowire:属性的作用,完成对象依赖之间的自动装配 no(默认执行) byName:使用需要注入的属性对应的set的方法名字和spring容器中的对象的id进行匹配,如果能匹配上,进行自动注入 
    11         byType:使用需要注入的属性对应的set的方法参数类型和spring容器中的对象的类型进行匹配,如果能匹配上,进行自动注入 constructor:在byName和byType之间进行选择(首先byName,如果byName不匹配再byType) 
    12         实际使用:byName -->
    13     <bean id="usersService" class="org.guangsoft.service.impl.UsersServiceImpl"
    14         autowire="byType">
    15     </bean>
    16     <!-- 3实例化Action对象 -->
    17     <bean id="usersAction" class="org.guangsoft.action.UsersAction"
    18         autowire="byType">
    19     </bean>
    20 </beans>
    21  

    2 spring的扫描注解

    使用spring的扫描注解,重构三层结构。配置更少的内容

    在applicationContext.xml文件中,导入扫描的xsd

    l  开启注解扫描

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!-- 到入xml文件的约束 -->
     3 <beans xmlns="http://www.springframework.org/schema/beans"
     4     xmlns:context="http://www.springframework.org/schema/context" [ A1 ]
     5     xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     6     xsi:schemaLocation="http://www.springframework.org/schema/beans
     7      http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
     8      http://www.springframework.org/schema/context
     9      http://www.springframework.org/schema/context/spring-context-4.1.xsd
    10      ">
    11     <!-- 开启注解扫描 base-package属性:指定需要扫描的包,多个包之间使用,号隔开 a.b.c a.b.d a.b.e -->
    12     <context:component-scan
    13         base-package="org.guangsoft.dao.impl,
    14         org.guangsoft.service.impl,org.guangsoft.action"></context:component-scan>
    15 </beans> 

    注解进行总结

    类注解:

    @controller(给web层的注解)

    @service(给serivce层加的注解)

    @repository(给dao层加的注解) 

    @component(给java类加注解,老版本spring只有这一个注解)

    以上三个注解:将对应的类纳入spring容器中对应的

    Id:类名第一个字母小写(默认)

    如果需要自己指定id需要给三个注解加入String类的参数  

    @controller(“uAction”)   id=uAction

    @resouce(给需要依赖的对象属性加的注解)

    通过自动装配完成需要依赖属性的注入。

    参数:name:按照byName进行自动装配

    参数:type:按照byType进行自动装配

      

    注解执行过程

    1,加载spring的容器

    2, 扫描spring容器中指定包

    3, 扫描指定的包中,加了三个类注解的类,然后将该类纳入spring容器

    4, <bean id=””  class=””>

    5, 扫描类中被加入@resource注解的属性,然后按照自动装配的方式进行关系建立

    6, Autowrie

  • 相关阅读:
    【leetcode】1020. Partition Array Into Three Parts With Equal Sum
    【leetcode】572. Subtree of Another Tree
    【leetcode】123. Best Time to Buy and Sell Stock III
    【leetcode】309. Best Time to Buy and Sell Stock with Cooldown
    【leetcode】714. Best Time to Buy and Sell Stock with Transaction Fee
    【leetcode】467. Unique Substrings in Wraparound String
    【leetcode】823. Binary Trees With Factors
    【leetcode】143. Reorder List
    【leetcode】1014. Capacity To Ship Packages Within D Days
    【leetcode】1013. Pairs of Songs With Total Durations Divisible by 60
  • 原文地址:https://www.cnblogs.com/guanghe/p/6123330.html
Copyright © 2011-2022 走看看