Spring 在xml文件中配置Bean
bean常用的配置方式有2种:
- xml文件
- 注解
使用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 name="user" class="com.chy.bean.User" />
<bean name="goods" class="com.chy.bean.Goods" />
</beans>
<beans>是根元素。一个<bean>配置一个bean:
<bean name="" class="" scope="" />
name指定bean的名称,也可以使用id指定。如果要指定多个名称,用逗号或分号隔开即可,比如name="grade,score"。
scope指定Bean的实例的作用域,可选,缺省时默认为singleton(单例)。
id和name的区别:
- id必须唯一,name可以重复
- id不能含有特殊字符,name可以
- id只能有一个值,name可以有多个值,逗号或分号隔开即可
Bean的作用域
| 作用域 | 说明 |
| singleton(单例) |
默认值。 该Bean(类)在Spring容器中只有一个实例,无论引用/获取这个Bean多少次,都指向同一个对象。 singleton适用于无会话状态的Bean(如Dao组建、Service组件)。 在spring容器启动时就创建实例,且程序运行过程中,这个Bean只创建、使用一个实例。 由spring容器负责管理生命周期,实例的创建、销毁均由spring容器负责。 |
| prototype(多例) |
每次获取该Bean的实例时,都会创建一个新的实例。 在需要时(从容器中获取该bean的时)才创建该bean的一个实例。 由spring容器负责创建实例,创建好之后交给调用者,由调用者负责后续处理,spring容器不再管理这个实例。 |
| request |
web中使用。 在一次HTTP请求中,获取的是该Bean的同一个实例,该实例只在此次HTTP请求中有效。 新的HTTP请求,会创建新的实例。 |
| session |
web中使用。 在一次HTTP session中获取的是该Bean的同一个实例(一个session中只创建此bean的一个实例),创建实例只在本次HTTP session中有效。 新的session,会创建新的实例 |
| globalSession |
在一个全局的HTTP session中,获取到的是该Bean的同一个实例。 只在使用portlet上下文时有效。 |
| application |
为每个ServletContext对象创建一个实例。 只在web相关的ApplicationContext中有效。 |
| websocket |
为每个websocket对象创建一个实例。 只在web相关的ApplicationContext中有效。 |
Student student1=applicationContext.getBean("student",Student.class);
Student student2=applicationContext.getBean("student",Student.class);
如果Student的作用域是singleton,则student1、student2都指向内存中的同一个Student对象。
如果Student的作用域是prototype,则student1、student2指向不同的Student对象。
Spring加载xml配置文件的常用方式有2种
// 从类盘符加载,写相对路径
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config.xml"); // 从文件系统加载,写绝对路径,不利于移植,一般都是用上面那种。
// ApplicationContext applicationContext=new FileSystemXmlApplicationContext("E:\\spring\\src\\spring-config.xml");
Spring的模块化配置
spring的模块化配置,也叫spring的模块化开发。
- 把配置写在一个xml文件中,xml文件会很臃肿,可以拆分为多个xml文件。
- 有时候需要多人协作,你写一个模块,我写一个模块,你那儿有一个xml配置,我这儿有一个xml配置
使用时需要同时引入多个xml配置。
有2种方式:
- 在xml种使用<import />导入其它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 https://www.springframework.org/schema/context/spring-context.xsd"> <import resource="spring-config.xml" />
</beans>
- 在程序中使用多个xml配置构建spring容器
ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-config-controller.xml","spring-config-service.xml" );
参数个数可变。
使用注解配置Bean
使用xml进行配置,如果<bean>很多,xml文件会很臃肿。所以通常使用注解来配置bean。
使用spring的注解,需要引入spring-aop-RELEASE.jar。
spring常用的类注解:
- @Service 将业务层(Service层)的类标识为Spring容器中的Bean
- @Controller 将控制层的类标识为Spring容器中的Bean
- @Repository 将数据访问层(Dao层)的类标识为Spring容器中的Bean
- @Component 将一个类标识为Spring容器中的Bean。
前3个是专用的,@Component是通用的,能用专用的就尽量用专用的。
这4个注解都是类注解,只能对类使用,只能写在类上面。
使用示例:
@Component("dog")
public class Dog{
public void shout(){
System.out.println("汪汪汪");
}
}
小括号中写Bean的name。
@Component
public class Dog{
public void shout(){
System.out.println("汪汪汪");
}
}
如果缺省name,默认为类名的camel写法。
需要在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 https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.chy.bean" />
</beans>
会自动扫描指定的包,如果类上面有这4个注解中的一个,就把这个类放到Spring容器中,由Spring容器管理这个类的实例。
如果要扫描多个包,比如bean、service、dao,可以使用多个<context:component-scan />,也可以直接扫描父包com.chy。
使用spring的注解时,需要在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 https://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config />
</beans>
因为包扫描<context:component-scan base-package="" />是先启用注解,再扫描包,所以使用包扫描时不必再写 <context:annotation-config /> 。
使用注解配置scope
@Component
@Scope("prototype")
public class A {
//......
}
Spring 在xml文件中配置Bean的更多相关文章
- Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)
<bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil& ...
- JPA 不在 persistence.xml 文件中配置每个Entity实体类的2种解决办法
在Spring 集成 Hibernate 的JPA方式中,需要在persistence配置文件中定义每一个实体类,这样非常地不方便,远哥目前找到了2种方法. 这2种方式都可以实现不用persist ...
- 不在JPA 的 persistence.xml 文件中配置Entity class的解决办法
在Spring 集成 Hibernate 的JPA方式中,需要在persistence配置文件中定义每一个实体类,这样非常地不方便,2种方法可以解决此问题: 这2种方式都可以实现不用在persiste ...
- struts.xml文件中配置tiles.xml
Apache Tiles是一个JavaEE应用的页面布局框架.Tiles框架提供了一种模板机制,可以为某一类页面定义一个通用的模板,该模板定义了页面的整体布局.布局由可以复用的多个块组成,每个页面可以 ...
- web.xml文件中配置ShallowEtagHeaderFilter需注意的问题
问题现象及解决方法 今天在Spring MVC应用中加入ShallowEtagHeaderFilter时,发现返回的响应中没有etag头,上网查了很多相关资料,也试了很多方法依然不起作用.在查看web ...
- Spring基础——在Spring Config 文件中配置 Bean
一.基于 XML 的 Bean 的配置——通过全类名(反射) <bean <!-- id: bean 的名称在IOC容器内必须是唯一的若没有指定,则自动的将全限定类名作为 改 bean 的 ...
- xml文件中配置JDBC源遇到问题 : The reference to entity "characterEncoding" must end with the ';' delimiter
数据源配置时加上编码转换格式后出问题了: The reference to entity"characterEncoding" must end with the ';' deli ...
- Maven 在 pom.xml 文件中配置 repositories 仓库
如果你希望在你的项目中使用独立的 repositories . 例如,你希望配置使用自己的 https://maven.ossez.com/repository/internal 作为仓库. 例如,修 ...
- ssm框架中applicationContext.xml文件中配置别名
在applicationContext.xml中配置如下: 通过以下property标签中给定name属性value属性及对应的值,来将domain包下所有实体类设置别名. 在xxxDao.xml中 ...
随机推荐
- pom.xml activatedProperties --spring.profiles.active=uat 对应
<profiles> <profile> <id>dev</id> <properties> <!-- 环境标识,需要与配置文件的名称 ...
- Oracle的“ORA-00937: 不是单组分组函数” 如何解决?
之前在编写oracle的sql语句时遇到这个问题,这里做个记录 问题描述:ORA-00937: 不是单组分组函数 问题原因:select语句中又在查询某一列的值,其中还有聚合函数 原先本人编写SQL是 ...
- MQTT研究之EMQ:【CoAP协议的ECC证书研究】
今天研究的内容,是CoAP这个协议在EMQ消息队列的支持,CoAP是一个受限资源的协议,基于UDP实现的多用于物联网环境的通信协议.相关介绍不多说,可以看RFC. CoAP协议下,基于DTLS通信,同 ...
- Nginx warn:an upstream response is buffered to a temporary file
我通过nginx下载文件,error.log中出现如下警告日志:warn:an upstream response is buffered to a temporary file . 虽然网上各种例 ...
- 卷积神经网络概念及使用 PyTorch 简单实现
卷积神经网络 卷积神经网络(CNN)是深度学习的代表算法之一 .具有表征学习能力,能够按其阶层结构对输入信息进行平移不变分类,因此也被称为“平移不变人工神经网络”.随着深度学习理论的提出和数值计算设备 ...
- docker 启动mysql 闪退 无法启动问题
docker 安装mysql [获取容器] docker pull mysql:5.6 [启动容器] docker run -p 3306:3306 --name mymysql -v $PWD/co ...
- 【翻译】Flink Table Api & SQL — Hive —— Hive 函数
本文翻译自官网:Hive Functions https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/table/hive/h ...
- pytorch使用DataParallel并行化负载不均衡问题
使用DataParallel进行并行化时的结构如下: 在上图第一行第四个步骤中,GPU-1 其实汇集了所有 GPU 的运算结果.这个对于多分类问题还好,但如果是自然语言处理模型就会出现问题,导致 GP ...
- es6 学习小计
es6允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这称之为解构:
- javascript 从对象数组中 按字段/属性取最大值或最小值
var array=[ { "index_id": 119, "area_id": "18335623", "name" ...