meleto 2020-05-29
在构造器中进行某些初始化操作 public class ExampleBean { public ExampleBean() { System.out.println("正在进行初始化操作。。。。"); } public void execute() { System.out.println("调用了execute方法"); } } 1 在构造器中进行某些初始化操作
public class ExampleBean {
public ExampleBean() {
System.out.println("正在进行初始化操作。。。。");
}
public void execute() {
System.out.println("调用了execute方法");
}
}
public static void main(String[] args) { String conf = "applicationContext.xml"; //创建容器对象 ApplicationContext ac = new ClassPathXmlApplicationContext(conf); //得到Example对象 ExampleBean e1 = ac.getBean("example",ExampleBean.class); ExampleBean e2 = ac.getBean("example",ExampleBean.class); System.out.println(e1 == e2); } 1 public static void main(String[] args) {
String conf = "applicationContext.xml";
//创建容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//得到Example对象
ExampleBean e1 = ac.getBean("example",ExampleBean.class);
ExampleBean e2 = ac.getBean("example",ExampleBean.class);
System.out.println(e1 == e2);
}
<!-- 创建一个ExampleBean对象 --> <bean id ="example" class="com.zlc.test.ExampleBean" scope="prototype" init-method = "init"></bean> 1 <!-- 创建一个ExampleBean对象 -->
<bean id ="example" class="com.zlc.test.ExampleBean" scope="prototype" init-method = "init"></bean>
<!-- 创建一个ExampleBean对象 --> <bean id ="example" class="com.zlc.test.ExampleBean" init-method = "init" destroy-method="destroy"> </bean> 1 <!-- 创建一个ExampleBean对象 -->
<bean id ="example" class="com.zlc.test.ExampleBean" init-method = "init"
destroy-method="destroy">
</bean>
public static void main(String[] args) { String conf = "applicationContext.xml"; //创建容器对象 AbstractApplicationContext ac = new ClassPathXmlApplicationContext(conf); //得到Example对象 ExampleBean e1 = ac.getBean("example",ExampleBean.class); ExampleBean e2= ac.getBean("example",ExampleBean.class); ac.close();//释放spring容器对象,它会自动调用单例的destroy方法。 } 1 public static void main(String[] args) {
String conf = "applicationContext.xml";
//创建容器对象
AbstractApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//得到Example对象
ExampleBean e1 = ac.getBean("example",ExampleBean.class);
ExampleBean e2= ac.getBean("example",ExampleBean.class);
ac.close();//释放spring容器对象,它会自动调用单例的destroy方法。
}
public class Role { private int id; private String roleName; private String note; public Role(){} public Role(int id,String roleName, String note) { super(); this.id = id; this.roleName = roleName; this.note = note; } //setter,getter方法省略 } 1 public class Role {
private int id;
private String roleName;
private String note;
public Role(){}
public Role(int id,String roleName, String note)
{
super();
this.id = id;
this.roleName = roleName;
this.note = note;
} //setter,getter方法省略
}
<bean id="Role" class="com.xiaojiang.Spring.Role"> <constructor-arg value="1" type="int"></constructor-arg> <constructor-arg value="小江" type="java.lang.String"></constructor-arg> <constructor-arg value="有点丑" type="java.lang.String"></constructor-arg> </bean> 1 <bean id="Role" class="com.xiaojiang.Spring.Role">
<constructor-arg value="1" type="int"></constructor-arg>
<constructor-arg value="小江" type="java.lang.String"></constructor-arg>
<constructor-arg value="有点丑" type="java.lang.String"></constructor-arg>
</bean>
<bean id="Role" class="com.xiaojiang.Spring.Role"> <property name="id" value="1"><property> <property name="roleName" value="小江"><property> <property name="note" value="测试"><property> </bean> 1 <bean id="Role" class="com.xiaojiang.Spring.Role">
<property name="id" value="1"><property>
<property name="roleName" value="小江"><property>
<property name="note" value="测试"><property>
</bean>
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationfile.xml"); } 1 public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationfile.xml");
}