文章目录
1. 创建bean的三种方式
1.1 使用默认构造函数创建
- 在spring配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。
- 采用的就是默认无参构造函数创建bean对象,此时如果类中没有默认无参构造函数,则无对象创建。
在bean.xml中配置如下
<bean id="accountService" class="com.jh.service.impl.AccountServiceImpl"></bean>
出现如下情况就会报错:
1.2 使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
普通工厂方法:getAccountService()
package com.jh.factory;
import com.jh.service.IAccountService;
import com.jh.service.impl.AccountServiceImpl;
/**检测第二种创建bean的方式:普通方法
*
* 模拟一个工厂类(该类可能存在于jar包中,我们无法通过修改源码的方式来提供默认构造函数)
* */
public class InstanceFactory {
public IAccountService getAccountService(){
return new AccountServiceImpl();
}
}
在bean.xml中配置如下
<bean id="instanceFactory" class="com.jh.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
上述通过如下线路调用
1.3 使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring对象)
静态方法:static IAccountService getAccountService()
package com.jh.factory;
import com.jh.service.IAccountService;
import com.jh.service.impl.AccountServiceImpl;
/**检测第三种创建bean的方式:静态方法
*
* 模拟一个工厂类(该类可能存在于jar包中,我们无法通过修改源码的方式来提供默认构造函数)
* */
public class StaticFactory {
public static IAccountService getAccountService(){
return new AccountServiceImpl();
}
}
在bean.xml中配置如下
<bean id="accountService" class="com.jh.factory.StaticFactory" factory-method="getAccountService"></bean>
2. bean的作用范围调整
bean标签的scope属性:
作用:用于指定bean的作用范围; 取值:常用的就是单例的和多例的
singleton : 单例的(默认值)
prototype : 多例的
request : 作用于web应用的请求范围
session : 作用于web应用的会话范围
global-session : 作用于集群环境(多个服务器)的会话范围(全局会话范围),当不是集群环境时,它就是session
bean.xml配置
<bean id="accountService" class="com.jh.service.impl.AccountServiceImpl" scope="singleton"></bean>
3. bean对象的生命周期
单例对象
出生:当容器创建时对象出生
活着:只要容器还在,对象一直活着
死亡:容器销毁,对象消亡
总结:单例对象的生命周期和容器相同
多例对象
出生:当我们使用对象时spring框架为我们创建
活着:对象只要是在使用过程中就一直活着。
死亡:当对象长时间不用,且没有别的对象引用时,由Java的垃圾回收器回收
bean.xml配置
<bean id="accountService" class="com.jh.service.impl.AccountServiceImpl"
scope="singleton" init-method="init" destroy-method="destroy"></bean>
4.案例
AccountServiceImpl.java实现类
package com.jh.service.impl;
import com.jh.service.IAccountService;
public class AccountServiceImpl implements IAccountService {
public AccountServiceImpl(){
System.out.println("对象创建了");
}
public void saveAccount(){
System.out.println("AccountServiceImpl执行了");
}
public void init(){
System.out.println("对象初始化了。。。");
}
public void destroy(){
System.out.println("对象销毁了。。。。");
}
}
IAccountService接口
package com.jh.service;
public interface IAccountService {
void saveAccount();
}
表现层 Client.java
package com.jh.ui;
import com.jh.service.IAccountService;
import com.jh.service.impl.AccountServiceImpl;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
/**
* 模拟一个表现层,用于调用业务层
* */
public class Client {
public static void main(String[] args) {
//--------ApplicationContext 立即加载----------------------
//1.获取核心容器对象
//ApplicationContext ac= new ClassPathXmlApplicationContext("bean.xml");
ClassPathXmlApplicationContext ac= new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取Bean对象
IAccountService as= (IAccountService)ac.getBean("accountService");
System.out.println(as);//com.jh.service.impl.AccountServiceImpl@1dd92fe2
as.saveAccount();
//手动关闭容器
ac.close();
}
}