一、实例
(1)新建maven项目并编写基础代码。
InstanceFactory.java
package com.example.factory;
import com.example.service.IUserService;
import com.example.service.impl.UserServiceImpl;
/**
* @className: InstanceFactory
* @description: 模拟工厂类
* @author: j
* @date: 2020/3/22 15:28
*/
public class InstanceFactory {
//模拟在一个工厂类中得到UserService对象
public IUserService getUserService(){
return new UserServiceImpl();
}
}
StaticFactory.java
package com.example.factory;
import com.example.service.IUserService;
import com.example.service.impl.UserServiceImpl;
/**
* @className: StaticFactory
* @description: 模拟一个工厂类
* @author: j
* @date: 2020/3/22 15:35
*/
public class StaticFactory {
//静态方法 模拟在一个工厂类中得到UserService对象
public static IUserService getUserService(){
return new UserServiceImpl();
}
}
IUserService.java
package com.example.service;
/**
* @className: IUserService
* @description: 用户业务层接口
* @author: j
* @date: 2020/3/17 15:36
*/
public interface IUserService {
/**
* 模拟保存用户
*/
public void saveUser();
}
UserServiceImpl.java
package com.example.service.impl;
import com.example.service.IUserService;
/**
* @className: UserServiceImpl
* @description:
* @author: j
* @date: 2020/3/17 15:38
*/
public class UserServiceImpl implements IUserService {
public void saveUser(){
System.out.println("执行UserServiceImpl中的saveUser()方法");
}
}
Client.java
package com.example.ui;
import com.example.service.IUserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @className: Client
* @description: 模拟一个表现层,用于调用业务层
* @author: j
* @date: 2020/3/17 15:41
*/
public class Client {
/**
* 获取spring ioc容器
* @param args
*/
public static void main(String[] args) {
//获取ioc容器对象
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
//获取bean对象
IUserService userService= (IUserService) applicationContext.getBean("userService");
//测试
System.out.println("userService对象:" +userService);
//userService.saveUser();
}
}
bean.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">
<!--把对象的创建交给spring来管理-->
<!--方式一:默认创建bean的方式,使用bean标签和id、class属性(spring会使用默认构造函数来创建)-->
<bean id="userService" class="com.example.service.impl.UserServiceImpl"/>
</beans>
(2)执行Client.java中的方法,结果如下
此时生成bean采用的是第一种方式(默认方式)
<!--方式一:默认创建bean的方式,使用bean标签和id、class属性(spring会使用默认构造函数来创建)-->
<bean id="userService" class="com.example.service.impl.UserServiceImpl"/>
(3)InstanceFactory是一个模拟工厂类,一般情况下我们无法修改其源码,其里面有一个得到对象的方法,若根据此类来创建对象,则是spring创建bean的第二种方式
<!--方式二:使用某个类的方法创建bean并存入spring容器-->
<!--<bean id="instanceFactory" class="com.example.factory.InstanceFactory"/>
<bean id="userService" factory-bean="instanceFactory" factory-method="getUserService"/>
(4)StaticFactory与InstanceFactory类似,不过其得到对象的方法是静态方法,相应的spring创建bean的第三种方式是
<!--方式三:使用某个类中的静态方法创建bean并存入spring容器-->
<!--<bean id="userService" class="com.example.factory.StaticFactory" factory-method="getUserService"/>
二、总结
spring创建bean一共有三种方式
<!--方式一:默认创建bean的方式,使用bean标签和id、class属性(spring会使用默认构造函数来创建)-->
<bean id="" class=""/>
<!--方式二:使用某个类的方法创建bean并存入spring容器-->
<bean id="" class=""/>
<bean id="" factory-bean="" factory-method=""/>
<!--方式三:使用某个类中的静态方法创建bean并存入spring容器-->
<!--<bean id="" class="" factory-method=""/>
第一种即默认方式:bean标签赋以id(表示所要生成的bean)和class属性(所要生成的bean的相应的类所在位置)。
第二种适合对于一些我们无法修改源码或一些其他类里面可以得到bean对象的类,第一个bean标签对应相应的其他类,第二个bean标签中factory-bean对应第一个bean的id,factory-method对应相应的得到对象的方法。
第三种和第二种类似只不过得到对象的方法是静态方法而已。
2020.03.22