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

网友投稿 834 2022-10-31

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

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

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

Spring加载属性文件

方式1、用xml文件配置

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

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

jdbc.url=jdbc:mysql://localhost:3306/ssm

jdbc.driver=com.mysql.jdbc.Driver

jdbc.username=root

jdbc.password=123456

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

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

若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中加入,如果用

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd"

default-autowire="byName">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd"

default-autowire="byName">

方式2、用注解

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

测试的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里就行

名称空间

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:自定义spring boot starter三部曲之一:准备工作
下一篇:GUI程序,记录已执行终端命令的详细历史记录,以便于访问
相关文章

 发表评论

暂时没有评论,来抢沙发吧~