03 Spring对Bean的管理
Spring创建bean的三种方式
1.第一种方式:使用默认构造函数创建
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">
<!-- 默认情况下使用的就是无参数的构造方法. -->
<bean id="accountService" class="com.itzn.service.AccountServiceImpl"></bean>
</beans>
AccountServiceImpl.java
package com.itzn.service;
import com.itzn.dao.AccountDaoImpl;
import com.itzn.dao.IAccountDao;
public class AccountServiceImpl implements IAccountService {
public AccountServiceImpl(){
System.out.println("默认构造");
}
public void save() {
System.out.println("保存方法");
}
}
测试:AccountTest.java
package com.itzn.ui; import com.itzn.dao.AccountDaoImpl;
import com.itzn.dao.IAccountDao;
import com.itzn.service.AccountServiceImpl;
import com.itzn.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AccountTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService iAccountService = (IAccountService) ac.getBean("accountService");
System.out.println(iAccountService); iAccountService.save();
}
}
输出结果:

2.第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
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">
<!-- 第一种方式:默认情况下使用的就是无参数的构造方法.
<bean id="accountService" class="com.itzn.service.AccountServiceImpl"></bean>
-->
<!-- 第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器) -->
<bean id="beanFactory" class="com.itzn.factory.BeanFactory"></bean>
<bean id="accountService" factory-bean="beanFactory" factory-method="getASIBean"></bean>
</beans>
BeanFactory.java
package com.itzn.factory; import com.itzn.service.AccountServiceImpl;
public class BeanFactory {
public AccountServiceImpl getASIBean(){
System.out.println("BeanFactory实例工厂的getASIBean方法...");
return new AccountServiceImpl();
}
}
AccountTest .java
package com.itzn.ui; import com.itzn.dao.AccountDaoImpl;
import com.itzn.dao.IAccountDao;
import com.itzn.service.AccountServiceImpl;
import com.itzn.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AccountTest {
public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService iAccountService = (IAccountService) ac.getBean("accountService");
System.out.println(iAccountService); iAccountService.save();
}
}
测试结果:

3.第三种方式:第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器)
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">
<!-- 第一种方式:默认情况下使用的就是无参数的构造方法.
<bean id="accountService" class="com.itzn.service.AccountServiceImpl"></bean>
-->
<!-- 第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
<bean id="beanFactory" class="com.itzn.factory.BeanFactory"></bean>
<bean id="accountService" factory-bean="beanFactory" factory-method="getASIBean"></bean>
-->
<!-- 第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器) -->
<bean id="accountService" class="com.itzn.factory.StaticFactory" factory-method="getASIBean"></bean>
</beans>
StaticFactory.java
package com.itzn.factory;
import com.itzn.service.AccountServiceImpl;
public class StaticFactory { public static AccountServiceImpl getASIBean(){
System.out.println("StaticFactory实例工厂的getASIBean方法...");
return new AccountServiceImpl();
}
}
AccountTest.java
package com.itzn.ui;
import com.itzn.dao.AccountDaoImpl;
import com.itzn.dao.IAccountDao;
import com.itzn.service.AccountServiceImpl;
import com.itzn.service.IAccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class AccountTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService iAccountService = (IAccountService) ac.getBean("accountService");
System.out.println(iAccountService); iAccountService.save();
}
}
测试结果:

bean对象的作用范围
scope属性 :
* singleton:单例的.(默认的值.)
* prototype:多例的.
* request:web开发中.创建了一个对象,将这个对象存入request范围,request.setAttribute();
* session:web开发中.创建了一个对象,将这个对象存入session范围,session.setAttribute();
* globalSession:一般用于Porlet应用环境.指的是分布式开发.不是porlet环境,globalSession等同于session;
实际开发中主要使用singleton,prototype
Customer.java
package cn.itzn.srping.demo3;
public class Customer {
public Customer(){
System.out.println("Customer被实例化了");
}
}
bean.xml
//单例配置
<bean id="customer" class="cn.itzn.srping.demo3.Customer"></bean>
//多例配置
<bean id="customer" class="cn.itzn.srping.demo3.Customer" scope="prototype"></bean>
SpringTest.java
public class SpringTest {
@Test
public void Dome1()
{
ApplicationContext myContext=new ClassPathXmlApplicationContext("bean.xml");
Customer customer1=(Customer) myContext.getBean("customer");
System.out.println(customer1);
Customer customer2=(Customer) myContext.getBean("customer");
System.out.println(customer2);
}
}
bean对象的生命周期
单例对象:scope="singleton"
一个应用只有一个对象的实例。它的作用范围就是整个引用。
生命周期:
对象出生:当应用加载,创建容器时,对象就被创建了。
对象活着:只要容器在,对象一直活着。
对象死亡:当应用卸载,销毁容器时,对象就被销毁了。
多例对象:scope="prototype"
每次访问对象时,都会重新创建对象实例。
生命周期:
对象出生:当使用对象时,创建新的对象实例。
对象活着:只要对象在使用中,就一直活着。
对象死亡:当对象长时间不用时,被 java 的垃圾回收器回收了。
03 Spring对Bean的管理的更多相关文章
- spring对bean的管理细节
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- Spring中bean的管理
Spring 中常见的容器 我们知道spring容器就是spring中bean的驻留场所.spring容器并不是只有一个.spring自带了多个容器实现,可以归为两种不同的类型:bean工厂和应用上下 ...
- Spring之Bean的管理方式(Content,Beans)
Spring的bean管理(注释) 注解 代码里特殊的标记,使用注解也可以直接完成相关功能 注解写法:@注解名称(属性名=属性值) 使用在类,方法,属性上面 Spring注解开发准备 导入jar包 ( ...
- IOC——Spring的bean的管理(xml配置文件)
Bean实例化(三种方式) 1.使用类的无参构造进行创建(大多数情况下) <bean id="user" class="com.bjxb.ioc.User" ...
- Spring中Bean的管理问题
首先,配置文件中定义的bean并不是都在启动时实例化. <bean id="accountService" class="com.foo.DefaultAccoun ...
- 03 Spring框架 bean的属性以及bean前处理和bean后处理
整理了一下之前学习spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节我们给出了三个小demo,具体的流程是这样的: 1.首先在aplicationContext.xml中添加< ...
- IOC——Spring的bean的管理(注解方式)
注解(简单解释) 1.代码里面特殊标记,使用注解可以完成一定的功能 2.注解写法 @注解名称(属性名称=属性值) 3.注解使用在类上面,方法上面和属性上面 注意:注解方式不能完全替代配置文件方式 Sp ...
- 阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
目前这里能调用是因为,在service的实现类里面,new了一个dao的对象 正常情况下 这里不应该是new一个对象,应该等于null或为空 设置为空侯再运行就会报错 出错的原因是这里为null 需要 ...
- Spring(二)Bean入门
一.BeanFactory介绍 1.1.Bean: 在Spring技术中是基于组件的 最基本了是最常用的单元 其实实例保存在Spring的容器当中 Bean通常被定义在配置文件当中,Bean实例化由S ...
随机推荐
- solr搜索结果转实体类对象的两种方法
问题:就是把从solr搜索出来的结果转成我们想要的实体类对象,很常用的情景. 1.使用@Field注解 @Field这个注解放到实体类的属性[字段]中,例如下面 public class User{ ...
- 小程序里实现 watch 和 computed
小程序里的自定义组件里是有数据监听器的,可以监听对应数据的变化来执行callBack,但是页面Page里没有对应的api就显的很生硬,比如某个数据变了(如切换城市)需要重新刷页面,如果不做监听,每次都 ...
- MacOs 10.14.3 Matlab2018b 解决“找不到已安装的编译器 'Xcode Clang++'。错误使用mex,未找到合适的编译器”问题
这是目前网上state of the art 的解决方案: 最开始用的是matlab2017,结果爆了这么一个问题“找不到已安装的编译器 'Xcode Clang++'.错误使用mex,未找到合适的编 ...
- Variational Auto-encoder(VAE)变分自编码器-Pytorch
import os import torch import torch.nn as nn import torch.nn.functional as F import torchvision from ...
- TensorSpace:超酷炫3D神经网络可视化框架
TensorSpace:超酷炫3D神经网络可视化框架 TensorSpace - 一款 3D 模型可视化框架,支持多种模型,帮助你可视化层间输出,更直观地展示模型的输入输出,帮助理解模型结构和输出方法 ...
- 用pytorch1.0搭建简单的神经网络:进行多分类分析
用pytorch1.0搭建简单的神经网络:进行多分类分析 import torch import torch.nn.functional as F # 包含激励函数 import matplotlib ...
- Session中清除对象方法比较
转载. https://blog.csdn.net/u014401141/article/details/51816308 Session中清除对象方法比较 http://blog.csdn.ne ...
- 第1章:LeetCode--基础部分
LeetCode刷题指导(不能只实现功能就行需要尽量写最优解): 不可能刷遍所有题,只能通过刷题来恢复写代码的功力,熟悉常用算法(暴力算法.冒泡排序/快速排序.strStr KMP算法.二叉树遍历DF ...
- css拓展
自定义居中 .second-listleft{/*固定位置*/ position: relative; float: left; width: 25%; height: 100%; backgroun ...
- Python调用API接口的几种方式
Python调用API接口的几种方式 相信做过自动化运维的同学都用过API接口来完成某些动作.API是一套成熟系统所必需的接口,可以被其他系统或脚本来调用,这也是自动化运维的必修课. 本文主要介绍py ...