SpringBoot (Configuration 2)

1.@PropertySource
@PropertySource: Load the specified configuration file [properties].
Previously, we loaded the value in the global configuration file to javabean through @ConfifigurationProperties, but we will not save all the configurations used in the global configuration when we use it. In the file, different configurations may be saved in different configuration files, then we need @PropertySource annotation to load the specified configuration file for the specified javabean class.
For example:

package com.lx.springboot.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value = {"classpath:person.properties"})
public class PersonBean {
    private int perid;
    private String pername;
    private int perage;
    private String peraddress;

    public int getPerid() {
        return perid;
    }

    public void setPerid(int perid) {
        this.perid = perid;
    }

    public String getPername() {
        return pername;
    }

    public void setPername(String pername) {
        this.pername = pername;
    }

    public int getPerage() {
        return perage;
    }

    public void setPerage(int perage) {
        this.perage = perage;
    }

    public String getPeraddress() {
        return peraddress;
    }

    public void setPeraddress(String peraddress) {
        this.peraddress = peraddress;
    }
}
person.perid =1001
person.pername=zhangsan
person.perage=24
person.peraddress=上海
package com.lx.springboot.controller;
import com.lx.springboot.bean.PersonBean;
import com.lx.springboot.bean.StudentBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class TestController {
    @Autowired
    private StudentBean studentBean;
    @Autowired
    private PersonBean personBean;
    @RequestMapping(value = "/stuinfo")
    @ResponseBody
    public String getStudentInfo(){
        String stuinfo = studentBean.getStuid()+"\t"+
                studentBean.getStuname()+"\t"+
                studentBean.getStuage()+"\t"+
                studentBean.getStuaddress();
        return stuinfo;
    }
    @RequestMapping(value = "/perinfo")
    @ResponseBody
    public String getPersonInfo(){
        String stuinfo = personBean.getPerid()+"\t"+
                personBean.getPername()+"\t"+
                personBean.getPerage()+"\t"+
                personBean.getPeraddress();
        return stuinfo;
    }
}

2.
@ImportResource @ImportResource: Import the XML-based Spring configuration file to make the content in the configuration file effective;

package com.lx.springboot.bean;
public class UserBean {
    private int userid;
    private String username;
    private int userage;
    private String useraddress;

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return username;
    }

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

    public int getUserage() {
        return userage;
    }

    public void setUserage(int userage) {
        this.userage = userage;
    }

    public String getUseraddress() {
        return useraddress;
    }

    public void setUseraddress(String useraddress) {
        this.useraddress = useraddress;
    }
}
package com.lx.springboot.controller;
import com.lx.springboot.bean.UserBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.annotation.Annotation;

public class TestController implements Controller {

    private UserBean userBean;

    public UserBean getUserBean() {
        return userBean;
    }

    public void setUserBean(UserBean userBean) {
        this.userBean = userBean;
    }

    

    @Override
    public String value() {
        return null;
    }

    @Override
    public Class<? extends Annotation> annotationType() {
        return null;
    }
}

<?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="userBean" class="com.lx.springboot.bean.UserBean">
        <property name="userid" value="1003"></property>
        <property name="username" value="wangwu"></property>
        <property name="userage" value="25"></property>
        <property name="useraddress" value="北京"></property>
    </bean>
    <bean  name="/test" class="com.lx.springboot.controller.TestController">
       <property name="userBean" ref="userBean"></property>
    </bean>
</beans>

There is no Spring configuration file in Spring Boot, and the configuration file we wrote by ourselves cannot be automatically recognized; we
want Spring configuration file to take effect and load it in; @ImportResource is annotated on a main class.
3.
@Bean @Bean is added to the method Above, tell the springIOC container to create a javabean class object
. The return value of the
method is the javabean class object to be created . The name of the method is the name of the javabean class object.

Note: @Bean must appear in the configuration class [java class marked by @Configuration]
For example:

package com.lx.springboot.bean;

public class HelloService {
    public   void   getHelloServiceInfo(){
        System.out.println("HelloService类的测试方法");
    }
}

package com.lx.springboot.configuration;
import com.lx.springboot.bean.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//@Configuration注解标注某一个类为配置类
//代替基于XML的Spring配置文件
@Configuration
public class MyAppConfig {
    //@Bean添加在方法上,告诉springIOC容器创建javabean类对象
    //方法的返回值就是所要创建javabean类对象
    //方法的名称就是javabean类对象的名称
    //<bean id="对象的名称"  class="所要创建javabean类对象"></bean>
    @Bean
    public HelloService getHelloService(){
        return  new HelloService();
    }
}
package com.lx.springboot.controller;
import com.lx.springboot.bean.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
public class TestController {
    @Resource
    private HelloService getHelloService;
    @RequestMapping(value = "testinfo")
    @ResponseBody
    public  String  testinfo(){
        getHelloService.getHelloServiceInfo();
        return "测试@Bean";
    }
}

package com.lx.springboot.sptingboot7;

import com.lx.springboot.configuration.MyAppConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;

@SpringBootApplication
@ComponentScan(basePackages = "com.lx.springboot.controller")
@Import(MyAppConfig.class)
public class Sptingboot7Application {
    public static void main(String[] args) {
        SpringApplication.run(Sptingboot7Application.class, args);
    }
}

Insert picture description here

Placeholders in the configuration file [application.properties]
1, random number
random. value, {random.value},random.value{random.int}、${random.long}
r a n d o m . i n t ( 10 ) 、 {random.int(10)}、 r a n d o m . i n t ( 1 0 ) , {random.int[1024,65536]}
For example:

package com.lx.springboot.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@onfigurationProperties(prefix = "student")
public class StudentBean {
    private  int  stuid;
    private  String stuname;
    private  int stuage;
    private  String  stuaddress;

    public int getStuid() {
        return stuid;
    }

    public void setStuid(int stuid) {
        this.stuid = stuid;
    }

    public String getStuname() {
        return stuname;
    }

    public void setStuname(String stuname) {
        this.stuname = stuname;
    }

    public int getStuage() {
        return stuage;
    }

    public void setStuage(int stuage) {
        this.stuage = stuage;
    }

    public String getStuaddress() {
        return stuaddress;
    }

    public void setStuaddress(String stuaddress) {
        this.stuaddress = stuaddress;
    }
}

package com.lx.springboot.controller;

import com.lx.springboot.bean.StudentBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestController {
    @Autowired
    private StudentBean studentBean;
    @RequestMapping(value = "/test")
    @ResponseBody
    public String testStudent(){
        System.out.println(studentBean.getStuid());
        System.out.println(studentBean.getStuname());
        System.out.println(studentBean.getStuage());
        System.out.println(studentBean.getStuaddress());
        return "";
    }
}

student.stuid=${
    
    random.int}
student.stuname=zhangsan
student.stuage=${
    
    random.int[1024,65536]}
student.stuaddress=${
    
    student.stuname}_\u5730\u5740

Insert picture description here

Profiles
1.Profifile files are used to configure configuration data in different environments.
2. Because the data of the operating environment configured in the configuration file is different in different environments, we need to flexibly switch to the data of the corresponding operating environment under different operating environments. At this time, we will use different operating environments. The data is configured in different configuration files, and the switch is completed through the spring.profiles.active property in the main configuration file application.properties.
Test.properties configuration
application-dev.properties [development environment configuration]
server.port=8080
application-prod.properties [production environment configuration]
server.port=9090
application.properties [main configuration]
spring.profiles.active=prod [specify Use the production environment configuration]
http://localhost:9090/testInfo
or
spring.profiles.active=dev [Specify the use of the development environment configuration]
http://localhost:8080/testInfo

Test.yml configuration
application-devyml.yml [development environment configuration]

server: 
port: 8080

Insert picture description here

application-prodyml.yml【Production environment configuration】

server: 
port: 9090

Insert picture description here

application.yml [Main configuration]
spring:
profiles:
active: prodyml [Specify to use production environment configuration]
http://localhost:9090/testInfo
or
spring:
profiles:
active: devyml [Specify to use development environment configuration]
http://localhost :8080/testInfo The
above is to switch the operating environment configuration in 1. The main configuration file.
You can also specify which operating environment to use by configuring the 2. Operating environment parameter configuration view window
"--spring.profiles.active=dev"

You can also specify which operating environment to use when running the jar through the 3. command line
java -jar testspringboot002-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;
you can also specify by 4. Configure virtual machine parameters Which operating environment to use;
"-Dspring.profiles.active=dev"

Note: The name of the running environment configuration file is application-{profiles}.properties/yml The
main configuration file loading location
Spring boot startup will scan the application.properties or application.yml file in the following location as the default configuration file for Spring boot
-project root directory/ config/
– project root directory/
– resource/config/
– resource:/ The
above is in the order of priority from high to low, files in all locations will be loaded, and high-priority configuration content will override low-priority configuration content.
SpringBoot will load the main configuration file from these four locations; complementary configuration
We can also change the default configuration by configuring spring.config.location.
After the project is packaged, we can use command line parameters to specify the configuration when starting the project The new location of the file; the specified configuration file and the configuration files loaded by default work together to form a complementary configuration;
java -jar testspringboot02-0.0.1-SNAPSHOT.jar --spring.confifig.location=F:/application.properties
external configuration Loading sequence
Spring Boot supports multiple external configuration methods

  1. Command line parameters
  2. JNDI attributes from java:comp/env
  3. Java system properties (System.getProperties())
  4. Operating system environment variables
  5. Random.* property value configured by RandomValuePropertySource
  6. Application-{profile}.properties or application.yml (with spring.profile) configuration file outside the jar package
  7. Application-{profile}.properties or application.yml (with spring.profile) configuration file inside the jar package
  8. Application.properties or application.yml (without spring.profile) configuration file outside the jar package
  9. The application.properties or application.yml (without spring.profile) configuration file in the jar package is
    loaded first with profifile, and then loaded without profifile, and the jar package is searched outside the jar package
  10. @PropertySource on @Configuration annotated class
  11. The default properties specified by SpringApplication.setDefaultProperties

Guess you like

Origin blog.csdn.net/guoguo0717/article/details/110916504