全屏浏览
缩小浏览
回到页首

日记整理---->2017-05-17

  起始时间是2017-05-17,记录一下spring的学习过程。陌生人可以变成熟人,但熟人一旦变成陌生人,就再也回不去了。

 

一、测试一下init-method和depend-on

  • huhx.xml文件中,添加内容:
<bean id="initTest" class="com.linux.huhx.BaseTest.InitBean" init-method="sayHello" lazy-init="false">
    <property name="username" value="huhx"/>
</bean>
  • InitBean.java的代码如下:
public class InitBean {
    private String username;

    public void setUsername(String username) {
        this.username = username;
    }

    public void sayHello() {
        System.out.println("Hello" + this.username);
    }
}
  • Main.java的代码如下,模拟容器启动。这个在后面的测试代码中不变。
public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("huhx.xml");
    }
}

执行的结果如下:Hellohuhx

如果将lazy-init的值改为true,那么在容器启动的时候。不会创建该对象,而init方法是在创建对象之后调用的。

 

二、测试一下depend-on的使用

  • 在huhx.xml中添加以下内容
<bean id="dependTest" class="com.linux.huhx.BaseTest.DependTest" init-method="sayMyName" lazy-init="true">
    <property name="password" value="12345"/>
</bean>

修改initTest的定义,现在如下所示:

<bean id="initTest" class="com.linux.huhx.BaseTest.InitBean" init-method="sayHello" lazy-init="false" depends-on="dependTest">
    <property name="username" value="huhx"/>
</bean>

执行的结果如下:

hello12345
Hellohuhx

对于depends-on属性官方的文档说明:

1、If a "depends-on" relationship exists between any two objects, the dependent side will start after its dependency, and it will stop before its dependency. 
2、The depends-on attribute can explicitly force one or more beans to be initialized before the bean using this element is initialized.

 

 三、快速在字符串中插入变量

 

友情链接

 

posted @ 2017-07-28 23:09  huhx  阅读(156)  评论(0编辑  收藏  举报