1
I have a project Java
with Spring Boot
that has to be mult-tenacy
and for that I’m trying to use the Flyway
to execute the migrate()
by an endpoint passing the data needed to connect to a specific database (url, username, password
) and to make connections with the database I am using the JdbcTemplate
and there’s the problem.
How the project has to be mult-tenacy
(can connect with several databases) I need to run the project without a datasource
configured (usually a url, user e password
of a bank in the archive application.properties
) That’s the problem because when executing the auto configuration
of datasource
and of JdbcTemplate
the Spring
of error (Consider revisiting the entries above or defining a bean of type 'javax.sql.DataSource' ou 'org.springframework.jdbc.core.JdbcTemplate' in your configuration.)
, I have researched and seen many people talking to put the classes of auto configuration
in the exclude
of @SpringBootApplication
, and even putting the two still can not execute the project.
Someone there can give me a light?
Edit 1:
here is the code from my archive application.properties
:
server.port=${port:8082}
spring.flyway.enabled=false
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
The main class:
@SpringBootApplication(scanBasePackages = "br.com.b2code")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RunAdm extends SpringBootServletInitializer implements
CommandLineRunner {
public static final String URL_FRONTEND = "*";
/**
* Método main do módulo de Gestão.
*
* @param args argumentos de inicialização
* @throws Exception uma exception genérica
*/
public static void main(String[] args) throws Exception {
SpringApplication.run(RunAdm.class, args);
}
@Override
protected SpringApplicationBuilder
configure(SpringApplicationBuilder application) {
return application.sources(RunAdm.class);
}
@Override
public void run(String... args) throws Exception {
}
}
and here a class I use the JdbcTemplate
:
@Repository
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ClienteQueryRepositoryImpl implements ClienteQueryRepository {
private final @NonNull
JdbcTemplate jdbc;
@Override
public List<Cliente> findAll() {
return jdbc.query(ClienteQuerySQL.SELECT_ALL_CLIENTE_SQL, new ClienteRowMapper());
}
}
And ai @Bruno, you can put your current configuration of
application.properties
and some code for how is your application configured today? (class where you are using Jdbctemplate and the app’s Main)– nullptr
I’ll give you a detail that may help, you can pass a data source instantiating your
JdbcTemplate
. You can mount the datasource as I passed the other answer (usingDataSourceBuilder
)– nullptr
Link to the documentation of Jdbctemplate
– nullptr
Opa @nullptr edited the question there and put the code, the way I ask Jdbctemplate I think we can not pass a datasource, if there was some way to create a Bean to set the datasource that did not run when the startup project, type a method I call and pass the parameters(url, password and user) for it to create the main datasource and Jdbctemplate would use this datasource... (ah and sorry for the delay in responding )
– Bruno Eduardo
You could have an abstract class for your repositories that receives as parameters the user, password and url, and use the same strategy I used in wrapper of the flyway, and create a
new JdbcTemplate(datasource)
, without an injection– nullptr
@nullptr Baah man, I think this is not very viable for me, but so what I really want is to be able to set the
datasource
in real time, because it is usually set in the startup of the project (Spring has theautoconfiguration
that does this automatically only needing you to put the bank properties in the fileapplication.properties
but you can also make aBean
that returns aDataSource
that he does the same thing)– Bruno Eduardo
I’ve already managed to make the project run with a
datasource
"empty" only that I do not know how to do, for example, that certain endpoint with parameters for the connection to the database (url, username and password) execute a Bean or a method that uses these parameters and arrow thedatasource
– Bruno Eduardo
I didn’t understand the not feasible, Voce will mount the datasource in Runtime, it’s not exactly what you want?
– nullptr
@nullptr could give me an example of this idea of creating an abstract class for my
respositorys
with the parameters to connect to the database (url, user, password) and tals... ?– Bruno Eduardo
Let’s go continue this discussion in chat.
– nullptr