The problem

1. The Beean does not know whether the configuration is successful

2. Check whether the configured Bean is contaminated

3. The -start(mybatis,mybatis-start) configuration is mixed with the user-defined configuration

Solution:

1. Use the test class junit-start

@SpringBootTest(classes = CsdzApplication.class) public class demo2 { @Autowired private DruidDataSource dataSource; @Test public void getLearn(){ System.out.println(dataSource.getActiveCount()); }}Copy the code

Use the Debug command to perform debugging

2. Configuration details

(1) With the -start identifier

Like Mybatis -start, which can be configured directly with configuration files in YML and loaded directly into SpringBoot.

(2) No -start logo

The Configuration must be set in Configuration

(3) Scenarios with -start identifier but personalized Configuration is still required (data source) Automatic Configuration fails when Configuration is configured

3. Configuration injection details

(1) Inject the contents of the configuration file into the class using @value

@ Value (” ${person. Last ‐ name} “)

@ConfigurationProperties(prefix = “person”)

(2) Write prefix above class

@ConfigurationProperties(prefix = "jdbc") 

public class JdbcProperties {}
Copy the code

(3) Write prefix on method

@ConfigurationProperties(prefix = "jdbc")
    public DataSource dataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        return dataSource;
    }
Copy the code

The set method is automatically injected into the returned object, but it will not work unless the returned object has such a set method. The input to this property is to set the annotated objects.