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

  • 相关阅读:
    CSS3阴影 box-shadow的使用和技巧总结
    css实现一行文字居中,多行文字左对齐
    让一个div里面的其它div或者标签内容居中
    巧用css内容生成
    Emmet-前端开发神器
    新手向:Vue 2.0 的建议学习顺序
    require() 源码解读
    Vue-webpack项目配置详解
    webpack---webpack构建vue多页面框架(一、工程布局)
    STS创建spring boot项目,pom.xml文件第一行报错
  • 原文地址:https://www.cnblogs.com/swordtm/p/6618554.html
Copyright © 2011-2022 走看看