💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
可以将不同功能的配置分别抽取到单独的配置文件中,这样方便维护,减少配置文件的臃肿。 **1. dao层配置`ApplicationContext-dao.xml`** ```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="userDao" class="learn.springconfigsplit.dao.impl.UserDaoImpl" /> </beans> ``` **2. service层配置`ApplicationContext-service.xml`** ```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="userService" class="learn.springconfigsplit.service.impl.UserServiceImpl" autowire="byName"/> </beans> ``` **3. 配置文件的合并** 合并方式1:直接在代码中合并。 ```java public class UserTest { private final ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext-dao.xml" , "ApplicationContext-service.xml"); @Test public void testGetUsername() { UserService userService = context.getBean(UserServiceImpl.class); System.out.println(userService.getUsername()); ///[getUsername] } } ``` <br/> 合并方式2:配置文件中合并`ApplicationContext.xml`。 ```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"> <!-- 导入其他配置文件 --> <import resource="ApplicationContext-dao.xml"/> <import resource="ApplicationContext-service.xml"/> </beans> ``` ```java public class UserTest { private final ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); @Test public void testGetUsername() { UserService userService = context.getBean(UserServiceImpl.class); System.out.println(userService.getUsername()); ///[getUsername] } } ``` **** 案例代码:https://gitee.com/flymini/codes03/tree/master/learn-springconfigsplit