1. Spring生成bean的三种方式
1.1. 无参构造方法
public class UserDaoImpl implements UserDao {
public UserDaoImpl() {
System.out.println("调用了无参构造方法...");
}
@Override
public void save() {
System.out.println("user save...");
}
}<bean id="userDao" class="cn.itcast.dao.impl.UserDaoImpl"></bean>
@Test
public void test1() {
// 创建spring工厂(创建ioc容器)
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) ac.getBean("userDao");
userDao.save();
}
1.2. 静态工厂实例化方式
package cn.itcast.dao;
public interface DeptDao {
public void save();
}

public class DeptDaoImpl implements DeptDao{
@Override
public void save() {
System.out.println("持久层:部门保存...");
}
}
public class Factory {
/**
* 静态工厂方法
*/
**public** **static** DeptDao create(){
System.**out**.println("调用了静态工厂方法");
**return** **new** DeptDaoImpl();
}
}
<?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*
www.springframework.org/schema/bean…">
<bean id="userDao" class="cn.itcast.dao.impl.UserDaoImpl"></bean>
<bean id="deptDao" class="cn.itcast.factory.Factory" factory-method="create"></bean>
</beans>
@Test
public void test4(){
//创建Spring工厂(创建IOC容器)
ApplicationContext ac = **new** ClassPathXmlApplicationContext("applicationContext.xml");
DeptDao deptDao = (DeptDao) ac.getBean("deptDao");
deptDao.save();
}
1.3. 实例工厂实例化方式
public class Factory {
/**
* 静态工厂方法
*/
/*public static DeptDao create(){
System.out.println("调用了静态工厂方法");
return new DeptDaoImpl();
}*/
/**
* 实例工厂方法
* **@return**
*/
**public** DeptDao create(){
System.**out**.println("调用了实例工厂方法");
**return** **new** DeptDaoImpl();
}
}
<?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*
www.springframework.org/schema/bean…">
<bean id="userDao" class="cn.itcast.dao.impl.UserDaoImpl"></bean>
<!-- <bean id="deptDao" class="cn.itcast.factory.Factory" factory-method="create"></bean> -->
<!-- 实例工厂方法来实例化 -->
<bean id="factory" class="cn.itcast.factory.Factory"></bean>
<bean id="deptDao" factory-bean="factory" factory-method="create"></bean>
</beans>

1.1. 无参构造方法
默认调用无参构造方法实例化bean。
编写UserDao接口及实现类UserDaoImpl:
file://C:\Users\Student\Desktop\上海Java3月精品文章\肖凯波\spring生成bean的三种方式\Img\wps4600.tmp.jpg?lastModify=1523327470
在UserDaoImpl添加无参构造方法:
public UserDaoImpl() {
System.out.println("调用了无参构造方法...");
}
@Override
public void save() {
System.out.println("user save...");
}
}
把UserDaoImpl配置spring容器中:
创建单元测试类TestDI,并编写单元测试方法test1:
file://C:\Users\Student\Desktop\上海Java3月精品文章\肖凯波\spring生成bean的三种方式\Img\wps4601.tmp.jpg?lastModify=1523327470
@Test
public void test1() {
// 创建spring工厂(创建ioc容器)
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) ac.getBean("userDao");
userDao.save();
}
测试结果如下:
file://C:\Users\Student\Desktop\上海Java3月精品文章\肖凯波\spring生成bean的三种方式\Img\wps4602.tmp.jpg?lastModify=1523327470
通过调用工厂类的静态方法来生成bean
编写DeptDao接口
file://C:\Users\Student\Desktop\上海Java3月精品文章\肖凯波\spring生成bean的三种方式\Img\wps4603.tmp.jpg?lastModify=1523327470
package cn.itcast.dao;
public interface DeptDao {
public void save();
}
编写DeptDaoImpl实现类
file://C:\Users\Student\Desktop\上海Java3月精品文章\肖凯波\spring生成bean的三种方式\Img\wps4604.tmp.jpg?lastModify=1523327470
public class DeptDaoImpl implements DeptDao{
@Override
public void save() {
System.out.println("持久层:部门保存...");
}
}
编写工厂类,在其中创建静态工厂方法
file://C:\Users\Student\Desktop\上海Java3月精品文章\肖凯波\spring生成bean的三种方式\Img\wps4605.tmp.jpg?lastModify=1523327470
public class Factory {
/**
* 静态工厂方法
*/
**public** **static** DeptDao create(){
System.**out**.println("调用了静态工厂方法");
**return** **new** DeptDaoImpl();
}
}
编写applicationContext.xml配置文件,采用静态工厂方式配置DeptDaoImpl类
<?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*
www.springframework.org/schema/bean…">
<bean id="userDao" class="cn.itcast.dao.impl.UserDaoImpl"></bean>
<bean id="deptDao" class="cn.itcast.factory.Factory" factory-method="create"></bean>
</beans>
在配置DeptDaoImpl这个bean时,class属性写的不是DeptDaoImpl的全路径名,而是工厂类的全路径名;
factory-method:指定工厂类中静态方法的名字
在TestIOC类中编写测试方法test4
@Test
public void test4(){
//创建Spring工厂(创建IOC容器)
ApplicationContext ac = **new** ClassPathXmlApplicationContext("applicationContext.xml");
DeptDao deptDao = (DeptDao) ac.getBean("deptDao");
deptDao.save();
}
测试结果如下:
file://C:\Users\Student\Desktop\上海Java3月精品文章\肖凯波\spring生成bean的三种方式\Img\wps4606.tmp.jpg?lastModify=1523327470
修改Factory工厂类,创建实例工厂方法:
public class Factory {
/**
* 静态工厂方法
*/
/*public static DeptDao create(){
System.out.println("调用了静态工厂方法");
return new DeptDaoImpl();
}*/
/**
* 实例工厂方法
* **@return**
*/
**public** DeptDao create(){
System.**out**.println("调用了实例工厂方法");
**return** **new** DeptDaoImpl();
}
}
编写applicationContext.xml,采用实例工厂方式重写配置DeptDaoImpl
<?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*
www.springframework.org/schema/bean…">
<bean id="userDao" class="cn.itcast.dao.impl.UserDaoImpl"></bean>
<!-- <bean id="deptDao" class="cn.itcast.factory.Factory" factory-method="create"></bean> -->
<!-- 实例工厂方法来实例化 -->
<bean id="factory" class="cn.itcast.factory.Factory"></bean>
<bean id="deptDao" factory-bean="factory" factory-method="create"></bean>
</beans>
factory-bean:指定工厂bean的id;
Factory-method:指定工厂bean的实例工厂方法的名字
运行test4测试方法,测试结果如下:
file://C:\Users\Student\Desktop\上海Java3月精品文章\肖凯波\spring生成bean的三种方式\Img\wps4617.tmp.jpg?lastModify=1523327470
更多技术资讯可关注:gzitcast