Spring学习(1)
Spring2.5学习
1.实例化bean的三种方法:
(1) 构造器
<bean id="personService" value="构造注入的name" ></constructor-arg> <!-- 基本类型可以不写type --> <constructor-arg index="1" type="com.persia.IDaoBean"ref="personDao"> </constructor-arg> </bean>
public PersonServiceBean(String name, IDaoBean personDao) { this.name = name; this.personDao = personDao; }<bean id="personService2" factory-method="createInstance"> <!-- 静态工厂获取bean --> </bean>
public static PersonServiceBean createInstance(){ return new PersonServiceBean(); }<bean id="fac" factory-bean="fac" factory-method="createInstance"> <!-- 实例工厂获取bean,先实例化工厂再实例化bean--> </bean>
public PersonServiceBean createInstance(){ return new PersonServiceBean(); }ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});