举例说明spring框架中三种创建bean的方法

1.通过创建静态工厂的方法进行创建bean

代码:

  • com.phome.dao.DAO -> dao
public class DAO {
// 创建一个当前类的静态的实例对象
private static DAO dao = new DAO();

// 构造方法私有化
private DAO() {

}
// 创建一个外部访问当前对象的方法
public static DAO getDao(){
System.out.println("getDao()的方法被执行了");
return dao;
}
public void add(){
System.out.println("的add方法正在执行...");
}
}
   
  •  com.phome.service ->service
public class DAO {
// 创建一个当前类的静态的实例对象
private static DAO dao = new DAO();

// 构造方法私有化
private DAO() {

}
// 创建一个外部访问当前对象的方法
public static DAO getDao(){
System.out.println("getDao()的方法被执行了");
return dao;
}
public void add(){
System.out.println("的add方法正在执行...");
}
}
  • com.phome.main -> main
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.phome.service.Service;


public class Main {
public static void main(String[] args) {
// 获得IOC容器工厂
BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml");
// 从容器中获取对应id名称的对象
Service service = (Service)factory.getBean("service");
// 执行对象方法
service.regist();
}
}
  • spring.xml中的配置( 新建xml文件命名为spring.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" factory-method="getDao"></bean>

<bean id="service" class="com.phome.service.Service">
<property name="dao" ref="dao"></property>
</bean>


</beans>
2.通过创建构造方法创建bean(注意里面的service类与main类与第一种方法中的一样这里就不重复写了)
代码:
  • com.phome.dao -> DAO
public class DAO {
private String name;

// 提供构造方法
public DAO() {

}
public DAO(String name) {
this.name = name;
}
public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}
public void add(){
System.out.println(name + "的add方法正在执行...");
}
}

  • spring.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="mysqldependecyinjection"></constructor-arg>
<!-- 方法2 -->
<!-- <constructor-arg type="java.lang.String" value="hello"></constructor-arg> -->
</bean>

<bean id="service" class="com.phome.service.Service">
<property name="dao" ref="dao"></property>
</bean>


</beans>
3.通过创建工厂的方法创建bean(注意里面的service类与main类与第一种方法中的一样这里就不重复写了)
代码:
  • com.phome.dao -> 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(name + "的add方法正在执行...");
}
}

  • com.phome.dao -> DaoFactory
/**
 * 生产Dao的工厂
 *
 */
public class DaoFactory {
private DAO dao = new DAO();

private DaoFactory() {

}

public DAO getDao() {
System.out.println("DAOFactory的getDao方法执行");
return dao;
}
}

  • spring.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">
<property name="name" value="mysql"></property>
</bean>


<bean id="service" class="com.phome.service.Service">
<property name="dao" ref="dao"></property>
</bean>


</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值