基于xml配置对象容器——xml 标签说明

alias标签

作用:为已配置的bean设置别名

--applicationContext.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="org.cjw.pojo.User" /> <!--
标签alias:为已配置的bean设置别名
name属性:必要属性,代表为哪一个bena配置别名
此属性的值为其他bean标签的id或name属性值
alias属性:必要属性,代表新命名的别名是什么
-->
<alias name="user" alias="user1" />
</beans>

--测试代码

package org.cjw.pojo.test;

import org.cjw.pojo.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class UserTest { @Test
public void testAlias() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 通过id获取User对象
User user = context.getBean("user", User.class);
System.out.println(user);
System.out.println("-----------------");
// 通过别名获取User对象
User user2 = context.getBean("user1", User.class);
System.out.println(user2);
}
}

--测试结果

bean标签的配置

5.2.1. bean标签作用

用于声明一个类,在启动Spring框架的时候根据配置信息创建对象到Spring容器里面。

5.2.2. 属性说明

<?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>标签:用于声明一个类,在启动Spring框架的时候配置信息创建对象到spring容器里面去
name属性:设置对象名(唯一标识符),可以有多个名称,每一个名称用逗号隔开,如name1,name2
id属性:设置对象名(唯一标识符),功能和name一样,但是id只能有一个
class属性:用于指定对象对应的类名,用于反射创建对象
scope属性:用于设置对象的作用范围,可选参数如下:
*singleton:单例(默认)
对象出生:当程序加载配置文件创建容器时,创建
对象活着:只要容器还在,一直活着
对象死亡:应用停止,容器销毁,对象死亡
*propertype:多例(原型对象)
对象出生:当程序加载配置文件创建容器时,创建
对象活着:只要对象被使用,一直活着
对象死亡:对象长时间不用,会被java立即回收机制回收
*request:web项目中,Spring将创建的对象放在request作用域中
*session:web项目中,Spring将创建的对象放在session作用域中
-->
<bean id="customerServiceImpl" class="org.cjw.service.impl.CustomerServiceImpl" scope="singleton" />
</beans>

5.2.3. Bean作用范围

作用范围也可以说生命周期(bean能存活多久)

<bean id="" class="" scope="作用域"/>

在开发中主要使用 scope="singleton"、 scope="prototype"

对于MVC中的Action/Controller使用prototype类型,其他使用singleton

<?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="" class="" scope="作用域"/>
scope : 配置当前bean的范围大小 singleton: 单例 ,在Spring IoC容器中仅存在一个Bean实例 (默认的scope)
prototype: 多例 ,每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时 ,相当于执行new XxxBean()
--> <bean id="customerServiceImpl" class="org.cjw.service.impl.CustomerServiceImpl" scope="prototype" />
</beans>

在Web开发的三层架构中

  • Web:一般都是多例

  • Service:单例

  • DAO:单例

如果使用struct,那么会在表示层使用成员变量来接受前端发送过来的参数,此时如果表示层使用的是单例,那么会造成数据的错乱,所以表示层需要使用多例。

而现在springMVC已经不再使用成员变量来接受前端参数了,而是直接对应方法的参数,所以表示层可以使用单例,因为方法的参数是在程序运行过程中才会被赋值,所以不存在数据错乱的问题,因此可以使用单例。

单例和多例的使用判断准则:是否存在共享数据情况,如果有,使用多例,没有则单例。如果使用了单例还存在共享数据的情况,那么就需要使用锁来保证数据的正确性。

实例化Bean的四种方式

Spring创建对象的四种方式

5.3.1. 构造器实例化(无参数构造器),最标准,使用最多。

package org.cjw.pojo;

public class SomeBean1 {
public SomeBean1() {
System.out.println("SomeBean.SomeBean1()");
}
}

--配置文件

<!-- ①构造器实例化(无参构造器),最标准、使用最多 -->
<bean id="someBean1" class="org.cjw.pojo.SomeBean1"/>

5.3.2. 通过静态方法工厂创建(了解)

--bean、静态工厂类

SomeBean2、SomeBean2Factory
public class SomeBean2 {
public SomeBean2() {
System.out.println("SomeBean.SomeBean2()");
}
}
public class SomeBean2Factory { public static SomeBean2 getSomeBean2() {
System.out.println("执行静态工厂方法");
return new SomeBean2();
}
}

--静态工厂配置

<!-- ②.静态工厂方法实例化:解决系统遗留问题 -->
<bean id="someBean2" class="org.cjw.factory.SomeBean2Factory" factory-method="getSomeBean2" />

5.3.3. 通过实例工厂创建(了解)

--实体工厂

public class SomeBean3 {
public SomeBean3() {
System.out.println("SomeBean.SomeBean3()");
}
}
public class SomeBean3Facotry {
//实例工厂方法
public SomeBean3 getSomeBean3() {
System.out.println("执行实例工厂方法");
return new SomeBean3();
}
}

--配置方式

<!-- 1.配置工厂bean -->
<bean id="someBean3Factory" class="org.cjw.factory.SomeBean3Facotry"></bean>
<!-- 2.配置bena
factory-bean : 创建bean的工厂对象对应的 id
factory-method : 工厂bean中返回 bean对象的方法
-->
<bean id="someBean3" factory-bean="someBean3Factory" factory-method="getSomeBean3"/>

5.3.4. 实现FactoryBean接口实例化:实例工厂变种(了解)

实现FactoryBean接口,MyBatis和Spring集成就是使用的这种方式。

此种方式,如果没有使用Bean对应的对象,Spring就不会自动创建,只有在使用的时候Spring才会创建对应的对象。

public class SomeBean4 {
public SomeBean4() {
System.out.println("SomeBean4.SomeBean4()");
}
}
public class SomeBean4ObjectFactory implements FactoryBean<SomeBean4> { @Override
public SomeBean4 getObject() throws Exception {
SomeBean4 bean4 = new SomeBean4();
return bean4;
} @Override
public Class<?> getObjectType() {
return null;
} @Override
public boolean isSingleton() {
return false;
}
}

--配置方式

<!-- ④.实现FactoryBean接口实例化:实例工厂变种:集成其他框架使用:LocalSessionFactoryBean -->
<bean id="someBean4" class="org.cjw.factory.SomeBean4ObjectFactory" />

初始化和销毁方法

比如DataSource,SessionFactory最终都需要关闭资源:在Bean销毁之前,都要调用close方法.

<bean id="someBean" class="......"
init-method="该类中初始化方法名" destroy-method="该类中销毁方法名">
</bean>

init-method:bean生命周期初始化方法,对象创建后就进行调用

destroy-method:容器被销毁的时候,如果bean被容器管理,会调用该方法。

default-init-method:指定默认的初始化方法

分析原理:

如果bean的scope="prototype",那么容器只负责创建和初始化,它并不会被spring容器管理。交给用户自己调用。

<?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"
default-init-method="init">
<!--
配置全局初始化方法,如果有100个bean中都有init方法,那么只要Spring容器一启动,bean对象一创建
默认对象中只要有 init方法,都全部会执行:一般不建议使用
--> <!--
init-method : 配置初始化方法名
destroy-method : 配置销毁方法名
-->
<bean id="someBean" class="org.cjw.pojo.SomeBean1" init-method="init" destroy-method="destory" />
</beans>

获得properties文件的值

Spring配置文件支持通过xxx.properties文件的Key获得对应的值。实现该功能是通过${Key}来获得Properties文件指定Key的Value值。

使用Spring读取配置文件必须导入新的命名空间(context)。

导入命名空间方法:将命名空间和约束重新拷贝一份,将对应的全部替换成 context,然后关联context本地schema约束。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

使用Spring创建阿里巴巴 Druid连接池(读取配置文件)

7.1.1. 拷贝Mysql驱动包和druid连接池jar包到项目中

7.1.2. 创建 db.properites

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/users
jdbc.username=root
jdbc.password=root
jdbc.maxActive=10

7.1.3. applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 读取classpath下的db.properties配置文件 -->
<context:property-placeholder location="classpath:db.properties" /> <!-- 配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${jdbc.maxActive}" />
</bean>
</beans>

7.1.4. 测试代码

@Test
public void testAlias() {
try {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = context.getBean("dataSource", DataSource.class);
Connection connection = dataSource.getConnection();
System.out.println(connection);
} catch (SQLException e) {
e.printStackTrace();
}
}

7.1.5. 效果

综合案例-模拟注册功能

此功能重点在于将每一层对象的创建交给Spring管理,对象之间的依赖关系交给Spring来维护。

Dao层接口以及实现代码

public interface UserDao {

    public void insert(User user);
}
public class UserDaoImpl implements UserDao {
@Override
public void insert(User user) {
System.out.println("注册功能Dao层方法执行了");
}
}

Service层接口以及实现代码

public interface UserService {

    void insert(User user);

}
public class UserServiceImpl implements UserService { private UserDao userDao; public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} @Override
public void insert(User user) {
System.out.println("注册功能Service层方法执行了");
userDao.insert(user);
}
}

Web表现层实现代码

package org.cjw.controller;

import org.cjw.pojo.User;
import org.cjw.service.UserService; public class UserController { private UserService userService; public void setUserService(UserService userService) {
this.userService = userService;
} public void insert() {
System.out.println("注册功能Controller层方法执行了");
User user = new User();
userService.insert(user);
} }

applicationContext.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="userController" class="org.cjw.controller.UserController">
<property name="userService" ref="userService" />
</bean> <bean id="userService" class="org.cjw.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao" />
</bean> <bean id="userDao" class="org.cjw.dao.impl.UserDaoImpl" /> </beans>

测试代码

@Test
public void testAlias() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserController userController = context.getBean("userController", UserController.class);
userController.insert();
}

测试结果

小结

1. 基于xml和ClassPathXmlApplicationContext配置容器

2. Spring读取 .Properteis配置文件

3. 综合案例-模拟注册功能-使用Spring管理对象

【Spring Framework】Spring入门教程(二)基于xml配置对象容器的更多相关文章

  1. MyBatis入门程序(基于XML配置)

    创建一个简单的MyBatis入门程序,实现对学生信息的增删改查功能(基于XML配置) 一.新建一个Java工程,导入MyBatis核心jar包.日志相关的jar包以及连接Oracle数据库所需驱动包, ...

  2. 使用Spring框架入门一:基于XML配置的IOC/DI的使用

    一.Spring框架 1.方法一:逐项导入基础依赖包: spring-core.spring-beans.spring-context.spring-expression 2.方法二:最简洁的导入,直 ...

  3. 使用Spring框架入门三:基于XML配置的AOP的使用

    一.引入Jar包 <!--测试1使用--> <dependency> <groupId>org.springframework</groupId> &l ...

  4. JAVA Spring 事物 ( 已转账为例 ) 基于 XML 配置,事务类型说明

    < 1 > 配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...

  5. Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

    本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...

  6. Spring Boot 框架下使用MyBatis访问数据库之基于XML配置的方式

    MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 XML ...

  7. 一步一步深入spring(6)--使用基于XML配置的spring实现的AOP

    上节我们提到了使用基于注解实现的AOP,这节我们将用基于xml配置的方式来实现的AOP. 1.首先建立一个类,作为切面类,这个类主要用来实现注解中各种通知要实现的方法. package com.yan ...

  8. Spring声明式事务管理(基于XML方式实现)

    --------------------siwuxie095                             Spring 声明式事务管理(基于 XML 方式实现)         以转账为例 ...

  9. Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用

    Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...

随机推荐

  1. 问题 D: 某种序列

    题目描述 数列A满足An = An-1 + An-2 + An-3, n >= 3  编写程序,给定A0, A1 和 A2, 计算A99 输入 输入包含多行数据  每行数据包含3个整数A0, A ...

  2. R数据分析:如何给结构方程画路径图,tidySEM包详解

    之前一直是用semPlot这个包给来进行结构方程模型的路径绘制,自从用了tidySEM这个包后就发现之前那个包不香了,今天就给大家分享一下tidySEM. 这个包的很大特点就是所有的画图原始都是存在数 ...

  3. SpringCloud微服务实战——搭建企业级开发框架(十九):Gateway使用knife4j聚合微服务文档

      本章介绍Spring Cloud Gateway网关如何集成knife4j,通过网关聚合所有的Swagger微服务文档 1.gitegg-gateway中引入knife4j依赖,如果没有后端代码编 ...

  4. [luogu5564]Say Goodbye

    两树相同的定义与题中相同,并定义两树完全相同当且仅当在不允许基环旋转的条件下相同 枚举基环的长度$l$,根据置换群的理论,答案即$\frac{1}{l}\sum_{i=1}^{l}f(i)$(其中$f ...

  5. [bzoj1145]图腾

    如果将关系用一个数字来表示(相等表示不确定),那么题目相当于要计算$1324-1243-1432$=$(1323-1423)-(1233-1234)-(1322-1423)$=$1323+1234-( ...

  6. Roslyn+T4+EnvDTE项目完全自动化 (一)

    前言 以前做一个金融软件项目,软件要英文.繁体版本,开始甲方弄了好几个月,手动一条一条替换,发现很容易出错,因为有金融专业术语,字符串在不同语义要特殊处理,第三方工具没法使用.最后我用Roslyn写了 ...

  7. Codeforces 1137F - Matches Are Not a Child's Play(LCT)

    Codeforces 题面传送门 & 洛谷题面传送门 考虑将一个点 \(x\) 的编号变为当前所有点编号最大值 \(+1\) 会对每个点的删除时间产生怎么样的影响.由于编号最大的点肯定是最后一 ...

  8. 洛谷 P4484 - [BJWC2018]最长上升子序列(状压 dp+打表)

    洛谷题面传送门 首先看到 LIS 我们可以想到它的 \(\infty\) 种求法(bushi),但是对于此题而言,既然题目出这样一个数据范围,硬要暴搜过去也不太现实,因此我们需想到用某种奇奇怪怪的方式 ...

  9. Codeforces 1158F - Density of subarrays(dp,神仙题)

    Codeforces 题目传送门 & 洛谷题目传送门 人生中第一道 *3500(显然不是自己独立 AC 的),不过还是祭一下罢 神仙 D1F 首先考虑对于给定的序列 \(a_1,a_2,\do ...

  10. 曼哈顿距离最小生成树 codechef Dragonstone

    曼哈顿距离最小生成树 codechef Dragonstone 首先,对于每一个点来说有用的边只有它向它通过 x=0,y=0,y=x,y=-x 切出来的八个平面的最近点. 证明 我不会 反正当结论记住 ...