Get the App
SLTechnology News&Howtos  ›  Development  › 

How does springboot read template files

Shulou Source: shulou.com Published: 2022-06-03 17:36:03 10月01日 Update

This article mainly explains "springboot how to read template files", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "springboot how to read template files" bar!

Catalogue

Springboot reads template files

First kind

The second kind

The third kind

SpringBoot reads profile information

I. create a configuration file

Application.properties

Application.yml

Second, read configuration information

@ value

@ ConfigurationProperties

3. Read the specified environment configuration

Springboot reads template files

The template file under the template directory under resources

TemplateDir: the first kind of template/ Resource resource = new ClassPathResource (templateDir + templateName)

Unable to read in linux production environment, it may also be due to other reasons, the private network is not good-looking error

The second kind of ResourceLoader resourceLoader = new DefaultResourceLoader (); Resource resource = resourceLoader.getResource ("classpath:template/" + templateName); InputStream inputStream = resource.getInputStream ()

Can be read in a variety of environments

The third kind of Resource resource = new PathResource (templateDir + "black gray data sharing template .xls"); File file = resource.getFile ()

Uncertain linux environment

SpringBoot reads configuration file information 1. Create configuration file

When we create a new SpringBoot project, the default application.properties configuration file is automatically generated under the resources folder resources.

Application.properties

Its writing style is the full path at the decimal interval level. There are more in this old code.

Examples are as follows:

Server.port=8080spring.datasource.url=jdbc:mysql://localhost:3306/demospring.datasource.username=rootspring.datasource.password=root# presentation content demo.username=testdemo.password=testapplication.yml

Application.yml is different from application.properties in that it uses a "tree structure" writing style and reduces redundant code.

Note: there is only one space between the value of the variable and the name of the variable. String variables do not need quotation marks, and of course, adding them will not report an error.

Examples are as follows:

Server: port: 8080spring: datasource: url: jdbc:mysql://localhost:3306/demo username: root password: root # Demo content demo: username: test password: test II, read configuration information @ value

If you want to read information about single or several configuration values, you can introduce this member variable directly into the business Bean and add the @ value annotation declaration.

/ / other packages import org.springframework.beans.factory.annotation.Value;@Componentpublic class ReadConfigValueDemo {@ Value ("${demo.username}") private String username; @ Value ("${demo.password}") private String password; / / Business Code} @ ConfigurationProperties

If there are a lot of configuration files to read, or a set of related configuration files that you want to assemble and reuse in the system, then we can build the configuration Bean.

1. Add pom dependency

This is so that profile information can be scanned when configuring Bean in the second step.

Org.springframework.boot spring-boot-configuration-processor true

two。 Create configuration Bean

Through the prefix prefix attribute of ConfigurationProperties, we can specify a set of configuration values, and note that the property name should be the same as the configuration file, and the class name does not matter.

Import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component@ConfigurationProperties (prefix= "demo") public class DemoConfig {private String username; private String password; public String getUsername () {return username;} public String getPassword () {return password;}}

3. Used in business code

Where you need this set of configuration files, you can automatically inject them through the @ Resource or @ Autowired annotations.

Note: the class that injects the configuration Bean must itself be a Bean under Spring management, otherwise null values will be injected. This can happen on some utility classes that provide static methods.

@ Servicepublic class DemoServiceImpl {@ Resource private DemoConfig demoConfig; public void test () {/ / read the value System.out.println (demoConfig.getUsername ()) in the configuration Bean;}} III. Read the specified environment configuration

The SpringBoot project supports multiple configurations, such as production environment prod, development environment dev, test environment test, etc.

Take the application.yml format as an example:

# currently enable dev configuration file spring: profiles: active: dev

In this case, both application.yml and application-dev.yml can take effect. Configuration items with the same name are based on configuration files in a specific environment.

If we want to specify that the configuration Bean is only enabled in a certain environment, we can do the following:

@ Profile ("dev") / / effective only in dev environment @ Component@ConfigurationProperties (prefix= "demo") public class DemoConfig {/ /. Attribute} @ Profile ("! prod") / / prod environment does not take effect @ Component@ConfigurationProperties (prefix= "demo") public class DemoConfig {/ /. Attribute} Thank you for your reading, the above is the content of "how to read the template file from springboot". After the study of this article, I believe you have a deeper understanding of how to read the template file from springboot. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Configuration files environment templates information code content variables attributes business situation learning time annotations directories examples style presentation production consistency Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Docker MySQL vpn MariaDB