我正在编写一个简单的 CRUD 应用程序,该应用程序将从数据库中获取个人记录,并且我正在使用 SparkJava 框架,但我有可以获取的工作代码从数据库中记录,但我想提取 JOOQ DSLContext 代码并将其作为bean注入并在另一个类中进行初始化,以获取更多信息更干净的代码,但我不确定如何实现它,这是目前可容纳所有内容的主要方法:
public static void main(String[] args) throws IOException {
final BasicDataSource ds = new BasicDataSource();
final Properties properties = new Properties();
properties.load(BankApiapplication.class.getResourceAsStream("/application.properties"));
ds.setDriverClassname(properties.getProperty("db.driver"));
ds.setUrl(properties.getProperty("db.url"));
ds.setusername(properties.getProperty("db.username"));
ds.setPassword(properties.getProperty("db.password"));
final ConnectionProvider cp = new DataSourceConnectionProvider(ds);
final Configuration configuration = new DefaultConfiguration()
.set(cp)
.set(SQLDialect.H2)
.set(new ThreadLocalTransactionProvider(cp,true));
final DSLContext ctx = DSL.using(configuration);
final JSONFormat format = new JSONFormat().format(true).header(false).recordFormat(JSONFormat.RecordFormat.OBJECT);
port(8080);
get("/persons",(request,response) -> {
return ctx.select().from(Person.PERSON).fetch().formatJSON();
});
}
我如何提取用于初始化数据源并配置 DSLContext 的代码,而只能注入 DSLContext 或某种 DSLContextHolder 并进行查询?