配置加载多个xml

要想配置多个xml有三种方法:
1. 
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext3.xml","另外一个xml文件");
Bean9 bean9 = (Bean9) act.getBean("bean9"); 
Bean0 bean0 = (Bean0) act.getBean("bean0");
bean9.fn();
2.
ApplicationContext act = new ClassPathXmlApplicationContext(new String[]{"applicationContext3.xml","另外一个xml文件"});
Bean9 bean9 = (Bean9) act.getBean("bean9"); 
Bean0 bean0 = (Bean0) act.getBean("bean0");
bean9.fn();
3.
在xml文件中配置,可以添加多个
<import resource="xxx.xml"/>
如果在文件中有重名问题
如果bean在同一个文件中有相同的id就会报错,
如果是导入的另一个xml文件中与当前主要的xml有相同的id,后面加载的会覆盖前面的

解决方法:可以为id起别名
name="别名1  别名2"或者name="别名1, 别名2"
这里我遇到一个问题 classloader init(unknown sources)
解决方式:刚开始我换了jdk也不行,最后将jar包3.2换成4.0以上(这里我也不知道为什么会这样)
还有一个问题就是注入属性报错
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.String com.array.ioc.annotation.UserService.name; 
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.String] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
需要设置requre属性   @Autowired(required=false)
junit的方法:
TestCase
TesuSuit
setUp
tearDown



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个快速开发的框架,提供了很多方便的工具,其中包括Mybatis框架。在使用Mybatis框架时,需要将Mapper.xml文件加载到应用程序中。 在Spring Boot中,可以使用@MapperScan注解将Mapper接口的包路径配置到扫描器中,然后使用@Mapper注解将Mapper接口注入到IOC容器中。但是,这种方式只会自动扫描Mapper接口,对于Mapper.xml文件需要手动进行配置。 首先,需要在application.properties或application.yml文件中配置Mapper.xml文件的路径,例如: mybatis.mapper-locations=classpath:mapper/*.xml 然后,需要创建SqlSessionFactoryBean并为其设置DataSource和Mapper.xml文件的位置: @Configuration public class MybatisConfig { @Value("${mybatis.mapper-locations}") private String mapperLocations; @Autowired private DataSource dataSource; @Bean public SqlSessionFactoryBean sqlSessionFactory() throws Exception { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource); sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources(mapperLocations)); return sessionFactory; } } 在创建完SqlSessionFactoryBean后,需要在Mapper接口中使用@Mapper注解,并通过@Autowired注入SqlSessionFactoryBean,例如: @Mapper public interface UserMapper { @Autowired SqlSessionFactory sqlSessionFactory; // ... } 最后,在需要使用Mapper接口的地方,可以直接将Mapper接口注入到Spring Bean中,并使用@Autowired注入即可,例如: @RestController public class UserController { @Autowired private UserMapper userMapper; // ... } 通过这种方式,就可以在Spring Boot中方便地加载Mapper.xml文件,并使用Mybatis框架完成相应的数据操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值