第七课 从零开始学Spring boot 之 (自定义配置文件资源获取,扫描指定包下的文件)

1、自定义配置文件资源获取

    spring boot使用application.properties默认了很多配置。但需要自己添加一些配置的时候,我们应该怎么做呢。

 (1)、继续在application.ppoperties中添加

gh.username=gongh
gh.password=123456

    定义配置类:

package com.gongh.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * 自定义的配置文件
 * @author gh
 */
@ConfigurationProperties(prefix = "gh") 
public class GhCustom {
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

}

 (2)、新建properties文件

        若新用新的配置文件,如我在application.properties同目录下新建一个ghcustom.properties

    


需定义如下配置类:

package com.gongh.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * 自定义的配置文件
 * @author gh
 * 在配置类中采用@Component的方式注册为组件,然后使用@PropertySource来指定自定义的资源目录
 */
@Component
@ConfigurationProperties(prefix = "gh") 
@PropertySource("classpath:ghcustom.properties")
public class GhCustom {
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}	

}
最后注意在 spring Boot 入口类加上 @EnableConfigurationProperties
/**
 *使用@SpringBootApplication制定这是一个springboot的应用程序
 *
 */
@SpringBootApplication
@ServletComponentScan //是的spring能够扫描到我们自己编写的servlet和filter。
@EnableConfigurationProperties({GhCustom.class}) //加载自定义properties配置文件
public class App extends WebMvcConfigurerAdapter{
	    
	  public static void main(String[] args) {  
	          SpringApplication.run(App.class, args);  
	  }  
}	  

使用定义的properties

@RestController
public class TestController {
	
	@Autowired
	private GhCustom ghCustom;
	
	@RequestMapping("/test")  
	public String test(){  
		String str = "username: "+ ghCustom.getUsername()+"----,password: "+ghCustom.getPassword();
		System.out.println(str);  
		return str;  
	} 
}

打开浏览器,访问http://localhost:8080/test,得到

扫描二维码关注公众号,回复: 477303 查看本文章


2、扫描指定包下的文件

     在开发中我们知道Spring Boot默认会扫描启动类同包以及子包下的注解,那么如何进行改变这种扫描包的方式呢,

     原理很简单就是:

     在spring boot的启动类App.java中添加@ComponentScan注解进行指定要扫描的包以及要扫描的类。

 首先,我们在项目中新建一个包cn.company

在这里为了方便测试,我们让我们的类在启动的时候就进行执行,所以我们就编写两个类,实现接口CommandLineRunner,这样在启动的时候我们就可以看到打印信息了。

cn.company.MyCommandLineRunner :

package cn.company;
import org.springframework.boot.CommandLineRunner;

@Configuration
public class MyCommandLineRunner implements CommandLineRunner {

    @Override
    publicvoid run(String... args) throws Exception {
       System.out.println("MyCommandLineRunner run()");
    }
}

在spring boot的启动类中添加如下注解

@SpringBootApplication
@ServletComponentScan //是的spring能够扫描到我们自己编写的servlet和filter。
@EnableConfigurationProperties({GhCustom.class}) //加载自定义properties配置文件
@ComponentScan(basePackages={"cn.company"})//扫描指定包下的资源
public class App extends WebMvcConfigurerAdapter{
	    
	  public static void main(String[] args) {  
	          SpringApplication.run(App.class, args);  
	  } 
}

启动如果看到打印信息:

MyCommandLineRunner.run()

说明我们配置成功了。

这时候你会发现,在App.java同包下的都没有被扫描了,所以如果也希望App.java包下的也同时被扫描的话,那么在进行指定包扫描的时候一定要进行指定配置:

@ComponentScan(basePackages={"cn.company",com.gh"})




猜你喜欢

转载自blog.csdn.net/gonghua0502/article/details/80180902