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中 ...
随机推荐
- 【Python】 vscode使用code-runner 调试代码
插件名称: code-runner 插件设置: "code-runner.executorMap": { "python" : "set PYTHON ...
- python字符串拼接N种姿势
字符串大家都不陌生,应用比较广泛,强大,总是会给你一些惊喜的数据类型.我们本篇文章主要介绍的就是关于字符串的多种方法的拼接. 第一种:直接通过+号拼接 输出结果: 2.通过 str.join()方法拼 ...
- phpspreadsheet 中文文档(四) 创建电子表格+档案格式
2019年10月11日14:01:48 该Spreadsheet班 该Spreadsheet班是PhpSpreadsheet的核心.它包含对所包含工作表,文档安全性设置和文档元数据的引用. 为了简化P ...
- C++中的结构体所占内存空间总结
因为结构体有时候需要字节对齐.一般而言,struct 的 sizeof 是所有成员字节对齐后长度相加,而 union 的 sizeof 是取最大的成员长度. 在默认情况下,编译器为每一个变量或数据单元 ...
- 003 SpringBoot整合SpringMVC、Mybatis 案例
1.不使用骨架创建一个maven工程 2.修改POM.xml文件 <?xml version="1.0" encoding="UTF-8"?> &l ...
- 防范sql注入值得注意地方
sql注入是大家基本都清楚,一般来说用参数化就能解决注入的问题,也是最好的解决方式. 有次技术群里问到一个问题,如下图 很显然tableName是外部传递过来的,暂时不考虑具体的业务环境,但如果以se ...
- day18——re正则表达式
day18 re模块--正则表达式 匹配方法 findall():从字符串中全部查找内容,返回一个列表 s = "meet_宝元_meet" print(re.findall(&q ...
- LeetCode 5108. Encode Number - Java - 2进制
题目链接:https://leetcode-cn.com/problems/encode-number/ Given a non-negative integer num, Return its en ...
- Linux基础(10)AIO项目设计与POSIX文件操作和目录管理
实现fast-cp :拷贝文件到目标对象 Linux的七种文件类型 :https://blog.csdn.net/linkvivi/article/details/79834143 ls -al :h ...
- Akka-CQRS(10)- gRPC on SSL/TLS 安全连接
使用gRPC作为云平台和移动前端的连接方式,网络安全应该是必须考虑的一个重点.gRPC是支持ssl/tls安全通讯机制的.用了一个周末来研究具体使用方法,实际上是一个周末的挖坑填坑过程.把这次经历记录 ...