构造方法类Bean1
package com.hao947.bean; public class Bean1 { public Bean1() { System.out.println("bean1...构造方法"); } public void show(){ System.out.println("bean1...方法"); } }
配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- 整个Spring文件根元素就是beans --> <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-3.1.xsd"> <!-- 此处及用Bean元素来定义Spring能创建的对象 --> <bean id="bean1" class="com.hao947.bean.Bean1"> </bean> </beans>
测试
public class InitBeanApp { @Test public void show() { ApplicationContext ac = new ClassPathXmlApplicationContext( "applicatioContext.xml"); Bean1 bs = (Bean1) ac.getBean("bean1"); System.out.println(bs); } }
结果
bean1...构造方法
com.hao947.bean.Bean1@663b1f38