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的更多相关文章

  1. Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)

    <bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil& ...

  2. JPA 不在 persistence.xml 文件中配置每个Entity实体类的2种解决办法

    在Spring 集成 Hibernate 的JPA方式中,需要在persistence配置文件中定义每一个实体类,这样非常地不方便,远哥目前找到了2种方法.   这2种方式都可以实现不用persist ...

  3. 不在JPA 的 persistence.xml 文件中配置Entity class的解决办法

    在Spring 集成 Hibernate 的JPA方式中,需要在persistence配置文件中定义每一个实体类,这样非常地不方便,2种方法可以解决此问题: 这2种方式都可以实现不用在persiste ...

  4. struts.xml文件中配置tiles.xml

    Apache Tiles是一个JavaEE应用的页面布局框架.Tiles框架提供了一种模板机制,可以为某一类页面定义一个通用的模板,该模板定义了页面的整体布局.布局由可以复用的多个块组成,每个页面可以 ...

  5. web.xml文件中配置ShallowEtagHeaderFilter需注意的问题

    问题现象及解决方法 今天在Spring MVC应用中加入ShallowEtagHeaderFilter时,发现返回的响应中没有etag头,上网查了很多相关资料,也试了很多方法依然不起作用.在查看web ...

  6. Spring基础——在Spring Config 文件中配置 Bean

    一.基于 XML 的 Bean 的配置——通过全类名(反射) <bean <!-- id: bean 的名称在IOC容器内必须是唯一的若没有指定,则自动的将全限定类名作为 改 bean 的 ...

  7. xml文件中配置JDBC源遇到问题 : The reference to entity "characterEncoding" must end with the ';' delimiter

    数据源配置时加上编码转换格式后出问题了: The reference to entity"characterEncoding" must end with the ';' deli ...

  8. Maven 在 pom.xml 文件中配置 repositories 仓库

    如果你希望在你的项目中使用独立的 repositories . 例如,你希望配置使用自己的 https://maven.ossez.com/repository/internal 作为仓库. 例如,修 ...

  9. ssm框架中applicationContext.xml文件中配置别名

    在applicationContext.xml中配置如下: 通过以下property标签中给定name属性value属性及对应的值,来将domain包下所有实体类设置别名. 在xxxDao.xml中 ...

随机推荐

  1. node.js GET与POST请求

    node.js GET与POST请求 转 http://www.voidcn.com/article/p-ncglaiqx-bdx.html 标签 get post node.js 栏目 Node.j ...

  2. django在centos生产环境的部署

    # 安装数据库和web服务器nginx # yum install –y nginx mariadb-server # 安装虚拟环境 pip install virtualenv pip instal ...

  3. 【Base】死锁产生的四个必要条件

    死锁产生的四个必要条件 互斥条件:资源是独占的且排他使用,进程互斥使用资源,即任意时刻一个资源只能给一个进程使用,其他进程若申请一个资源,而该资源被另一进程占有时,则申请者等待直到资源被占有者释放. ...

  4. Data Flow ORB-SLAM for Real-Time Performance on Embedded GPU Boards

    张宁 Data Flow ORB-SLAM for Real-Time Performance on Embedded GPU Boards 数据流ORB-SLAM可在嵌入式GPU板上实现实时性能链接 ...

  5. windows下ffmpeg批量转码

    以mp4转mp3为例 for %%i in (*.mp4) do ffmpeg -i "%%i" "%%i.mp3" 将当前文件夹下的mp4文件全部转码为mp3

  6. Django框架 + Djiango安装 + First Djiango + 常用命令

    一.Django框架 MVC框架和MTV框架 参考:https://www.cnblogs.com/taosiyu/p/11260016.html MVC,全名是Model View Controll ...

  7. Activiti Service介绍

    原文地址:https://www.cnblogs.com/lyh421/p/6419518.html 第一章 认识Activiti 内容概览:讲解activiti的特点.接口概览.架构等基本信息. 1 ...

  8. npm与yarn命令

    npm 1. 查看npm版本 node -v npm -v 2. 更新npm至最新版 npm install npm@latest -g 3. npm install:安装依赖 # 在本地node_m ...

  9. Data-Structure-Notes

    Data Structure Notes Chapter-1 Sorting Algorithm Selection Sorting: /* * Selection Sort */ template& ...

  10. Python 入门(2):数据类型

    一 Number(数字) 1.1 数字类型的创建 a = 10 b = a b = 5 print(a) 10 print(b) 5 1.2 Number 类型转换 a = 5.2 b = 5 c = ...