标签:整理 ace cvs ihe classpath pat jin ram password
起始时间是2017-05-17,记录一下spring的学习过程。陌生人可以变成熟人,但熟人一旦变成陌生人,就再也回不去了。
<bean id="initTest" class="com.linux.huhx.BaseTest.InitBean" init-method="sayHello" lazy-init="false"> <property name="username" value="huhx"/> </bean>
public class InitBean { private String username; public void setUsername(String username) { this.username = username; } public void sayHello() { System.out.println("Hello" + this.username); } }
public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("huhx.xml"); } }
执行的结果如下:Hellohuhx
如果将lazy-init的值改为true,那么在容器启动的时候。不会创建该对象,而init方法是在创建对象之后调用的。
<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.
标签:整理 ace cvs ihe classpath pat jin ram password
原文地址:http://www.cnblogs.com/huhx/p/basediary20170517.html