Bean的实例化

bean实例化方式有3种:默认构造、静态工厂、实例工厂

默认构造

调用无参构造, 属性+setter

User.java

package entity;

public class User {
private int id;
private String name; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
} }

ApplicatitonContext.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"> <!-- 1.默认调用无参构造 属性+setter
name:真实赋值使用setter方法
scope:作用域
-->
<bean id="user" class="entity.User">
<property name="id" value="1"></property>
<property name="name" value="zs"></property>
</bean> </beans>

Test.java

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import entity.User; public class Test { @org.junit.Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicatitonContext.xml");
User user = context.getBean("user",User.class);
System.out.println(user);
}
}

运行结果

二月 01, 2018 8:19:43 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2a33fae0: startup date [Thu Feb 01 20:19:43 CST 2018]; root of context hierarchy
二月 01, 2018 8:19:44 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [ApplicatitonContext.xml]
User [id=1, name=zs]

调用有参构造      属性+有参构造

User.java

package entity;

public class User {
private int id;
private String name; public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
}
}

ApplicatitonContext.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"> <!-- 2.调用有参构造 属性+有参构造
constructor-arg:
name:属性名称
value:简单类型的值
ref:引用 index:参数的索引
type:参数的类型
-->
<bean id="user" class="entity.User">
<constructor-arg name="id" value="1" ></constructor-arg>
<constructor-arg name="name" value="lisi"></constructor-arg> <!-- <constructor-arg index="0" type="int" value="1"></constructor-arg>
<constructor-arg index="1" value="zs"></constructor-arg> -->
</bean> </beans>

Test.java

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import entity.User; public class Test { @org.junit.Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicatitonContext.xml");
User user = context.getBean("user",User.class);
System.out.println(user);
}
}

运行结果

二月 01, 2018 8:23:39 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2a33fae0: startup date [Thu Feb 01 20:23:39 CST 2018]; root of context hierarchy
二月 01, 2018 8:23:39 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [ApplicatitonContext.xml]
User [id=1, name=lisi]

静态工厂

User.java

package entity;

public class User {
private int id;
private String name; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
} }

StaticFactory.java

package factory;

import entity.User;

public class StaticFactory {

    public static User createUser() {
return new User();
}
}

ApplicatitonContext.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="user" class="factory.StaticFactory" factory-method="createUser"></bean> </beans>

Test.java

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import entity.User; public class Test { @org.junit.Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicatitonContext.xml");
User user = context.getBean("user",User.class);
System.out.println(user);
}
}

运行结果

二月 01, 2018 8:28:44 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2a33fae0: startup date [Thu Feb 01 20:28:44 CST 2018]; root of context hierarchy
二月 01, 2018 8:28:44 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [ApplicatitonContext.xml]
User [id=0, name=null]

动态工厂

User.java

package entity;

public class User {
private int id;
private String name; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
} }

InstanceFactory.java

package factory;

import entity.User;

public class InstanceFactory {

    public User createUser(){
return new User();
}
}

ApplicatitonContext.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"> <!-- 3.实例工厂 -->
<bean id="factory" class="factory.InstanceFactory"></bean>
<bean id="user" factory-bean="factory" factory-method="createUser"></bean> </beans>

Test.java

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import entity.User; public class Test { @org.junit.Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicatitonContext.xml");
User user = context.getBean("user",User.class);
System.out.println(user);
}
}

运行结果

二月 01, 2018 8:32:59 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2a33fae0: startup date [Thu Feb 01 20:32:59 CST 2018]; root of context hierarchy
二月 01, 2018 8:32:59 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [ApplicatitonContext.xml]
User [id=0, name=null]

Bean的作用域

  • singleton:(单实例模式)spring容器只会存在一个共享的bean实例,并且所有针对该bean的请求只会返回同一个bean实例。
  • propertype(no-singleton):对每一次针对该bean的请求都会生成一个新的bean实例。 相当于java中的new 操作。定义为propertype的bean其生命周期很长,不易回收,通常要额外的处理。
  • request:针对每一次的http请求都会产生一个新的bean实例,Bean仅在当前的http request范围内有效
  • session:针对每一次的http请求都会产生一个新的bean实例,Bean仅在当前的http session范围内有效

Bean的声明周期

图示说明

初始化方法和销毁方法

User.java

package entity;

public class User {
private int id;
private String name; public void init() {
System.out.println("初始化");
} public void destory() {
System.out.println("销毁");
} public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]";
} }

ApplicatitonContext.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"> <!-- 1.默认调用无参构造 属性+setter
name:真实赋值使用setter方法
scope:作用域
-->
<bean id="user" class="entity.User" init-method="init" destroy-method="destory">
<property name="id" value="1"></property>
<property name="name" value="zs"></property>
</bean> </beans>

Test.java

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import entity.User; public class Test { @org.junit.Test
public void test() {
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicatitonContext.xml");
User user = context.getBean("user",User.class);
System.out.println(user);
//容器关闭,必须使用单例默认,只有容器关闭之后才能调用destroy方法
((ClassPathXmlApplicationContext)context).close();
}
}

运行结果

二月 01, 2018 8:44:32 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2a33fae0: startup date [Thu Feb 01 20:44:32 CST 2018]; root of context hierarchy
二月 01, 2018 8:44:32 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [ApplicatitonContext.xml]
初始化
二月 01, 2018 8:44:32 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@2a33fae0: startup date [Thu Feb 01 20:44:32 CST 2018]; root of context hierarchy
User [id=1, name=zs]
销毁

生命周期管理

两个时机

Spring可以管理实例化bean之间以及销毁之前的行为

注入依赖关系之后:

  • 使用init-method属性:通过指定init-method属性,确定某个方法应该在Bean依赖关系结束之后执行。这种方式无需要将代码与Spring的接口耦合在一起代码污染极小。通常在bean当中进行方法定义如init()方法,然后在配置文件Bean元素中加入init-method属性来实现这个过程。
  • 实现InnitializingBean接口:这种方式无须指明init-method属性,当窗口依赖注入以后,会自动调用afterPropertiesSet方法,它和init-method执行效果一样,但这种方式属于侵入性的代码设计不推荐使用

销毁Bean之前:

  • destroy-method:用于在执行Bean销毁之前所执行的方法,这种方式和init-method一样无压需要代码与Spring的接口耦合在一起代码污染极小。在bean中加入destory-method属性和实现这个过程
  • 实现DisposeableBean接口:无需要指明destory-method属性,当容器依赖注入以后,会自动调用destroty方法,属于侵入性代码设计不推荐使用

Day2 Spring初识(二)的更多相关文章

  1. Redis实战之征服 Redis + Jedis + Spring (二)

    不得不说,用哈希操作来存对象,有点自讨苦吃! 不过,既然吃了苦,也做个记录,也许以后API升级后,能好用些呢?! 或许,是我的理解不对,没有真正的理解哈希表. 相关链接: Redis实战 Redis实 ...

  2. Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控

    Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控 Spring Boot Actuator提供了对单个Spring Boot的监控,信息包含: ...

  3. spring batch(二):核心部分(1):配置Spring batch

    spring batch(二):核心部分(1):配置Spring batch 博客分类: Spring 经验 java   chapter 3.Batch configuration 1.spring ...

  4. Spring Boot 二十个注解

    Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...

  5. python day2:python 初识(二)

    大纲: 一.运算符 1.算数运算符 notice: 除法运算在python2.7和python3.x 的不同 2.比较运算符 3.赋值运算符 4.逻辑运算符 5.成员运算符 二.基本数据类型和方法介绍 ...

  6. Day2 Mybatis初识(二)

    mapper接口开发 传统dao的开发问题(ibatis) 方法调用:字符串易错,硬编码 mapper代理开发 a) 编写全局配置 b) 编写接口(自动根据接口和映射文件创建实现类) c) 编写映射文 ...

  7. Spring(二)__bean的装配

    Bean的装配: 在spring容器内拼凑bean叫做装配.装 配bean的时候,需要告诉容器哪些bean 以及容器如何使用依赖注入将它们配合在一起. 上下文定义文件的根元素是<beans> ...

  8. 攻城狮在路上(贰) Spring(二)--- Spring IoC概念介绍

    一.IoC的概念: IoC(控制反转)是Spring容器的核心.另一种解释是DI(依赖注入),即让调用类对某一个接口的依赖关系由第三方注入,以移除调用类对某一个接口实现类的一览. 定义如此,由此可见, ...

  9. 文件上传和下载(可批量上传)——Spring(二)

    针对SpringMVC的文件上传和下载.下载用之前“文件上传和下载——基础(一)”的依然可以,但是上传功能要修改,这是因为springMVC 都为我们封装好成自己的文件对象了,转换的过程就在我们所配置 ...

随机推荐

  1. Spark of work

    Today I attended a meeting of reviewing code,  and I learned a lot from it. In the discuss, we found ...

  2. .NET Core 2.1路线图

    Microsoft的Scott Hunter发布了Microsoft .NET Core 2.1版本的路线图.Hunter宣布Microsoft .NET Core每天约有五十万开发人员的使用量.根据 ...

  3. android之在view中内嵌浏览器的方法

    我要做的一个东西是在一个页面的中间嵌入浏览器,一开始不知道从哪里开始,因为以前用的都是Textveiw或者editVeiw之类的控件,而它们并不能用来显示网页的内容,怎么办呢? 首先想到的是:是不是有 ...

  4. JS基础(四)之jQuery

    31.jQuery(http://jquery.com/)是一个快速.简洁的JavaScript框架. 它封装了JavaScript常用的功能代码,提供一种便捷的JavaScript设计模式,优化HT ...

  5. html基础-a标签-img标签-绝对/相对路径(3)

    美好的星期六,今天多写一点,争取早点写js这个有点小无聊. 一.先来讲点网页之间的跳转 (1).<a href=""></a>  href="这里 ...

  6. Dynamics 365Online 查询Web Api的请求WebUri

    在on-premises版本中,获取weburi的方式是进设置-自定义项-开发人员资源中查看地址,但online版本中的地址会有些许的差异 online的开发者资源中的地址如下图,如果你在页面java ...

  7. 玩转Android拍摄功能

    简单拍照与摄像 在富媒体开始流行之前,整个世界是一个灰暗且平淡无奇的地方.还记得Gopher吗?我或许不记得了.自从APP成为用户生活的一部分之后,这便给他们提供了一种方式可以来存放他们生活的细节.使 ...

  8. git远程仓库问题

    1:下载下来的仓库,可能变更远程仓库 git remote rm origin (origin默认的远程仓库名) 可以在.git文件夹下的config文件查看remote的信息. 同时也可以查看bra ...

  9. git 错误error: failed to push some refs to

    今天使用VSCODE 学习node.js,  想在git上push代码 于是在git上建立了一个私有的长裤, 连接后push代码时提示如下错误: error: failed to push some ...

  10. windows域渗透实战

    测试环境 域控: 192.168.211.130 已经控制的机器: 192.168.211.133 获取网络信息 查看机器的网络信息 ipconfig /all # 查看 网卡信息,获取dns 服务器 ...