• Spring依赖注入


    注入依赖对象有手工装配(推荐)和自动装配的方式。手工装配可在xml中,也可用注解方式。

    1.Setter

    2.构造器方式

    1 <bean id="pDao" class="PersonDaoBean"/>
    2 <bean id="ps" class="PersonServiceBean">
    3 <constructor-arg index="0" type="PersonDao" ref="pDao"/>
    4 <constructor-arg index="1" value="tazi"/>
    5 </bean>

    3.注解方式

    @Resource先按名称(指定name属性或Bean的字段名)查找,再按类型查找

    @Autowired默认按照类型查找,它要求依赖对象必须存在,如果允许为空,可以设置@Autowired(required=false)。

    如果要按名称,则@Autowired @Qualifier("pDao")。

    依赖的jar包在spring2.5/lib/j2ee/common-annotations.jar

    (1).必须引入context命名空间:

    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation加上

    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd"

    (2)加上<context:annotation-config/>

    这种配置隐式注册了多个注解解析器:AutowiredAnnotationBeanPostProcesor,CommonAnnotationBeanPostProcesor,PersistenceAnnotationBeanPostProcesor,RequiredAnnotationBeanPostProcesor.



  • 相关阅读:
    负载均衡
    重写类的Equals以及重写Linq下的Distinct方法
    关于URLEnCode,URLDeCode,Base64,公钥私钥
    JAVA 从头开始<六>
    JAVA 从头开始<五>
    JAVA 从头开始<四>
    JAVA 从头开始<三>
    JAVA 从头开始<二>
    JAVA 从头开始<一>
    ASP.Net MVC OA项目笔记<六>
  • 原文地址:https://www.cnblogs.com/tazi/p/2307034.html
Copyright © 2020-2023  润新知