您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
18-Spring配置文件-import和知识要点
发布时间:2024-12-20 15:54:12编辑:雪饮阅读()
-
Spring Config配置文件尤其是配置特别多的注入时候就感觉挺乱的,不方便管理,那么其实还有一个import标签支持导入其它Spring Config配置文件,那么假如我们上篇中的哪些东西都是如果按业务划分是user模块的,那么我们可以这样创建一个如:applicationContext-User.xml
<?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 id="buser1" class="com.User">
<property name="name" value="user1"></property>
<property name="addr" value="shenzhen"></property>
</bean>
<bean id="buser2" class="com.User">
<property name="name" value="user2"></property>
<property name="addr" value="shanxi"></property>
</bean>
<bean id="userDao" class="com.dao.impl.UserDaoImpl">
<property name="name" value="zhangsan"></property>
<property name="age" value="18"></property>
<property name="strList">
<list>
<value>a</value>
<value>b</value>
<value>c</value>
</list>
</property>
<property name="userMap">
<map>
<entry key="mp_user1" value-ref="buser1"></entry>
<entry key="mp_user2" value-ref="buser2"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="key1">val1</prop>
<prop key="key2">val2</prop>
</props>
</property>
</bean>
<bean id="userService" class="com.service.impl.UserServiceImplement">
<constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>
</beans>
然后原来的applicationContext.xml直接import这个user模块:
<?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">
<import resource="applicationContext-User.xml"/>
</beans>
代码调用上面是没有任何改动的哈。
关键字词:Spring,import
上一篇:17-Spring配置文件-依赖注入7(map、list、properties类型注入)
下一篇:19-Spring相应API1(绝对路径加载Spring配置文件及解决无法读取方案文档 'httpwww.sspringframework.orgschemabeansspring-bean