Day2 Spring初识(二)
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初识(二)的更多相关文章
- Redis实战之征服 Redis + Jedis + Spring (二)
不得不说,用哈希操作来存对象,有点自讨苦吃! 不过,既然吃了苦,也做个记录,也许以后API升级后,能好用些呢?! 或许,是我的理解不对,没有真正的理解哈希表. 相关链接: Redis实战 Redis实 ...
- Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控
Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控 Spring Boot Actuator提供了对单个Spring Boot的监控,信息包含: ...
- spring batch(二):核心部分(1):配置Spring batch
spring batch(二):核心部分(1):配置Spring batch 博客分类: Spring 经验 java chapter 3.Batch configuration 1.spring ...
- Spring Boot 二十个注解
Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...
- python day2:python 初识(二)
大纲: 一.运算符 1.算数运算符 notice: 除法运算在python2.7和python3.x 的不同 2.比较运算符 3.赋值运算符 4.逻辑运算符 5.成员运算符 二.基本数据类型和方法介绍 ...
- Day2 Mybatis初识(二)
mapper接口开发 传统dao的开发问题(ibatis) 方法调用:字符串易错,硬编码 mapper代理开发 a) 编写全局配置 b) 编写接口(自动根据接口和映射文件创建实现类) c) 编写映射文 ...
- Spring(二)__bean的装配
Bean的装配: 在spring容器内拼凑bean叫做装配.装 配bean的时候,需要告诉容器哪些bean 以及容器如何使用依赖注入将它们配合在一起. 上下文定义文件的根元素是<beans> ...
- 攻城狮在路上(贰) Spring(二)--- Spring IoC概念介绍
一.IoC的概念: IoC(控制反转)是Spring容器的核心.另一种解释是DI(依赖注入),即让调用类对某一个接口的依赖关系由第三方注入,以移除调用类对某一个接口实现类的一览. 定义如此,由此可见, ...
- 文件上传和下载(可批量上传)——Spring(二)
针对SpringMVC的文件上传和下载.下载用之前“文件上传和下载——基础(一)”的依然可以,但是上传功能要修改,这是因为springMVC 都为我们封装好成自己的文件对象了,转换的过程就在我们所配置 ...
随机推荐
- Django(三):HttpRequest和HttpResponse
当一个请求连接进来时,django会创建一个HttpRequest对象来封装和保存所有请求相关的信息,并且会根据请求路由载入匹配的视图函数.每个请求的视图函数都会返回一个HttpResponse. H ...
- 14、通过jpa往数据库插数据
这是接着上一篇写的,在上一篇的基础上添加 Controller @RestController public class HelloController { @Resource private Hel ...
- SQL0286N 找不到页大小至少为 "8192"、许可使用授权标识 "db2inst" 的缺省表空间。
在 SQL 处理期间,它返回: SQL0286N 找不到页大小至少为 "8192".许可使用授权标识 "db2inst" 的缺省表空间. 顾名思义,DB2默认 ...
- [js常用]将秒转化为时分秒
内容引入至网络 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...
- oracle 企业管理器及无线网环境下配置方法
注意: oracle em 的访问地址在 D:\oracle\product\11.2.0\dbhome_1\install 下的文件里. 如果在windows 安装oracle,并且在本地访问,or ...
- 为什么不要使用 select * from xxx (oracle 亲测)
打开已用时间set timing on;create table users(id number(20), name varchar2(20), password varchar2(20));inse ...
- mysql 安装版
安装 1.MySQL的安装类型选择: 在“Choose Setup Type”对话框有“Typical”默认安装类型:“complete"完全安装类型:Custom自定义安装类型. 我们选择 ...
- Mybatis显示SQL语句
众所周知,hibernate可以通过配置show_sql在控制台显示sql语句,Mybatis可不可以呢?当然是可以的,将ibatis log4j运行级别调到DEBUG可以在控制台打印出ibatis运 ...
- 1.创建maven 项目 动态web工程完整示例
注意,以下所有需要建立在你的eclipse等已经集成配置好了maven了,说白了就是新建项目的时候已经可以找到maven了 没有的话需要安装maven 一.创建项目 1.新建maven项目,如果不在上 ...
- CentOS6.4 下安装 Apache2.4.16
1.准备工作 1.1.yum安装部分工具 1)yum -y install vim 2)yum -y install wget 3)yum -y install gcc 4)yum -y instal ...