当前位置: 移动技术网 > IT编程>开发语言>Java > Spring中多配置文件及引用其他bean的方式

Spring中多配置文件及引用其他bean的方式

2019年07月22日  | 移动技术网IT编程  | 我要评论
spring多配置文件有什么好处? 按照目的、功能去拆分配置文件,可以提高配置文件的可读性与维护性,如将配置事务管理、数据源等少改动的配置与配置bean单独分开。

spring多配置文件有什么好处?

按照目的、功能去拆分配置文件,可以提高配置文件的可读性与维护性,如将配置事务管理、数据源等少改动的配置与配置bean单独分开。

spring读取配置文件的几种方式:

1、使用spring自身提供的applicationcontext方式读取

在java程序中可以使用applicationcontext两个实现类classpathxmlapplicationcontext以及filesystemxmlapplicationcontext来读取多个配置文件,他们的构造器都可以接收一个配置文件数组。

如: applicationcontext ctx = new classpathxmlapplicationcontext(configlocations);与采用filesystemxmlapplicationcontext创建applicationcontext的方式相似,区别仅在于二者搜索配置文件的路径不同:classpathxmlapplicationcontext通过classpath路径搜索配置文件:而filesystemxmlapplicationcontext则在当前路径搜索配置文件。

方法一:在初始化时保存applicationcontext对象

代码:

applicationcontext ac = new filesystemxmlapplicationcontext("applicationcontext.xml"); 
ac.getbean("beanid"); 

说明:

这种方式适用于采用spring框架的独立应用程序,需要程序通过配置文件手工初始化spring的情况。

方法二:通过spring提供的工具类获取applicationcontext对象

代码:

import org.springframework.web.context.support.webapplicationcontextutils; 
applicationcontext ac1 = webapplicationcontextutils.getrequiredwebapplicationcontext(servletcontext sc) 
applicationcontext ac2 = webapplicationcontextutils.getwebapplicationcontext(servletcontext sc) 
ac1.getbean("beanid"); 
ac2.getbean("beanid"); 

说明:

这种方式适合于采用spring框架的b/s系统,通过servletcontext对象获取applicationcontext对象,然后在通过它获取需要的类实例。

上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。

方法三:继承自抽象类applicationobjectsupport

说明:

抽象类applicationobjectsupport提供getapplicationcontext()方法,可以方便的获取到applicationcontext。spring初始化时,会通过该抽象类的setapplicationcontext(applicationcontext context)方法将applicationcontext 对象注入。

方法四:继承自抽象类webapplicationobjectsupport

说明:

类似上面方法,调用getwebapplicationcontext()获取webapplicationcontext

方法五:实现接口applicationcontextaware

说明:

实现该接口的setapplicationcontext(applicationcontext context)方法,并保存applicationcontext 对象。spring初始化时,会通过该方法将applicationcontext 对象注入。

以上方法适合不同的情况,请根据具体情况选用相应的方法。

2、使用web工程启动时加载

在web.xml中配置web容器启动是自动加载哪些配置文件:

<context-param>
<param-name>contextconfiglocation</param-name>
<param-value>/web-inf/spring/spring-core.xml</param-value>
</context-param> 
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
<init-param>
<param-name>contextconfiglocation</param-name>
<param-value>/web-inf/spring/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> 

多个的时候可以用 * 号来代替。

<servlet> 
<servlet-name>app</servlet-name> 
<servlet-class> 
org.springframework.web.servlet.dispatcherservlet 
</servlet-class> 
<context-param> 
<param-name>contextconfiglocation</param-name> 
<param-value>/web-inf/applicationcontext*.xml,/web-
inf/user_spring.xml</param-value>
</context-param> 
<load-on-startup>1</load-on-startup> 
</servlet> 

3、xml配置文件中导入其他配置文件

在/web-inf/applicationcontext.xml配置应用服务去加载,可以在applicationcontext.xml中用import引入其他的配置文件

<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" 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-3.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.2.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<import resource="spring-servlet.xml"/>
<import resource="spring-security.xml"/>
<import resource="spring-hibernate.xml"/>
<import resource="spring-redis.xml"/>
</beans> 

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网