zoukankan      html  css  js  c++  java
  • 【学习笔记】 使用XML配置和注解实现Spring的依赖注入DI (2-3-2)

    Spring的四个核心组件


    1.beans

      Bean是包装应用程序自定义对象Object的 Object中保存数据
    2.core 
    3.context

      一个Bean的关系集合
    4.expression

    ApplicationContext ctx=new ClasspathXmlApplicationContext("applicationContext.xml");
    //容器一旦生成 所有bean都被加载到内存中

    Printer service=(Printer)ctx.getBean("printer");


    1.Spring容器创建的时候会将所有配置的Bean创建对象,默认bean都是单例的

    BeanFactory和ApplicationContext

    2.代码通过个体Bean()方法从容器获取指定的bean实例,容器会首先调用Bean类的无参构造器创建实例对象

    3.bean的作用域
    scope="prototype"

      原型模式:真正使用的时候才会创建 每获取一次都会创建不同对象


    scope="singleton"

      单例模式:容器初始化时需要使用name建 每次获取的都是同一个对象,默认值
    <bean id="stu1" class="xx.xx.Student" scope="prototype" ></bean>

    4.bean的生命周期(见另一篇博客)

    5.bean的id和name属性
    当bean中含有特殊字符时需要使用name属性

    6.基于xml的DI(Dependency Injection)
      Spring 提供了多种依赖注入的手段

      除了通过属性的setter访问器

      还可以通过带参构造方法实现依赖注入


    注入类型


    1.设值注入

    <bean id="stu1" class="xx.xx.Student" >
    <property name="name" value="斯沃"/>
    <property name="age" value="18"/>
    </bean>

    2.构造注入

    <bean id="stu1" class="xx.xx.Student" >
    <constructor-arg index="0" value="斯沃"></constructor-arg>
    <constructor-arg index="1" value="18"></constructor-arg>
    </bean>
    public Student(String name, int age){
    this.name=name;
    this.age=age;
    }


    域属性注入(Java Bean属性)

    <bean id=mcar" class="xx.xx.Car" >
    <property name="color" value="红色"/>
    </bean>
    
    <bean id="stu1" class="xx.xx.Student" >
    <property name="name" value="斯沃"/>
    <property name="age" value="18"/>
    <property name="car" ref="mcar"/>
    </bean>



    真正开发时推荐使用设值注入 因为要使用构造注入 Bean必须写出所有的构造


    3.命名空间注入

    xmlns:p="http://www.springframework.org/schema/p"    
    
    <bean id="stu1" class="xx.xx.Student" p:name="测试" p:age="18" p:car-ref="mcar">
    </bean>
    //命名空间p注入 p:xxx property
    //同样 有命名空间 c注入


    *4.集合属性注入

    public class CollectionBean{
    private List<String> list;
    }


    1.数组array

    <property name="names">
    <array>
    <value>a</value>
    <value>b</value>
    </array>
    </property>


    2.List

    <bean id="List" class="xx.xx.CollectionBean">
    <property name="list">
    <list>
    <value>sword</value>
    <value>fizz</value>
    </list>
    </property>
    </bean>


    3.Set
    4.Map

    <bean id="Map" class="xx.xx.CollectionBean">
    <property name="map">
    <map>
    <entry key="a">
    <value>shit</value>
    </entry>
    </map>
    </property>
    </bean>


    Properties


    7.基于注解的依赖注入


    1.引入 spring-aop-4.2.0.REKEASE.jar


    2.添加xsd约束

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    3.配置包扫描器  

      配置了包扫描器 该包下以及子包中的类才可以被Spring扫描
      去寻找被注解的类和属性,让Spring容器管理赋值


    component:组件
    scan:
    <context:component-scan base-package="sword.po"></context:component-scan>

    //标识这个类
    @Compoent("info")
    public class User{
    //普通属性
    @Value("斯沃")
    private String name;
    @Value("18")
    private int age;
    //域属性
    @Resource
    private Car car;
    }
    
    //@Autowired
    @Compoent("car")
    public class Car{
    @Value("red")
    private String color;
    }


    等价
    @Compoent("car") //不分层
    @Repository("info") //dao
    @Service //biz
    @Controller //Action

  • 相关阅读:
    SCI写作经典替换词,瞬间高大上!(转)
    最佳化常用测试函数 Optimization Test functions
    算法复杂度速查表
    VS 代码行统计
    CPLEX IDE 菜单栏语言设置( 中文 英文 韩文 等多国语言 设置)
    如何从PDF文件中提取矢量图
    Matlab无法打开M文件的错误( Undefined function or method 'uiopen' for input arguments of type 'char)
    visual studio 资源视图 空白 解决方案
    MFC DialogBar 按钮灰色不响应
    嗨翻C语言笔记(二)
  • 原文地址:https://www.cnblogs.com/swordtm/p/6618554.html
Copyright © 2011-2022 走看看