在实际项目中使用了通过配置中心配
了数据库,而我们改了application.yml
并没有起作用,我们可以手动创建一个配置类来定义自定义的数据源,并在Spring容器中注册它
@Primary@Beanpublic DataSource customDataSource() {return DataSourceBuilder.create().url("jdbc:mysql://localhost:3306/customdb").username("root").password("password").driverClassName("com.mysql.cj.jdbc.Driver").build();}
然后在启动类上加上exclude = {DataSourceAutoConfiguration.class}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;//移除默认数据库配置类
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DbDemoApplication {public static void main(String[] args) {SpringApplication.run(DbDemoApplication.class, args);}}