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的管理的更多相关文章

  1. spring对bean的管理细节

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  2. Spring中bean的管理

    Spring 中常见的容器 我们知道spring容器就是spring中bean的驻留场所.spring容器并不是只有一个.spring自带了多个容器实现,可以归为两种不同的类型:bean工厂和应用上下 ...

  3. Spring之Bean的管理方式(Content,Beans)

    Spring的bean管理(注释) 注解 代码里特殊的标记,使用注解也可以直接完成相关功能 注解写法:@注解名称(属性名=属性值) 使用在类,方法,属性上面 Spring注解开发准备 导入jar包 ( ...

  4. IOC——Spring的bean的管理(xml配置文件)

    Bean实例化(三种方式) 1.使用类的无参构造进行创建(大多数情况下) <bean id="user" class="com.bjxb.ioc.User" ...

  5. Spring中Bean的管理问题

    首先,配置文件中定义的bean并不是都在启动时实例化. <bean id="accountService" class="com.foo.DefaultAccoun ...

  6. 03 Spring框架 bean的属性以及bean前处理和bean后处理

    整理了一下之前学习spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节我们给出了三个小demo,具体的流程是这样的: 1.首先在aplicationContext.xml中添加< ...

  7. IOC——Spring的bean的管理(注解方式)

    注解(简单解释) 1.代码里面特殊标记,使用注解可以完成一定的功能 2.注解写法 @注解名称(属性名称=属性值) 3.注解使用在类上面,方法上面和属性上面 注意:注解方式不能完全替代配置文件方式 Sp ...

  8. 阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式

    目前这里能调用是因为,在service的实现类里面,new了一个dao的对象 正常情况下 这里不应该是new一个对象,应该等于null或为空 设置为空侯再运行就会报错 出错的原因是这里为null 需要 ...

  9. Spring(二)Bean入门

    一.BeanFactory介绍 1.1.Bean: 在Spring技术中是基于组件的 最基本了是最常用的单元 其实实例保存在Spring的容器当中 Bean通常被定义在配置文件当中,Bean实例化由S ...

随机推荐

  1. Java IO把一个文件中的内容以字符串的形式读出来

    代码记录(备查): /** * 把一个文件中的内容以字符串的形式读出来 * * @author zhipengs * */ public class FileToString { public sta ...

  2. java多线程实现多客户端socket通信

    一.服务端 package com.czhappy.hello.socket; import java.io.IOException; import java.net.InetAddress; imp ...

  3. linux系统界面转换

    普通使用的切换: 命令行->图形 startx 或者 ctrl+alt+F7切换到图形界面,虚拟机里面使用Alt+F7返回到图形界面 图形->命令行 Ctrl+Alt+F1--F6 如果想 ...

  4. 与TypeScript的一场美丽邂逅

    TypeScript(一)前言:当你点开这篇文章时,我相信你已经在很多地方都已经听说过或者见过TypeScript了.但是可能对TypeScript依然有很多问号:TypeScript到底是什么?为什 ...

  5. .NET开发的一些积累

    ASP.NET项目开发一些琐碎的积累 1.过滤危险的字符串,诸如“=”.“>”等可能会诸如数据库的危险字符串,我看过很多人做的网页仅仅进行客户端脚本验证是不够的.必须在服务器段的后台代码里面也进 ...

  6. LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)

    718. 最长重复子数组 718. Maximum Length of Repeated Subarray 题目描述 给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的 ...

  7. 关于工作中.net转java遇到的一个远程调用传递重复参的问题。

    工作中遇到一个很奇怪的传参问题.之前.net使用的是一个List列表,列表中有几个重复的参数.列表中使用的model类是KeyValue. 我使用java模仿其写法,传递List和KeyValue.对 ...

  8. golang之结构体使用注意事项和细节

    1. 结构体的所有字段在内在中是连续的 2. 结构体是用户单独定义的类型,和其它类型进行转换时需要有完全相同的字段(名字.个数和类型) 3. 结构体进行type重新定义(相当于取别名),Golang认 ...

  9. 1010 Radix:猥琐的测试数据

    谨以此题纪念边界测试数据浪费了我多少时间:https://pintia.cn/problem-sets/994805342720868352/problems/994805507225665536 # ...

  10. The 2018 ACM-ICPC Asia Nanjing Regional Programming Contest

    A. Adrien and Austin 大意: $n$个石子, 编号$1$到$n$, 两人轮流操作, 每次删除$1$到$k$个编号连续的石子, 不能操作则输, 求最后胜负情况. 删除一段后变成两堆, ...