ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringBoot配置----@PropertySource、@ImportResource、@Bean

2022-08-04 00:31:37  阅读:157  来源: 互联网

标签:ImportResource PropertySource SpringBoot map springframework import put org publ


一、@PropertySource

如果想使用项目加载特定的配置文件,可以使用@PropertySource

新建一个项目

 DemoApplication.java

package com.zk.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

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

}

 Person.java

package com.zk.demo;

import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import java.util.List;

@Component
//@Validated
@ConfigurationProperties(prefix = "person")
@PropertySource(value = "classpath:person.properties")
public class Person {
    //@Value("${person.last-name}")
    //@Email
    private String lastName;
    private String Sno;
    private int grade;
    private List<Object> list;
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getSno() {
        return Sno;
    }

    public void setSno(String sno) {
        Sno = sno;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public List<Object> getList() {
        return list;
    }

    public void setList(List<Object> list) {
        this.list = list;
    }

    @Override
    public String toString() {
        return "Person{" +
                "lastName='" + lastName + '\'' +
                ", Sno='" + Sno + '\'' +
                ", grade=" + grade +
                ", list=" + list +
                '}';
    }
}

 ZkController.java

package com.zk.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class ZkController {
    @Autowired
    private Environment environment;

    @Value("${server.port}")
    private Integer port;
    @Value("${alipay.pay.appid}")
    private String appid;
    @Value("${alipay.pay.notify}")
    private String notify;
    @Value("${java.version}")
    private String javaVersion;
    @Value("${JAVA_HOME}")
    private String javaHome;
    @Value("${MAVEN_HOME}")
    private String mavenHome;
    @Value("#{11*2}")
    private Integer number;

    @GetMapping("/read/file")
    public Map<String, Object> readInfo1() {
        Map<String, Object> map = new HashMap<>();
        map.put("port", environment.getProperty("server.port"));
        map.put("appid", environment.getProperty("alipay.pay.appid"));
        map.put("notify", environment.getProperty("alipay.pay.notify"));
        map.put("javaversion", environment.getProperty("java.version"));
        map.put("javahome", environment.getProperty("JAVA_HOME"));
        map.put("mavenhome", environment.getProperty("MAVEN_HOME"));
        map.put("number",environment.getProperty("number"));
        return map;
    }

    @GetMapping("/read/value")
    public Map<String, Object> readInfo2() {
        Map<String, Object> map = new HashMap<>();
        map.put("port", port);
        map.put("appid", appid);
        map.put("notify", notify);
        map.put("javaversion", javaVersion);
        map.put("javahome", javaHome);
        map.put("mavenhome", mavenHome);
        map.put("number",number);
        return map;
    }

    @GetMapping("/read/v2")
    public Map<String, Object> r2() {
        Map<String, Object> map = new HashMap<>();
        map.put("port", port);
        map.put("appid", appid);
        map.put("notify", notify);
        map.put("javaversion", javaVersion);
        map.put("javahome", javaHome);
        map.put("mavenhome", mavenHome);
        map.put("number",number);
        return map;
    }
}

 application.properties

# 应用名称
spring.application.name=demo
# 应用服务 WEB 访问端口
server.port=8080
spring.main.banner-mode=console
alipay.pay.appid=123456
alipay.pay.notify=http://www.xxx.com
# person.grade=0
# person.list=a,b,c
# person.last-name=张坤
# person.sno=1223
number=11

server.servlet.encoding.charset=utf-8
server.servlet.encoding.force=true
server.servlet.encoding.enabled=true

 person.properties

person.grade=0
person.list=a,b,c
person.last-name=张坤
person.sno=1223
SpringBootApplicationTest.java
package com.zk.demo;

import com.zk.demo.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootApplicationTest {

    @Autowired
    Person person;
    @Test
    public void ContextLoads(){
        System.out.println(person);
    }
}

 运行结果如下:

 使用@PropertySource可以读出特定配置文件中的数据

二、@ImportResource

@ImportResource的作用是导入Spring配置文件,让配置文件中的内容生效

SpringBoot里面没有Spring的配置文件,我们自己编写的配置文件也不能自动识别。

首先创建一个helloService

helloService.java

package com.zk.demo;

public class helloService {
}

beans.xml

在beans.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="helloService" class="com.zk.demo.helloService"></bean>
</beans>

 在SpringBootApplicationTest.java中编写测试用例

package com.zk.demo;

import com.zk.demo.Person;
import org.springframework.context.ApplicationContext;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootApplicationTest {

    @Autowired
    Person person;

    @Autowired
    ApplicationContext ioc;

    @Test
    public void testHelloService(){
        Boolean result=ioc.containsBean("helloService");
        System.out.println(result);
    }
    @Test
    public void ContextLoads(){
        System.out.println(person);
    }
}

 运行测试用例

 此时运行结果为false。

说明IOC此时没有引入beans.xml这个配置文件,识别不到HelloService

然后在DemoApplication.java中使用@ImportResource注解引入beans.xml

package com.zk.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

@ImportResource(locations = "classpath:beans.xml")
@SpringBootApplication
public class DemoApplication {

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

}

 此时获取值为true

标签:ImportResource,PropertySource,SpringBoot,map,springframework,import,put,org,publ
来源: https://www.cnblogs.com/longlyseul/p/16549255.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有