spring xml实现bean对象(仅供自己参考)

对于spring 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 -->
    <!-- 这个的意思是有一个属性或者成员变量的 message ,
然后在使用setter注入的方法完成hello的注入-->
    <bean id="myBean" class="com.example.MyBean">
        <property name="message" value="Hello, Spring!" />
    </bean>

     <!-- 通过setter方法注入其他的bean,意思是,这个类中包含其他类的对象 -->
    <bean id="myDependentBean" class="com.example.MyDependentBean">
        <property name = "myBean" ref="myBean" />
    </bean>

    <!-- 定义一个依赖注入的 bean,使用的是构造函数的注入方式 -->
    <!-- 定一些基本类型也可以使用构造函数的初始化 -->
    <bean id="myDependentBean" class="com.example.MyDependentBean">
        <constructor-arg ref="myBean" />
    </bean>

</beans>

bean的作用域:单例和原型

单例(singleton):通过设置scope属性,来指定bean的作用域为单例模式,也就是说,所有的对象注入都是用的bean容器中那唯一一个对象。其他的对象都是这个对象的赋值,也就是一个对象=一bean容器中存放的对象

原型(prototype):通过设置scope属性,来指定bean的作用域为圆形模式,在自动注入的时候,bean容器会创建一个新的对象,把这个新的对象赋值给所需要的对象。这种作用域,也就是可以说bean容器中没有相对应的对象,只是在需要依赖注入的时候就地创建(仅我的猜想)。

bean的生命周期:

Spring Bean生命周期大致可以分为以下 5 个阶段:1.Bean 的实例化、2.Bean 属性赋值、3.Bean 的初始化、4.Bean 的使用、5.Bean 的销毁

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security 主要负责处理身份验证和授权,而跨域请求则通常由浏览器处理。因此,Spring Security 不提供专门的 XML 配置选项来设置跨域请求。 不过,你可以在 Spring Security 中使用 `CorsFilter` 实现跨域请求。`CorsFilter` 是一个 Spring 提供的过滤器,它可以在请求头中添加 CORS 相关的信息,以允许跨域请求。 以下是一个使用 `CorsFilter` 的例子: 1. 在 web.xml 中添加如下配置: ```xml <filter> <filter-name>CorsFilter</filter-name> <filter-class>org.springframework.web.filter.CorsFilter</filter-class> </filter> <filter-mapping> <filter-name>CorsFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> ``` 2. 在 Spring Security 的 XML 配置文件中添加如下配置: ```xml <http> ... <custom-filter ref="corsFilter" after="SECURITY_CONTEXT_FILTER"/> </http> <beans:bean id="corsFilter" class="org.springframework.web.filter.CorsFilter"> <beans:constructor-arg> <beans:bean class="org.springframework.web.cors.CorsConfiguration"> <beans:property name="allowedOrigins" value="http://example.com"/> <beans:property name="allowedMethods" value="GET, POST, PUT, DELETE, OPTIONS"/> <beans:property name="allowedHeaders" value="Authorization, X-Requested-With, Content-Type, Accept"/> <beans:property name="allowCredentials" value="true"/> <beans:property name="maxAge" value="3600"/> </beans:bean> </beans:constructor-arg> </beans:bean> ``` 在上面的配置中,我们将 `CorsFilter` 添加到了 Spring Security 过滤器链中,并设置了一些 CORS 相关的配置信息,例如允许跨域请求的源地址、允许的 HTTP 方法、允许的请求头等。 需要注意的是,以上代码仅供参考,具体的跨域请求配置应该根据你的实际需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值