beans头

发布时间 2023-10-06 09:27:04作者: 哲_心

MVC

全部导入的xml头

<!--导入p,c命名空间 context注解 -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://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/> <!--必须使用这个标签,才能使用注解-->
  1. 作用域 scope:
 1.singleton:(默认)将单个 Bean 定义的范围限定为每个 Spring IoC 的单个对象实例 容器。
 2.prototype:将单个 Bean 定义的作用域限定为任意数量的对象实例。
 3.request:将单个 Bean 定义的范围限定为单个 HTTP 请求的生命周期。那是 每个 HTTP 请求都有自己的 Bean 实例,该实例是在单个 Bean 的背面创建的 Bean 定义。仅在 Web 感知 Spring 的上下文中有效。ApplicationContext
 4.session:将单个 Bean 定义的范围限定为 HTTP 的生命周期。仅在以下情况下有效 网络感知的春天的上下文。SessionApplicationContext
 5.application:将单个 Bean 定义的范围限定为 .仅在以下情况下有效 网络感知的春天的上下文。ServletContextApplicationContext
 6.websocket:将单个 Bean 定义的范围限定为 .仅在以下情况下有效 网络感知的春天的上下文。WebSocketApplicationContext
  1. p,c命名空间:

      xmlns:p="http://www.springframework.org/schema/p"
      xmlns:c="http://www.springframework.org/schema/c"
    
    1. p:

      原文:

      <bean id="callOut" class="zhe.xin.service.CallOut">
        <property name="nameUser" ref="lizijie"/>
      </bean>

      p:

      <bean id="callOut2" class="zhe.xin.service.CallOut" p:nameUser="lizijie"></bean>
    2. c:

      有参构造: c:构造函数参数名

         <bean id="callOut2" class="zhe.xin.service.CallOut" c:构造函数参数名="lizijie"></bean>
  2. 基于注释的容器配置

与往常一样,您可以将后处理器注册为单独的 Bean 定义,但它们 也可以通过在基于 XML 的 Spring 中包含以下标记来隐式注册 配置(注意包含命名空间):context

<?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
https://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>