spring-boot之(2)加载xml

spring-boot采用了减少配置文件的情况,从前面的例子可以看到是0配置文件,但是万一我们确实需要用到xml文件配置怎么办呢?下面介绍@ImportResource注解。

  • 在上一篇博客的基础上,新增一个类User.java
package com.fzhsh.microservice.bean;

public class User {
    private int id;
    
    private String username;
    
    private int age;
    
    private int sex;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }
    
    
}

 

 

  • 然后新建src/main/resources源文件夹(也就是新建 Source Folder),在文件夹下新增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" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-4.0.xsd
                              http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
                            http://www.springframework.org/schema/aop 
                            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                            http://www.springframework.org/schema/data/jpa
                            http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
    <bean id="user" class="com.fzhsh.microservice.bean.User"></bean>
</beans>

 

  • 然后在App类上加上@ImportResource注解指向spring-test.xml,并采用Autowired注入user,在调用hello方法时打印user是否被注入。
package com.fzhsh.microservice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fzhsh.microservice.bean.User;

/**
 * Hello world!
 *
 */
@RestController
@EnableAutoConfiguration
@ImportResource(locations="spring-test.xml")
public class App 
{
    
    @Autowired
    private User user;
    
    @RequestMapping("/")
    String hello(){
        System.out.println(user);
        return "hello world !!!";
    }
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        SpringApplication.run(App.class, args);
    }
}

 

  • 最后启动spring-boot应用(启动情参考前一章)后,访问localhost:8080检查后台打印
2016-05-08 14:05:21.958  INFO 5010 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 37 ms
com.fzhsh.microservice.bean.User@3849f2d7

 

参考资料:

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot-configuration-classes

posted @ 2016-05-08 22:57  風之殤  阅读(483)  评论(0)    收藏  举报