目录
- 项目结构
- 创建bean的三种方式
- bean对象的作用范围
- bean对象的生命周期
项目结构
- AccountServiceImpl.java
package com.itheima.service.impl;
import com.itheima.service.IAccountService;
/**
* 账户的业务层实现类
*/
public class AccountServiceImpl implements IAccountService {
public AccountServiceImpl(){
System.out.println("AccountService创建了。");
}
public void saveAccount(){
System.out.println("service中的saveAccount執行了。");
}
}
- IAccountService.java
package com.itheima.service;
/**
* 账户业务层的接口
*/
public interface IAccountService {
/**
* 模拟保存账户
*/
void saveAccount();
}
- Client.java
package com.itheima.ui;
import com.itheima.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 模拟一个表现层,用于调用业务层
*/
public class Client {
/**
* 获取spring的IOC核心容器,并根据id获取对象
* @param args
*/
public static void main(String[] args) {
//1、获取核心容器对象。配置文件在根目录,直接写文件名即可。
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2、根据id获取bean对象
IAccountService as = (IAccountService) ac.getBean("accountService");
as.saveAccount();
}
}
- 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来管理-->
<!--Spring对bean的管理细节
1、创建bean的三种方式
2、bean对象的作用范围
3、bean对象的生命周期-->
<!--创建bean的三种方式-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
</beans>
创建bean的三种方式
-
第一种方式:使用默认构造函数创建。
在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时,采用的就是默认构造函数创建bean对象,此时如果没有默认构造函数,则对象创建无法创建。 - 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来管理-->
<!--Spring对bean的管理细节
1、创建bean的三种方式
2、bean对象的作用范围
3、bean对象的生命周期-->
<!--创建bean的三种方式-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
</beans>
- AccountServiceImpl.java
/**
* 账户的业务层实现类
*/
public class AccountServiceImpl implements IAccountService {
//没有重写默认构造函数
public AccountServiceImpl(String name){
System.out.println("AccountService创建了。");
}
public void saveAccount(){
System.out.println("service中的saveAccount執行了。");
}
}
报错:Failed to instantiate: No default constructor.
<!--第二种方式-->
<bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
- InstanceFactory.java
/**
* 模拟一个工厂类,该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造函数
*/
public class InstanceFactory {
public IAccountService getAccountService(){
return new AccountServiceImpl();
}
}
<!--第三种方式-->
<bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>
- StaticFactory.java
/**
* 模拟一个工厂类,该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造函数
*/
public class StaticFactory {
public static IAccountService getAccountService(){
return new AccountServiceImpl();
}
}
在jar包中的方法,希望通过spring容器来获取,可以使用第二种和第三种方式。
bean对象的作用范围
在工厂模式解耦的博客中,我们说过spring的bean一般都是单例模式的bean。
Spring的bean对象默认情况下是单例的。
-
bean标签的scope属性
用于指定bean的作用范围。
取值:
- singleton
- prototype
- request
- session
- global-session- singleton
单例,默认值。 - prototpye
多例 - request
作用于web应用的请求范围 - session
作用于web应用的会话范围 - global-session
作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session。在负载均衡中,验证码需要使用global-session,因为登录请求和输入验证码请求可能会被分发到不同的服务器上。
- singleton
bean对象的生命周期
-
单例对象
出生:当容器创建时,对象出生
活着:只要容器存在,对象一直活着
死亡:容器销毁,对象消亡
总结:单例对象的生命周期和容器相同- bean.xml
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="singleton" init-method="init" destroy-method="destory"></bean>
- AccountServiceImpl.java
/**
* 账户的业务层实现类
*/
public class AccountServiceImpl implements IAccountService {
public AccountServiceImpl(){
System.out.println("AccountService创建了。");
}
public void saveAccount(){
System.out.println("service中的saveAccount執行了。");
}
public void init(){
System.out.println("service中的saveAccount初始化了。");
}
public void destory(){
System.out.println("service中的saveAccount销毁了。");
}
}
- Client.java
/**
* 模拟一个表现层,用于调用业务层
*/
public class Client {
/**
* 获取spring的IOC核心容器,并根据id获取对象
* @param args
*/
public static void main(String[] args) {
//1、获取核心容器对象。配置文件在根目录,直接写文件名即可。
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2、根据id获取bean对象
IAccountService as = (IAccountService) ac.getBean("accountService");
as.saveAccount();
//手动销毁容器
((ClassPathXmlApplicationContext) ac).close();
}
}
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="prototype" init-method="init" destroy-method="destory"></bean>