spring创建bean的方式

来源:互联网 发布:线切割操作方法编程 编辑:程序博客网 时间:2024/06/05 04:46
spring框架中创建bean的三种方式

1.Instantiation with the constructor(用空的构造方法实例化一个对象)

  • xxx.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.xsd">           <!-- 当前配置,调用的是class所指向类的默认的空的构造方法来实例化一个对象(spring默认的创建bean的方式)-->           <bean id="dao" class="com.phome.dao.DAO" >           <property name="name"  value="mysql"></property>           </bean>           <bean id="service" class="com.phome.service.Service">           <property name="dao" ref="dao"></property>           </bean></beans>

  • DAO
  • package com.phome.dao;public class DAO {//创建一个静态的当前类的实力对象private static DAO dao = new DAO();    //构造方法私有化private DAO(){        }//创建一个提供外部获取当前方法的对象    public static DAO getDAO(){    return dao;    }    public void add(){    System.out.println("dao的add方法在执行");    }}
    2.Instantiation with a static factory method(用静态的工厂方法实例化一个对象)
  •  xxx.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.xsd">           <bean id="dao" class="com.phome.dao.DAO" >           <!-- 1.根据参数的位置注入当前参数的值 -->           <constructor-arg index="0" value="mysql"></constructor-arg>           <!-- 2.根据参数的类型注入当前的值 -->           <!--  <constructor-arg type="String" value="mysql"></constructor-arg>           <constructor-arg type="java.lang.String" vaue="mysql"></constructor-arg>-->           </bean>           <bean id="service" class="com.phome.service.Service">           <property name="dao" ref="dao"></property>           </bean></beans>


  • DAO
  • package com.phome.dao;public class DAO {public DAO(){        }private String name;    public DAO(String name) {super();this.name = name;}   public String getName() {return name;}public void setName(String name) {this.name = name;}public void add(){    System.out.println("dao的add方法在执行");    }}

    3.Instantiation using an instance factory method(用实例化工厂的方法实例化一个对象) 
  • xxx.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.xsd">           <bean id="daofactory"  class="com.phome.dao.DAOFactory"></bean>           <bean id="dao" factory-bean="daofactory" factory-method="getDAO">           </bean>           <bean id="service" class="com.phome.service.Service">           <property name="dao" ref="dao"></property>           </bean></beans>


  • DAO
  • package com.phome.dao;public class DAO {private String name;       public String getName() {return name;}public void setName(String name) {this.name = name;}public void add(){    System.out.println("dao的add方法在执行");    }}


  • DAOFactory
  • package com.phome.dao;/** * 产生dao的工厂 * @author  * */public class DAOFactory {private DAO dao = new DAO();    private DAOFactory(){        }    public DAO getDAO(){    System.out.println("DAOFactory的getDAO方法执行了");    return dao;    }    }


0 0
原创粉丝点击