广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Spring加载属性文件方式(自动加载优先级问题)
  • 459
分享到

Spring加载属性文件方式(自动加载优先级问题)

2024-04-02 19:04:59 459人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

目录spring加载属性文件方式1、用xml文件配置方式2、用注解对Spring加载顺序的理解WEB.xml初始化spring加载流程Spring加载属性文件 方式1、用xml文件配

Spring加载属性文件

方式1、用xml文件配置

正常情况下,spring整合mybatis的配置文件的dataSource部分如下

 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.Mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/SSM"></property>
        <property name="username" value="root"></property>
        <property name="passWord" value="123456"></property>
    </bean>

可以将数据库的链接信息写到属性文件中,如下。

jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123456

在spring配置文件中,就可以用${}的形式获取属性信息,但需要加入 <context:property-placeholder />标签设置属性文件的路径。即

 <context:property-placeholder location="classpath:db.properties"/>    
 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
     <property name="driverClassName" value="${jdbc.driver}"></property>      
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"/>
 </bean>

但是由此会引发另一个问题,自动加载的优先级特别高(就是先实例化)

若org.mybatis.spring.SqlSessionFactoryBean的id为sqlSessionFactory,当自动注入时,org.mybatis.spring.mapper.MapperScannerConfigurer类下的SqlSessionFactory属性会自动注入,然后org.mybatis.spring.SqlSessionFactoryBean也会实例化,而org.mybatis.spring.SqlSessionFactoryBean中含有dateSourse,所以org.springframework.jdbc.datasource.DriverManagerDataSource也会实例化,但是这时属性文件还没有加载,造成程序出错Error setting property values,总而言之就是在属性文件加载之前,类实例化了,结果得不到属性文件中的值。

解决办法

第1步,更改org.mybatis.spring.SqlSessionFactoryBean的id名称,例如factory

第2步,将org.mybatis.spring.mapper.MapperScannerConfigurer中加入<property name="sqlSessionFactoryBeanName" value="factory"></property>,如果用<property name="sqlSessionFactory/>标签同样出现以上的问题。

因为自动注入只影响ref的,而sqlSessionFactoryBeanName的值的类型时string,用value赋值,所以不受影响

以下是完整的spring整合mybatis的配置文件

<?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:aop="http://www.springframework.org/schema/aop"
    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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd" 
        default-autowire="byName">
        
      <context:property-placeholder location="classpath:db.properties"/>        
        <!-- 数据源封装类,数据源:获取数据库连接,spring-jdbc.jar中 -->
      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <property name="driverClassName" value="${jdbc.driver}"></property>      
         <property name="url" value="${jdbc.url}"></property>
         <property name="username" value="${jdbc.username}"></property>
         <property name="password" value="${jdbc.password}"/>
      </bean>
        <!-- 创建SqlSessionFactory对象 -->
      <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
          <!-- 数据库连接信息来源于dataSource -->
          <!-- <property name="dataSource" ref="dataSource"></property> -->
          <!-- 相当于mybatis中别名默认包 -->
          <property name="typeAliasesPackage" value="com.lee.pojo"></property>
      </bean>
      <!-- 扫描器相当于mybatis设置接口绑定时xml的mappers下的package标签 -->
      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
          <!-- 扫描哪个包 -->
          <property name="basePackage" value="com.lee.mapper"></property>
          <!-- 和factory产生关系 -->       
      <property name="sqlSessionFactoryBeanName" value="factory"></property>
      </bean>     
</beans>

方式2、用注解

使用注解方法时,需要添加标签,这里的包名指的是含有注解的类所在包

<context:component-scan base-package="com.lee.service.impl"></context:component-scan>

测试的properties

my.value=hello

测试类

public class Demo{
    @Value("${my.value}")
    private String test;    
}

这样就可以实例化Demo时给test注入值 

对Spring加载顺序的理解

web.xml初始化

  • Web项目启动的时候,容器(如:Tomcat)读取webapp/WEB-INF/web.xml文件,读取和;
  • 创建ServletContex,Web项目所有部分都可以使用该上下文ServletContex;
  • 容器将解析为key-value对,并交给ServletContext;
  • 容器根据中的类创建监听实例,即启动监听;
  • listener监听类中会contextInitialized(ServletContextEvent servletContextEvent)初始化方法,可通过ServletContextEvent.getServletContext().getInitParameter(“field”)获得value的值;
  • 解析,并启动拦截器 拦截器开始起作用,当有请求进入时,执行Filter的doFilter方法;
  • 最后加载和初始化配置在load on startup的servlets;
  • 加载Spring,如果filter需要用到bean,但加载顺序是: 先加载filter 后加载spring,则filter中初始化操作中的bean为null.如果过滤器中要使用到 bean,可以将spring 的加载 改成Listener的方式:org.springframework.web.context.ContextLoaderListener

先创建上下文对象servletcontext,再加载监听器,然后去加载拦截器,最后加载servlet

路径问题:Spring mvc静态资源拦截(No mapping found for HTTP request with URI in DispatcherServlet with name ’ ')问题

/ 是加载视图配置的目录下的文件,前提是webapp下没有默认文件;如果有文件就访问默认文件

/* 我的测试是都报404

spring加载流程

启动先加载web.xml(包含:加载applicationContext.xml、listener:contextloadlistener、:DispatcherServlet),通过applicationContext.xml加载接口及java实现类、加载config.properties文件、加载数据库驱动等、加载mybatis.config文件(SqlSessionFactoryBean:加载xml文件)、加载数据库的接口和mapper.xml、加载springMVC视图等。

要保证install后mapper.java、mapper.xml要在同一文件下

如果用EL表达式(ModelAndView)时表达式出现问题解决如下:(搜索:SpringMVC中jsP页面不显示EL表达式的原因)

提高web.xml最上面dtd的版本

在jsp页面添加<%@ page isELIgnored=“false” %> ,添加head里就行

名称空间

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: Spring加载属性文件方式(自动加载优先级问题)

本文链接: https://www.lsjlt.com/news/139078.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作