Spring的IOC、DI

这篇写得很好,值得一看。

https://www.cnblogs.com/superjt/p/4311577.html

 

IOC创建对象

1.导包

2.测试类

3.XML文件

4.测试

DI(依赖注入)

作用:给创建好的对象中的全局属性或者对象进行赋值。

public class Student {

    private int age;
    private String name;
    private String sex;

    public Student() {
        System.out.println("Student的无参构造");
    }

    public Student(int age, String name, String sex) {
        System.out.println("有参构造123");
    }
}

因为name值与形参的顺序无关,所以如果有参构造的形参名字一样,仅仅顺序不一致,想要调用指定的有参构造,必须加条件(index、type)。

 

工厂模式创建对象

public interface People {
    void run();
    void eat();
}
public class Teacher implements People{

    @Override
    public void run() {
        System.out.println("老师run");
    }

    @Override
    public void eat() {
        System.out.println("老师eat");
    }
}
public class Student01 implements People {
    @Override
    public void run() {
        System.out.println("学生run");
    }

    @Override
    public void eat() {
        System.out.println("学生eat");
    }
}
public class Factory {
    public People getInstance(String type) {
        switch (type){
            case "teacher":
                return new Teacher();
            case "student":
                return new Student01();
        }
        return null;
    }
}
public class Test02 {
    public static void main(String[] args) {
        Factory factory=new Factory();
        People instance = factory.getInstance("student");
        instance.eat();
    }
}

 

IOC创建对象

<?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.xsd">

    <!--Factory factory=new Factory();-->
    <bean id="fac" class="com.gakki.pojo.Factory"></bean>
    
    <!--People instance = factory.getInstance(String type);-->
    <bean id="be" factory-bean="fac" factory-method="getInstance">
        <constructor-arg value="teacher"></constructor-arg>
    </bean>
</beans>
public class Test02 {
    public static void main(String[] args) {
        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext01.xml");
        People people = app.getBean("be", People.class);
        people.eat();
    }
}

 

ref:传入的是一个对象。

value:传入的是基本类型或者String。

public class Test03 {
    public static void main(String[] args) {
        ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext02.xml");
        Student02 stu2 = app.getBean("stu2", Student02.class);
        System.out.println(stu2);
    }
}

 

自动注入

<?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.xsd">

   <bean id="cla" class="com.gakki.pojo.Clazz">
       <constructor-arg name="deptno" value="1"></constructor-arg>
       <constructor-arg name="name" value="高一"></constructor-arg>
   </bean>
   <!--根据数据类型-->
    <bean id="stu2" class="com.gakki.pojo.Student02" autowire="byType"></bean>
</beans>

<?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.xsd">

   <bean id="user" class="com.gakki.pojo.User">
       <property name="array">
           <array>
               <value>A</value>
               <value>B</value>
               <value>C</value>
           </array>
       </property>
       <property name="list">
           <list>
               <value>D</value>
               <value>E</value>
               <value>F</value>
           </list>
       </property>
       <property name="set">
           <set>
               <value>G</value>
               <value>H</value>
               <value>I</value>
           </set>
       </property>
       <property name="map">
           <map>
               <entry>
                   <key><value>1</value></key>
                   <value>J</value>
               </entry>
               <entry>
                   <key><value>2</value></key>
                   <value>K</value>
               </entry>
               <entry>
                   <key><value>3</value></key>
                   <value>L</value>
               </entry>
           </map>
       </property>
   </bean>
</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值