Spring 讲解(二 )
1、Spring 容器加载的3种方式
public class ServiceTest {
public static void main(String[] args) {
//Spring容器加载有3种方式
//第一种:ClassPathXmlApplicationContext ClassPath类路径加载,指的就是classes路径
//第一种:最常用,spring的配置文件路径以后就直接放在src
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
//第二种方式:文件系统路径获得配置文件【绝对路径】
//ApplicationContext context = new FileSystemXmlApplicationContext("C:\\Users\\Desktop\\IDEAWorkspace\\spring-01\\src\\com\\rookie\\beans.xml");
//第三种方式:使用BeanFactory(了解)
//String path = "C:\\Users\\Desktop\\IDEAWorkspace\\spring-01\\src\\com\\rookie\\beans.xml";
//BeanFactory factory = new XmlBeanFactory(new FileSystemResource(path));
//IUserService user = (IUserService) factory.getBean("userService");
//user.add();
IUserService user = (IUserService) context.getBean("userService");
user.add();
}
}
Spring内部创建对象的原理
a.解析xml文件,获取类名,id,属性等。
b.通过反射,用类型创建对象。
c.给创建的对象赋值。
2、BeanFactory 和 ApplicationContext对比
BeanFactory 采取延迟加载,第一次 getBean 时才会初始化 Bean。
ApplicationContext 是即时加载,对 BeanFactory 扩展,提供了更多功能。
案例演示(在第一次的代码基础上)
public class UserServiceImpl implements UserService { private String name; public void setName(String name) {
this.name = name;
} @Override
public void add() {
System.out.println("创建用户...." + name);
} public UserServiceImpl(){
System.out.println("UserServiceImpl() 调用了");
}
}
public class ServiceTest { public static void main(String[] args) {
//1.加载beans.xml 这个spring的配置文件,内部就会创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
}
}
控制台打印日志:UserServiceImpl( ) 调用了
public class ServiceTest { public static void main(String[] args) {
String path = "C:\\Users\\Desktop\\IDEAWorkspace\\spring-01\\src\\com\\rookie\\beans.xml";
BeanFactory factory = new XmlBeanFactory(new FileSystemResource(path)); // 要使调用空参构造可以放开这段代码即可
// IUserService user = (IUserService) factory.getBean("userService");
}
}
控制台没有打印日志,说明空参构造没有被调用。
放开上面注释的代码,即可调用空参构造,控制台打印日志:UserServiceImpl( ) 调用了。
2、Spring 容器装配 bean 的 3 种方式
所谓的装配bean就是在xml写一个bean标签。
<?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的三种方式,所谓的装配bean就是在xml写一个bean标签-->
<!-- 第一种方式: new 实现类-->
<bean id="userService1" class="com.example.demo.service.impl.UserServiceImpl">
<property name="name" value="zhangsan"></property>
</bean>
<!-- 第二种方式:通过静态工厂方法 -->
<bean id="userService2" class="com.example.demo.service.UserServiceFactory" factory-method="createUserService"/>
<!--第三种方式:通过实例工厂方法 -->
<bean id="factory2" class="com.example.demo.service.UserServiceFactory2"/>
<bean id="userService3" factory-bean="factory2" factory-method="createUserService"/>
</beans>
测试上面哪种 bean 装配方式,需要注释掉其他 bean装配方式。
第一种上次已经讲过,现在只讲第二、三种案例。
public class UserServiceFactory {
public static UserService createUserService() {
return new UserServiceImpl();
}
}
=========================================================================================
public class UserServiceFactory2 {
public UserService createUserService() {
return new UserServiceImpl();
}
}
执行测试函数
public class ServiceTest {
public static void main(String[] args) {
//new 对象
ApplicationContext context1 = new ClassPathXmlApplicationContext("beans.xml");
UserService userService1 = (UserService) context1.getBean("userService1");
userService1.add();
//静态工厂
ApplicationContext context2 = new ClassPathXmlApplicationContext("beans.xml");
UserService userService2 = (UserService) context2.getBean("userService2");
userService2.add();
//实例工厂
ApplicationContext context3 = new ClassPathXmlApplicationContext("beans.xml");
UserService userService3 = (UserService) context3.getBean("userService3");
userService3.add();
}
}
感兴趣的可以自己看执行结果。三种结果都证明 bean 被 Spring 容器管理实例化了。
3、bean的作用域(掌握 singleton、prototype)
| 类别 | 说明 |
|---|---|
| singleton | 在Spring IoC容器中仅存在一个Bean实例,Bean以单例方式存在,默认值 |
| prototype | 每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时 ,相当于执行new XxxBean() |
| request | 每次HTTP请求都会创建一个新的Bean,该作用域仅适用于WebApplicationContext环境 |
| session | 同一个HTTP Session 共享一个Bean,不同Session使用不同Bean,仅适用于WebApplicationContext 环境 |
| globalSession | 一般用于Portlet应用环境,该作用域仅适用于WebApplicationContext 环境 |
案例代码演示
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="userService1" class="com.example.demo.service.impl.UserServiceImpl" scope="prototype">
</bean>
</beans>
public class ServiceTest {
public static void main(String[] args) {
ApplicationContext context1 = new ClassPathXmlApplicationContext("beans.xml");
UserService userService1 = (UserService) context1.getBean("userService1");
UserService userService2 = (UserService) context1.getBean("userService1");
System.out.println(userService1);
System.out.println(userService2);
}
}
控制台信息如下:
com.example.demo.service.impl.UserServiceImpl@2a556333
com.example.demo.service.impl.UserServiceImpl@7d70d1b1
如果去掉 bean.xml 文件的 scope="prototype",打印信息如下:
com.example.demo.service.impl.UserServiceImpl@7a187f14
com.example.demo.service.impl.UserServiceImpl@7a187f14
所以Spring IoC容器Bean以单例方式默认存在!!配置的话根据自己需要配置单例或者多例
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 注解的强大 ...
- 风炫安全web安全学习第三十七节课 15种上传漏洞讲解(二)
风炫安全web安全学习第三十七节课 15种上传漏洞讲解(二) 05后缀名黑名单校验之上传.htaccess绕过 还是使用黑名单,禁止上传所有web容器能解析的脚本文件的后缀 $is_upload = ...
- Spring Boot(二):数据库操作
本文主要讲解如何通过spring boot来访问数据库,本文会演示三种方式来访问数据库,第一种是JdbcTemplate,第二种是JPA,第三种是Mybatis.之前已经提到过,本系列会以一个博客系统 ...
- spring学习(二) ———— AOP之AspectJ框架的使用
前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...
- Spring(二十):Spring AOP(四):基于配置文件的方式来配置 AOP
基于配置文件的方式来配置 AOP 前边三个章节<Spring(十七):Spring AOP(一):简介>.<Spring(十八):Spring AOP(二):通知(前置.后置.返回. ...
- Spring系列(二):Spring IoC/DI的理解
这几天重新学习了一下Spring,在网上找了相关的ppt来看,当看到Spring IoC这一章节的时候,先大致浏览了一下内容,有将近50页的内容,内心窃喜~QAQ~,看完这些内容能够对IoC有更深层次 ...
随机推荐
- vagrant(一)初识与安装
链接:https://pan.baidu.com/s/1I2RNMNq9G-P4k42-U1Jjtw (这里有需要准备的安装包以及镜像文件,也可自己去官网下载)提取码:jis8 安装VirtualBo ...
- mysql 查询所有子节点
SELECT t3.college_code FROM ( SELECT t1.college_code, IF ( find_in_set( t1.parent_org_code, , ) AS i ...
- sql查看数据库环境及一些参数
sql查看数据库环境及一些参数 select parent_obj from sysobjects where name='FK_Student_banjiID' --根据外键名得到外表id sele ...
- 【Linux】ssh执行远程命令awk 参数报错问题
ssh ip sudo docker ps -a | grep none | awk '{print \$1}'| sed 's/%//g' $1前面加上转移符就好
- webpack 4.+版本需要注意的地方
在webpack打包的时候需要npm下载webpack-cli,而且打包JS的命令从以前的webpack .\src\main.js .\dist\boundle.js 要编程 webpack .\ ...
- Honk's pool(二分模板题)
题意:有n个水池,每个水池有a[i]单位水,有k次操作,每次操作将水量最多的水池减少一单位水,水量最少的水池增加一单位水,问最后水量最大的水池和水量最少的水池相差的水量. 思路:二分最后的最大水量和最 ...
- java中封装的使用方法(工具myeclipse)
封装可以实现属性私有化,将类的属性修饰符由public改为private,如此做者,其他类就无法访问该类中被private修饰的对象,一般我们会使用setter/getter()方法实现对这些对象的访 ...
- Vue.config.silent = true
Vue.config vue的全局配置文件 silent默认值是false Vue.config.silent = true 取消Vue所有的日志和警告
- 【BZOJ2138】stone
题目 好厉害的题啊 这道题不难看成一个二分图模型,但是给人一种求最大匹配的感觉,这实在不是很好求的样子,于是自闭了 但是不妨这样来考虑,对于一个需求\(k_i\),我们求一个最大的\(x\leq k_ ...
- java虚拟机规范(se8)——class文件格式(六)
4.7.4 StackMapTable 属性 StackMapTable 属性是一个变长属性,位于 Code(§4.7.3)属性的属性表中.这个属性会在虚拟机类加载的类型阶段(§4.10.1)被使用. ...