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. IDEA将Git本地仓库Push至远程仓库

    转自:https://blog.csdn.net/qq_15653601/article/details/79870996 本地本地仓库项目: 配置Git自动识别本机Git配置

  2. redhat与zlib兼容性问题?

    今天在redhat 6.3 x64版本上安装了zlib,安装完后可以正常使用,就是发现gedit使用有点异常——无法启动,当时也没在意,但是后来重启电脑后出现桌面背景图片后就不弹出登陆窗口了,但是进命 ...

  3. D2.Docker: 安装部署相关问题

    [mysql] docker 安装完mysql 后客户端无法访问

  4. springboot(3):整合Servlet,filter,listener

    1.springboot整合Servlet(2种方式) 添加maven依赖:spring-boot-starter-web 1>通过注解扫描完成Servlet组件的注册(方式1) 步骤:需要3步 ...

  5. ListView控件的理解——自洽理论

    写在前面的话: *标题中已经说明,是自洽理论.因此,有几率会有理解错误.但是,你不可以因此骂我. -我这个人经不起别人的批评,如果你批评我,我就,我就.... ## <第一行代码>读书笔记 ...

  6. C++ 字符串处理类 ProcessString (包含常用字符串处理函数)

    ProcessString.h //Linux & C++11 #pragma once //包含系统头文件 #include <string> #include <sstr ...

  7. Python07之分支和循环2(if...else、if...elif...else)

    一:if语句具体语法: if 表达式: 语句块 (表达式可以是一个布尔值或变量,也可以为一个逻辑表达式或比较表达式,表达式为真(即不为0即可,见下方实例),则运行语句块:表达式为假,则跳过语句块,继续 ...

  8. PowerBuilder学习笔记之2PowerScript语言(三)

    教材地址:https://wenku.baidu.com/view/1e82d26925c52cc58ad6be05.html?sxts=1565679996440 2.6嵌入式SQL语句 2.6.1 ...

  9. Linux磁盘管理系列 — 磁盘配额管理

    一.磁盘管理的概念 Linux系统是多用户任务操作系统,在使用系统时,会出现多用户共同使用一个磁盘的情况,如果其中少数几个用户占用了大量的磁盘空间,势必压缩其他用户的磁盘的空间和使用权限.因此,系统管 ...

  10. vue npm run build 失败

    之前删除过 node-moudel 文件夹,然后 npm install 重新安装,一切OK.打包的时候,报错,找不到caniuse什么的.再删除node-moudel,重新cnpm install ...