spring实例化bean以及构造器装配属性

来源:互联网 发布:sql 添加合计行 编辑:程序博客网 时间:2024/05/29 13:58
Spring三种实例化Bean的方式
1、使用类构造器实例化  90%
<bean id="personService" class="com.insigma.service.impl.PersonServiceBean"></bean>


2、使用静态工厂方法实例化
<bean id="personService" class="com.insigma.service.PersonServiceBeanFactory" factory-method="createPersonServiceBean"></bean>
public class PersonServiceBeanFactory
{
    public static PersonServiceBean createPersonServiceBean()
    {
        return new PersonServiceBean();
    }
}


3、使用实例化工厂方法实例化
<bean id="personServiceFactory" class="com.insigma.service.PersonServiceBeanFactory" ></bean>
<bean id="personService" class="com.insigma.service.PersonServiceBeanFactory" factory-bean="personServiceFactory"factory-method="createPersonServiceBean"></bean>
public class PersonServiceBeanFactory
{
    public PersonServiceBean createPerson()
    {
        return new PersonServiceBean();
    }

}



private PersonDao personDao;
private String personName;
private int personAge;


public PersonServiceBean(PersonDao personDao, String personName,
int personAge) {
this.personDao = personDao;
this.personName = personName;
this.personAge = personAge;
}


<bean id="personService" class="com.insigma.service.impl.PersonServiceBean"  init-method="initBean" destroy-method="destoryBean">
            <!--  <property name="personDao" ref="personDao"></property>
            <property name="personName" value="张三"></property>
            <property name="personAge" value="33"></property> -->
            <constructor-arg index="0" type="com.insigma.dao.PersonDao" ref="personDao" />
            <constructor-arg index="1" type="String" value="张三" />
            <constructor-arg index="2"  type="int" value="33" />
           </bean>

原创粉丝点击