• Spring学习(二)


    IoC

    1、Inverse of Control ,控制反转(控制权的翻转)

    2、控制:对对象的创建、对对象的属性赋值等一系列操作本来应该是我们做的事情

    Java Application :

    Date birthdate = new Date();
    Student s = new Student();
    s.setId( 1001 );
    s.setName( "张三丰" );
    s.setGender( '男' ) ;
    s.setBirthdate( birthdate )

    类似于自己在家吃饭得自己动手做

    3、反转:

    • 将对对象的创建、为属性赋值等操作的权限反转给容器,通常我们将控制权反转给Ioc container(ioc 容器)
    • Java程序中自主控制权限,包括创建对象,设置对象的值。逆转交给容器来做,做之前交给容器来做,文件名和路径没有要求

     spring 容器 读取配置文件生产相应的对象, Configuration Metadata ( "配置元数据" ) 用来 "告诉" Spring IoC 容器怎么产生对象,你的业务对象如果我们依赖于另一个对象,需要拿过来的时候,那个对象就是你的业务对象。类似去外面吃饭,spring容器相当于一个厨房,配置元数据相当于普通的材料,你的业务对象相当于特殊要求,例如加辣椒,由”厨房”做好之后直接吃就好了。

    Spring Bean Configuration File :

    <bean id="d" class="java.util.Date" />
    <bean id="s" class="io.spring.ioc.base.Student" >
        <property name="id" value="1001" />
        <property name="name" value="张三丰" />
         <property name="gender" value="男" />
        <property name="birthdate" ref="d" />
    </bean>

    id为d的bean就是你的业务对象

    Spring IoC Container :

    //指定configuration metadata配置元数据
    String configLocations = "classpath:ecut/**/base/ioc.xml" ;
    //创建spring IOC容器,在容器创建的时候调用无参构造
    AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations );
    //ready for use (此时可以从指定的IOC容器中获取指定名称的bean实例了)
    Student s = container.getBean( "s" , Student.class );
    //从容器中获取的bean实例中获取属性值
    System.out.println( s.getId() );

    BeanFactory就表示一个ioc容器,XmlBeanFactory是BeanFactory的实现类。     
    ApplicationContext是BeanFactory的子接口,ClassPathXmlApplicationContext是ApplicationContext的实现类。
    WebApplicationContext是ApplicationContext的子接口,XmlWebApplicationContext是WebApplicationContext的实现类。

    4、测试案例

    Student类

    package ecut.ioc.base;
    
    import java.util.Date;
    
    public class Student {
    
        private Integer id;
        private String name;
        private char gender;
        private Date birthdate;
        
        public Student() {
            super();
            System.out.println( "调用 Student 无参构造创建对象" );
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
            System.out.println( "为id属性赋值: " + id );
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public char getGender() {
            return gender;
        }
    
        public void setGender(char gender) {
            this.gender = gender;
        }
    
        public Date getBirthdate() {
            return birthdate;
        }
    
        public void setBirthdate(Date birthdate) {
            this.birthdate = birthdate;
            System.out.println( "为birthdate属性赋值: " + birthdate );
        }
    
    }

    ioc.xml(Spring Bean Configuration File)名称不一定需要交applicationContext.xml,可以安装spring插件方便生成配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
    
        <bean id="d" class="java.util.Date" />
    
        <bean id="s" class="ecut.ioc.base.Student">
            <property name="id" value="1001" />
            <property name="name" value="张三丰" />
            <property name="gender" value="男" />
            <property name="birthdate"  ref="d" /> 
        </bean>
        
        <!-- <bean id="s" class="io.spring.ioc.base.Student">
            <property name="id" value="1001" />
            <property name="name" value="张三丰" />
            <property name="gender" value="男" />
            <property name="birthdate" > 
                <bean class="java.util.Date" />
            </property> 
        </bean>-->
        
    </beans>

    测试类

    package ecut.ioc.base;
    
    
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class TestSpringContainer {
    
        public static void main(String[] args) {
            //指定configuration metadata配置元数据
            String configLocations = "classpath:ecut/**/base/ioc.xml" ;
            //创建spring IOC容器,在容器创建的时候调用无参构造
            AbstractApplicationContext container = new ClassPathXmlApplicationContext( configLocations );
            //ready for use (此时可以从指定的IOC容器中获取指定名称的bean实例了)
            Student s = container.getBean( "s" , Student.class );
            //从容器中获取的bean实例中获取属性值
            System.out.println( s.getId() );
            
            System.out.println( s.getName() );
            
            System.out.println( s.getGender() );
            
            System.out.println( s.getBirthdate() );
            //关闭spring的IOC容器
            container.close();
            
        }
    
    }

    classpath是指当前工程下bin目录,/**/指多级目录

    转载请于明显处标明出处

    https://www.cnblogs.com/AmyZheng/p/9249411.html

  • 相关阅读:
    最短路模板
    Vue中的列表item依次进入动画
    Vue2.6.11中禁止点击事件冒泡
    <img/>标签实现加载错误图的方法(包含Vue框架中的使用)
    H5实现弹窗从下边弹出、关闭效果
    CSS img 宽度固定,高度自适应
    CSS实现Android LinearLayout的效果
    css中实现Android relativeLayout的效果
    Android Log写入本地文件
    Cannot fit requested classes in a single dex file (# methods: 148419 > 65536
  • 原文地址:https://www.cnblogs.com/AmyZheng/p/9249411.html
Copyright © 2020-2023  润新知