Springboot 加载xml

1.springboot 加载xml 下面两种方式都可以

Spring Boot @ImportResource注释示例
  Spring提供了一个  @ImportResource  注释,用于将spring-test.xml文件中的bean加载到Application Context中。

@ImportResource({ “ classpath *:spring-test.xml ” })
 

在Spring Boot的入口类中我们使用:


@SpringBootApplication
@ImportResource(locations = {"classpath:spring-test.xml"})
public class ProductApplication {

   public static void main(String[] args) {
      SpringApplication.run(ProductApplication.class, args);
   }
}

				   
推荐的方法是创建一个单独的配置类来加载此XML bean定义文件。

@Configuration
@ImportResource(locations = {"classpath:spring-test.xml"})
public class XmlConfiguration {
}
 

2.新建spring-test.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="xxlTestImpl" class="com.test.mybatisplus.service.impl.XxlTestImpl"/>
</beans>

3.新建XxlTestImpl 类

public class XxlTestImpl  {
    public String getTest() {
       return "getTest";
    }

}

4. 调用xxlTestImpl 方法

 @Autowired
    com.test.mybatisplus.service.impl.XxlTestImpl xxlTestImpl;

    @ApiOperation(value = "查询list",notes = "查询list")
    @RequestMapping(value ="/selectList" , method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public List<PressInfoEntity> selectList(){
        QueryWrapper<PressInfoEntity> queryWrapper=new QueryWrapper();
        queryWrapper.like("short_press","版");
        List<PressInfoEntity> list=   pressInfoService.selectList(queryWrapper);
        System.out.println("XML配置:"+xxlTestImpl.getTest());
        return  list;
    }

5.MybatisplusApplication 启动类上添加

@ImportResource(locations = {"classpath:spring-test.xml"})

@Slf4j
@SpringBootApplication
@MapperScan(value="com.test.mybatisplus.mapper")
@ImportResource(locations = {"classpath:spring-test.xml"})
//@EnableApolloConfig
public class MybatisplusApplication {

    public static void main(String[] args) {

        SpringApplication.run(MybatisplusApplication.class, args);

        //NettyServer.run(8080);
        //log.info("======netty服务已经启动========");
    }

}

 

6.启动项目 运行方法

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 中,我们可以通过配置 `LocalSessionFactoryBean` Bean 来加载 Hibernate 的配置文件和映射文件。下面是一个示例配置: ```java @Configuration @EnableTransactionManagement public class HibernateConfig { @Bean public LocalSessionFactoryBean sessionFactory() { LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); sessionFactory.setPackagesToScan("com.example.demo.entity"); sessionFactory.setHibernateProperties(hibernateProperties()); sessionFactory.setMappingLocations(new ClassPathResource("com/example/demo/entity/User.hbm.xml")); return sessionFactory; } @Bean public DataSource dataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8"); dataSource.setUsername("root"); dataSource.setPassword("password"); return dataSource; } private final Properties hibernateProperties() { Properties hibernateProperties = new Properties(); hibernateProperties.setProperty("hibernate.hbm2ddl.auto", "update"); hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); hibernateProperties.setProperty("hibernate.show_sql", "true"); return hibernateProperties; } } ``` 在上面的配置中,我们通过 `setMappingLocations` 方法设置了 Hibernate 的映射文件位置。这里我们将 `User.hbm.xml` 文件放在了 `com/example/demo/entity/` 目录下,所以需要设置为 `new ClassPathResource("com/example/demo/entity/User.hbm.xml")`。如果有多个映射文件,可以设置多个 `ClassPathResource` 对象。当然,你也可以使用 `PathMatchingResourcePatternResolver` 来扫描指定目录下的所有映射文件。最后,将 `LocalSessionFactoryBean` Bean 注入到其他需要使用 Hibernate 的 Bean 中即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值