spring实例化bean之构造方法实例化

PersonDao

public interface PersonDao {

}

PersonDaoImpl
public class PersonDaoImpl implements PersonDao{
	
	public PersonDaoImpl(){
		System.out.println("spring通过构造方法来实例化对象");
	}
}

applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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-2.5.xsd">
	<!-- spring通过构造方法创建PersonDaoImpl -->				
	<bean id="personDao" class="com.xxc.initBean.one.dao.Impl.PersonDaoImpl"></bean>
</beans>

测试类
public class App {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("com/xxc/initBean/one/applicationContext.xml");
		PersonDao personDao = (PersonDaoImpl)ac.getBean("personDao");
	}
}


经验分享 程序员 微信小程序 职场和发展