基于setter的DI

相应的Example类

ExampleOne

package top.dj.bean;

/*基于 setter 的 DI*/
public class ExampleOne {

    private ExampleTwo exTwo;
    private ExampleThree exThree;
    private int number;

    public void setExTwo(ExampleTwo exTwo) {
        this.exTwo = exTwo;
    }

    public void setExThree(ExampleThree exThree) {
        this.exThree = exThree;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public void exOneMethod() {
        this.exTwo.exTwoMethod();
        this.exThree.exThreeMethod();
        System.out.println("number: " + this.number);
    }
}

ExampleTwo

package top.dj.bean;

public class ExampleTwo {

    public void exTwoMethod(){
        System.out.println("class -- ExampleTwo");
    }

}

ExampleThree

package top.dj.bean;

public class ExampleThree {

    public void exThreeMethod(){
        System.out.println("class -- ExampleThree");
    }

}

基于XML配置元数据

在bean.xml配置文件中指定一些bean定义。

<!--setter-->
<bean id="exampleOne" class="top.dj.bean.ExampleOne">
    <property name="exTwo">
        <ref bean="exampleTwo"/>
    </property>

    <property name="exThree" ref="exampleThree"/>
    <property name="number" value="66666"/>
</bean>

<bean id="exampleTwo" class="top.dj.bean.ExampleTwo"/>

测试类

测试是否依赖注入成功。

package top.dj;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.dj.bean.ExampleOne;

public class SetterTest {

    @Test
    public void test(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean.xml");
        ExampleOne one = (ExampleOne) context.getBean("exampleOne");
        one.exOneMethod();
    }

}

运行结果

class -- ExampleTwo
class -- ExampleThree
number: 66666

基于constructor的DI

相应的Constructor类

ConstructorOne

package top.dj.bean;

/*基于 constructor 的 DI*/
public class ConstructorOne {

    private ConstructorTwo two;
    private ConstructorThree three;
    private int num;

    public ConstructorOne(ConstructorTwo two, ConstructorThree three, int num) {
        this.two = two;
        this.three = three;
        this.num = num;
    }

    public void consMethodOne() {
        this.two.consMethodTwo();
        this.three.consMethodThree();
        System.out.println("num: " + this.num);
    }
}

ConstructorTwo

package top.dj.bean;

public class ConstructorTwo {

    public void consMethodTwo(){
        System.out.println("class -- ConstructorTwo");
    }

}

ConstructorThree

package top.dj.bean;

public class ConstructorThree {

    public void consMethodThree(){
        System.out.println("class -- ConstructorThree");
    }

}

bean.xml中配置元数据

<!--constructor-->
<bean id="consOne" class="top.dj.bean.ConstructorOne">
    <constructor-arg>
        <ref bean="consTwo"/>
    </constructor-arg>
    
    <constructor-arg ref="consThree"/>
    <constructor-arg type="int" value="77777"/>
</bean>

<bean id="consTwo" class="top.dj.bean.ConstructorTwo"/>
<bean id="consThree" class="top.dj.bean.ConstructorThree"/>

测试

package top.dj;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.dj.bean.ConstructorOne;

public class ConstructorTest {

    @Test
    public void testCons(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean.xml");
        ConstructorOne constructorOne = (ConstructorOne) context.getBean("consOne");
        constructorOne.consMethodOne();
    }

}

运行结果

class -- ConstructorTwo
class -- ConstructorThree
num: 77777

补充

  • 指示Spring调用静态工厂方法以返回对象的实例 (静态工厂方法的参数由<constructor-arg/>元素提供) 与实际使用了构造函数完全相同。
<bean id="exampleOne" class="top.dj.bean.ExampleOne" factory-method="creatInstance">
  • 由工厂方法返回的类的类型不必与包含静态工厂方法的类的类型相同。
  • 实例(非静态)工厂方法可以以基本相同的方式使用 (除了使用factory-bean属性而不是class属性之外) 。

分类: SSM

Deng Jia

Keep Moving.

0 条评论

发表评论

电子邮件地址不会被公开。