- Spring Boot 2.0 Cookbook(Second Edition)
- Alex Antonov
- 300字
- 2021-06-24 19:24:38
Getting ready
In our previous example, we created the basic application that will execute a command-line runner by printing a message in the logs. Let's enhance this application by adding a connection to a database.
Earlier, we already added the necessary jdbc and data-jpa starters as well as an H2 database dependency to our build file. Now we will configure an in-memory instance of the H2 database.
To demonstrate the fact that just by including the H2 dependency in the classpath, we will automatically get a default database, let's modify our StartupRunner.java file to look as follows:
public class StartupRunner implements CommandLineRunner { protected final Log logger = LogFactory.getLog(getClass()); @Autowired private DataSource ds; @Override public void run(String... args) throws Exception { logger.info("DataSource: "+ds.toString()); } }
Now, if we proceed with the running of our application, we will see the name of the datasource printed in the log, as follows:
2017-12-16 21:46:22.067 com.example.bookpub.StartupRunner
:DataSource: org.apache.tomcat.jdbc.pool.DataSource@4... {...driverClassName=org.h2.Driver; ... }
So, under the hood, Spring Boot recognized that we've autowired a DataSource bean dependency and automatically created one initializing the in-memory H2 datastore. This is all well and good, but probably not all too useful beyond an early prototyping phase or for the purpose of testing. Who would want a database that goes away with all the data as soon as your application shuts down and you have to start with a clean slate every time you restart the application?
- 數(shù)據(jù)挖掘原理與實(shí)踐
- Learning Spring Boot
- Python廣告數(shù)據(jù)挖掘與分析實(shí)戰(zhàn)
- 大數(shù)據(jù)可視化
- 數(shù)據(jù)庫應(yīng)用系統(tǒng)開發(fā)實(shí)例
- Power BI智能數(shù)據(jù)分析與可視化從入門到精通
- Mastering LOB Development for Silverlight 5:A Case Study in Action
- 貫通SQL Server 2008數(shù)據(jù)庫系統(tǒng)開發(fā)
- 大數(shù)據(jù)數(shù)學(xué)基礎(chǔ)(Python語言描述)
- 領(lǐng)域驅(qū)動(dòng)設(shè)計(jì)精粹
- Oracle 內(nèi)核技術(shù)揭密
- Tableau商業(yè)分析從新手到高手(視頻版)
- 敏捷數(shù)據(jù)分析工具箱:深入解析ADW+OAC
- 一本書講透數(shù)據(jù)治理:戰(zhàn)略、方法、工具與實(shí)踐
- 信息技術(shù)導(dǎo)論