zoukankan      html  css  js  c++  java
  • Springboot整合Spring的.xm配置文件

    1、在进行Spring项目配置的时候,可以通过*.xml文件配置,也可以通过Bean(@Configuration注解)配置。SpringBoot延续了Spring这一特点,在SpringBoot项目中依然可以使用配置文件定义。

    首先,创建一个Service类,编写一个方法,如下所示:

    1 package org.springboot.tentent.service;
    2 
    3 public class TententService {
    4 
    5     public String hello() {
    6         return "hello Springboot !!!";
    7     }
    8 
    9 }

    在描述Springboot整合Spring的过程中,可能比较适合一些老的SSM项目向Springboot过渡的时候使用,特别是一些额外的.xml配置文件如何进行依赖进入呢?

    2、在src/main/resources目录中创建spring的子目录,并且建立spring-service.xml配置文件。

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xsi:schemaLocation="http://www.springframework.org/schema/beans
     5     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
     6 
     7     <!-- 定义bean的配置 -->
     8     <bean id="tententService"
     9         class="org.springboot.tentent.service.TententService"></bean>
    10 
    11 </beans>    

    在SampleController控制层程序类中注入TententService类对象,并且调用方法返回信息。

     1 package org.springboot.tentent.controller;
     2 
     3 import org.springboot.tentent.service.TententService;
     4 import org.springframework.beans.factory.annotation.Autowired;
     5 import org.springframework.web.bind.annotation.RequestMapping;
     6 import org.springframework.web.bind.annotation.RestController;
     7 
     8 @RestController
     9 public class SampleController {
    10 
    11     @Autowired
    12     private TententService tententService;
    13 
    14     @RequestMapping(value = "/message")
    15     public String message() {
    16         String hello = tententService.hello();
    17         return hello;
    18     }
    19 
    20 }

    注意:此时,要修改程序启动主类,定义要导入的Spring配置文件。

     1 package org.springboot.tentent;
     2 
     3 import org.springframework.boot.SpringApplication;
     4 import org.springframework.boot.autoconfigure.SpringBootApplication;
     5 import org.springframework.context.annotation.ImportResource;
     6 
     7 @SpringBootApplication // 启动Springboot程序,自带子包扫描
     8 // 本程序在定义启动主类时,利用@ImportResource注解导入了所需要的Spring配置文件,
     9 // 而后会自动将配置文件中定义bean对象注入到控制层类的属性中。
    10 @ImportResource(locations = { "classpath:/spring/spring-service.xml" })
    11 public class Springboot01Application {
    12 
    13     public static void main(String[] args) {
    14         SpringApplication.run(Springboot01Application.class, args);
    15     }
    16 
    17 }

    3、SpringBoot强调的就是“零配置”,约定大于配置,虽然其本身支持配置文件定义,但很明显这样的处理形式不是最好的。如果确定要引入其他配置,强烈建议使用Bean的配置形式来完成。但是如果你就是有需要进行此种方法配置的,可以使用@ImportResource导入你的配置文件进行bean的注入。

     1 package org.springboot.tentent.config;
     2 
     3 import org.springboot.tentent.service.TententService;
     4 import org.springframework.context.annotation.Bean;
     5 import org.springframework.context.annotation.Configuration;
     6 
     7 @Configuration // 定义本类为配置Bean
     8 public class DefaultConfig {
     9 
    10     @Bean(name = "tententService") // 定义Bean
    11     public TententService tententService() {
    12         return new TententService(); // 配置Bean对象
    13     }
    14 
    15 }

    此时,就可以将启动类的@ImportResource注解去掉,对于程序的主类,也就没有必要使用@ImportResource注解读取配置文件了。

    注意:在SpringBoot项目中进行配置的时候,实际上有3种支持,按照优先选择顺序为:application.yml、Bean配置和*.xml配置文件。大部分的配置都可以在application.yml(相当于传统项目中的profile配置作用)里面完成,但很多情况下会利用Bean类来进行扩展配置。之所以提供*.xml配置文件的支持,主要目的是帮助开发者用已有代码快速整合SpringBoot开发框架,所以在自己的项目过渡到Springboot的时候,掌握这一点也是很重要的哦。

  • 相关阅读:
    Prometheus环境搭建系列(三):监控redis服务器(redis_exporter)
    Java诊断神器:Arthas常用功能
    sql优化练习
    navicat:[Err] 1055
    Prometheus环境搭建系列(二):监控mysql服务器(mysqld_exporter)
    【笔试必备】常见sql笔试题
    将博客搬至CSDN
    springboot假死、连接泄露
    测试字符串
    阿里云初次使用教程
  • 原文地址:https://www.cnblogs.com/biehongli/p/13874069.html
Copyright © 2011-2022 走看看