springimport配置文件|SpringBoot的配置文件有哪几种格式

|

㈠ Spring import引入多个文件加载好,还是用web.xml加载多个application.xml文件号

分模块管理,比如说ibatis的放到一个配置文件,springbean的放到一个配置文件,jdbc的放到一个,就是从配置文件能很快地定位模块,便于管理。当然,如果项目小的话,一个配置文件就够了

㈡ 如何在Spring容器中加载自定义的配置文件

配置文件名为:project.properties,内容如下:# 是否开启逻辑删除del.filter.on=falsedomain=http://www.366go.cn/修改Spring配置文件之前代码:<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:dbinfo.properties</value> </list> </property></bean>修改后的配置文件<bean id="propertyConfigurer" class="com.hisun.core.util."> <property name="locations"> <list> <value>classpath:dbinfo.properties</value> <value>classpath:project.properties</value> </list> </property></bean>加入了:project.properties,其为自定义的配置文件将PropertyPlaceholderConfigurer类修改为自定义类,PropertyPlaceholderConfigurer类的具体作用可以查资料这块儿不做详细介绍定义类类的具体内容为下,import java.util.HashMap;import java.util.Map;import java.util.Properties; import org.springframework.beans.BeansException;import org.springframework.beans.factory.config.;import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; public class extends PropertyPlaceholderConfigurer { private static Map ctxPropertiesMap; @Override protected void processProperties( beanFactoryToProcess, Properties props) throws BeansException { super.processProperties(beanFactoryToProcess, props); ctxPropertiesMap = new HashMap(); for (Object key : props.keySet()) { String keyStr = key.toString(); String value = props.getProperty(keyStr); ctxPropertiesMap.put(keyStr, value); } } public static Object getContextProperty(String name) { return ctxPropertiesMap.get(name); }}定义获取配置文件中值的类SpringPropertiesUtil类的具体内容如下: import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component; /** * Spring-PropertiesUtil工具类 -获取属性值 * */@Componentpublic class SpringPropertiesUtil implements ApplicationContextAware { public static final String KEY = "propertyConfigurer"; private static ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringPropertiesUtil.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 获取配置文件中的内容 * * @param keyName * @return */ public static String parseStr(String keyName) { cp = () applicationContext .getBean(KEY); return cp.getContextProperty(keyName).toString(); } /** * 获取配置文件中的内容 * * @param keyName * @return */ public static int parseInt(String keyName) { cp = () applicationContext .getBean(KEY); return Integer.parseInt(cp.getContextProperty(keyName).toString()); } /** * 获取配置文件中的内容 * * @param keyName * @return */ public static double parseDouble(String keyName) { cp = () applicationContext .getBean(KEY); return Double.parseDouble(cp.getContextProperty(keyName).toString()); }}这样,在项目当中就能够方便快速的获取properties文件中配置的参数如SpringPropertiesUtil.parseStr(“content”)

㈢ 如何在Spring容器中加载自定义的配置文件

自定义配置文件配置文件名为:project.properties,内容如下:[html] view plain # 是否开启逻辑删除 project_del.filter.on=false project_domain=修改Spring配置文件之前代码:[html] view plain <beanidbeanid="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <propertynamepropertyname="locations"> <list> <value>classpath:dbinfo.properties</value> </list> </property> </bean> 修改后的配置文件[html] view plain <beanidbeanid="propertyConfigurer" class="com.hisun.core.util."> <propertynamepropertyname="locations"> <list> <value>classpath:dbinfo.properties</value> <value>classpath:project.properties</value> </list> </property> </bean> 加入了classpath:project.properties,其为自定义的配置文件将PropertyPlaceholderConfigurer类修改为自定义类,PropertyPlaceholderConfigurer类的具体作用可以查资料这块儿不做详细介绍注意下:这个configurer类获取的是所有properties的属性map,如果希望处理某个properties文件,需要在properties中做一个命名区别,然后在加载的时候,根据key的前缀,进行获取。定义类类的具体内容为下,[java] view plain importjava.util.HashMap; importjava.util.Map; importjava.util.Properties; importorg.springframework.beans.BeansException; importorg.springframework.beans.factory.config.; importorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer; publicclass { privatestatic Map ctxPropertiesMap; @Override protectedvoid processProperties( beanFactoryToProcess, Properties props)throws BeansException { super.processProperties(beanFactoryToProcess, props); ctxPropertiesMap =new HashMap(); for(Object key : props.keySet()) { String keyStr = key.toString(); if(keyStr.startsWith("project_")){ String value = props.getProperty(keyStr); ctxPropertiesMap.put(keyStr, value); } } } publicstatic Object getContextProperty(String name) { returnctxPropertiesMap.get(name); } } 定义获取配置文件中值的类SpringPropertiesUtil类的具体内容如下:[java] view plain importorg.springframework.beans.BeansException; importorg.springframework.context.ApplicationContext; importorg.springframework.context.ApplicationContextAware; importorg.springframework.stereotype.Component; /** * Spring-PropertiesUtil工具类 -获取属性值 * */ @Component publicclass SpringPropertiesUtil { publicstatic final String KEY = "propertyConfigurer"; privatestatic ApplicationContext applicationContext; publicvoid setApplicationContext(ApplicationContext applicationContext) throwsBeansException { SpringPropertiesUtil.applicationContext = applicationContext; } publicstatic ApplicationContext getApplicationContext() { returnapplicationContext; } /** * 获取配置文件中的内容 * * @param keyName * @return */ publicstatic String parseStr(String keyName) { cp = () applicationContext .getBean(KEY); returncp.getContextProperty(keyName).toString(); } /** * 获取配置文件中的内容 * * @param keyName * @return */ publicstatic int parseInt(String keyName) { cp = () applicationContext .getBean(KEY); returnInteger.parseInt(cp.getContextProperty(keyName).toString()); } /** * 获取配置文件中的内容 * * @param keyName * @return */ publicstatic double parseDouble(String keyName) { cp = () applicationContext .getBean(KEY); returnDouble.parseDouble(cp.getContextProperty(keyName).toString()); } } 这样,在项目当中就能够方便快速的获取properties文件中配置的参数如SpringPropertiesUtil.parseStr(“content”)

㈣ SpringBoot的配置文件有哪几种格式

SpringBoot中的配置文件来主要有三种格式,自properties、yaml、和xml方式。- 其中properties格式配置文件后缀是.properties,配置项为:server.port = 9090- yaml格式配置文件后缀是.yml,配置项是:server.port: 9090在SpringBoot中,使用最广泛的配置文件是yaml,yaml之所以流行,除了他配置语法精简之外,还因为yaml是一个跨编程语言的配置文件。在SpringBoot中,除了yaml之外,properties也比较常用,但是XML几乎不用,看得出来Spring团队非常痛恨XML配置文件!认为它不是一个好的语言。如果你对常见的配置文件有哪几种格式不熟悉,就去黑马程序员官网视频库看免费视频。

㈤ Spring配置文件中如何注入另一个XML中的配

主要有两种方式:1、在一个配置文件中使用import标签导入其他配置文件,即applicationContext.xml中部分代码如下:<import resource="applicationContext-.xml" /><import resource="applicationContext-service.xml" /><import resource="applicationContext-action.xml" />2、在web.xml中配置Spring配置文件处导入多个配置文件,即可a、导入多个配置文件web.xml部分代码如下:<context-param> <param-name>contextConfigLocation</param-name> <param-value> applicationContext-core.xml, applicationContext-.xml, applicationContext-service.xml, applicationContext-action.xml </param-value></context-param>b、使用*通配符导入多个配置文件web.xml部分代码如下:<context-param> <param-name>contextConfigLocation</param-name> <param-value> applicationContext-*.xml </param-value></context-param>

㈥ spring如何动态加载配置文件,就是配置文件修改了,application.xml如何能读取到

项目,需要访问多个数据库,而且需要在服务器运行不重新启动的情况下,动态的修改spring中配置的数据源datasource,在网上找了很多资料,最后找到了适合我的方法,下面总结一下。 spring的配置文件是在容器启动的时候就加载到内存中的,如果手动改了application.xml,我们必须要重新启动服务器配置文件才会生效。而在spring中提供了一个类WebApplicationContext,这个类可以让你获得一些bean,可以修改内存中的信息,我就是通过这个类来实现的。下面是我具体的代码。 package com.southdigital.hospital; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.mchange.v2.c3p0.ComboPooledDataSource; public class ChangeSpringConfig extends HttpServlet { private String ipAddress = "127.0.0.1"; /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //先取得servleContext对象,提供给spring的WebApplicationUtils来动态修改applicationContext.xml ipAddress = request.getParameter("ipAddress"); System.out.println(ipAddress); ServletContext servletContext = this.getServletContext(); WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext); ComboPooledDataSource cpds = (ComboPooledDataSource) applicationContext.getBean("dataSource"); cpds.setJdbcUrl("jdbc:mysql://"+ipAddress+":3306/ssh"); } } 注意:通过这种方法修改applicationContext.xml文件的时候用c3p0,而不可以用dbcp,dbcp不支持动态修改读取到内存里面的数据。spring 3.1已经支持了。

㈦ spring 配置文件的 import 元素是怎么实现的呢 假如每个配置文件都有import元素,那岂不是成递归了

这个想法很奇葩啊,可是你明明知道这样做会有问题为什么还会这样去做呢?其实也不会递归,因为要调用的只是文件中的某个方法或代码块,除非你方法中刚好在互相调用,否则不会出现这种情况,而且我怀疑系统也会报错,因为你相当于在写一个方法的时候调用了自己,可是它自己还不存在,嗯,乱了……

㈧ WEI-INF 下有多个Spring配置文件 import resource

<import resource="classpath*:/spring/*.xml" />是classes下的spring目录中所有的xml文件

㈨ SpringBoot自动配置的原理及实现/SpringBoot之@Import注解正确使用方式

https://www.jianshu.com/p/6b2f672e2446 了解SpringBoot之@Import注解正确使用方式 SpringBoot 的核心就是自动配置,自动配置又是基于条件判断来配置 Bean。关于自动配置的源码在 spring-boot -autoconfigure-2.0.3.RELEASE.jar 在通常需要我们在 property 中配置信息时,通常使用 @ConfigurationProperties(pefix=“前缀”) 注解的方式从配置文件中获取配置,如下: application.yml 中配置信息 访问 url 获取配置信息返回的值 http://localhost:8080/msg 如果把 application.yml 中的配置信息注释掉则默认使用 default 值,否则使用配置信息中的值,以上便是普通配置方式 SpringBoot 运行原理 先看 @SpringBootApplication 主要关注的几个注解如下 @SpringBootConfiguration:标记当前类为配置类 @EnableAutoConfiguration:开启自动配置 @ComponentScan:扫描主类所在的同级包以及下级包里的 Bean 关键是 @EnableAutoConfiguration 最关键的要属 @Import(.class),借助** **,@EnableAutoConfiguration 可以帮助 SpringBoot 应用将所有符合条件的 @Configuration 配置都加载到当前 SpringBoot 创建并使用的 IoC 容器: 通过 @Import(.class) 导入的配置功能, 中的方法 getCandidateConfigurations,得到待配置的 class 的类名集合, 这个集合就是所有需要进行自动配置的类,而是是否配置的关键在于 META-INF/spring.factories 文件中是否存在该配置信息 打开,如下图可以看到所有需要配置的类全路径都在文件中,每行一个配置,多个类名逗号分隔, 而 \ 表示忽略换行 整个流程如上图所示 以 类来看其主要构成部分 都能看到各种各样的条件判断注解,满足条件时就加载这个 Bean 并实例化 此类的条件注解是:@ConditionalOnProperty @ConditionalOnBean:当容器里有指定 Bean 的条件下 @ConditionalOnClass:当类路径下有指定的类的条件下 @ConditionalOnExpression:基于 SpEL 表达式为 true 的时候作为判断条件才去实例化 @ConditionalOnJava:基于 JVM 版本作为判断条件 @ConditionalOnJndi:在 JNDI 存在的条件下查找指定的位置 @ConditionalOnMissingBean:当容器里没有指定 Bean 的情况下 @ConditionalOnMissingClass:当容器里没有指定类的情况下 @ConditionalOnWebApplication:当前项目时 Web 项目的条件下 @:当前项目不是 Web 项目的条件下 @ConditionalOnProperty:指定的属性是否有指定的值 @ConditionalOnResource:类路径是否有指定的值 @:当指定 Bean 在容器中只有一个,或者有多个但是指定首选的 Bean 这些注解都组合了 @Conditional 注解,只是使用了不同的条件组合最后为 true 时才会去实例化需要实例化的类,否则忽略 这种 spring4.X 带来的动态组合很容易后期配置,从而避免了硬编码,使配置信息更加灵活多变,同时也避免了不必要的意外异常报错。使用的人只要知道配置的条件即可也不用去阅读源码,方便快捷,这也是 sprignboot 快捷方式带来的好处 参考 HttpEncodingAutoConfiguration 配置信息如下 案例扩展 项目 需要实例化的服务类 配置信息对应的属性映射类, 需要 pom 中加入 spring-boot-starter 依赖 自动配置文件 在创建如下路径文件 src/main/resources/META-INF/spring.factories 必须是自动配置类的全路径 mvn install 该项目 创建一个 springboot-mvc 项目 pom 依赖上面的 jar http://localhost:8080 / 则返回当前服务的默认值 在 applicaton.yml 中加, 重启刷新则会更新为如下信息 SpringBoot 自动化配置关键组件关系图 mybatis-spring-boot-starter、spring-boot-starter-web 等组件的 META-INF 文件下均含有 spring.factories 文件,自动配置模块中,SpringFactoriesLoader 收集到文件中的类全名并返回一个类全名的数组,返回的类全名通过反射被实例化,就形成了具体的工厂实例,工厂实例来生成组件具体需要的 bean。 在 spring boot 中有时候需要控制配置类是否生效, 可以使用 @ConditionalOnProperty 注解来控制 @Configuration 是否生效.


赞 (0)