Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the contents of SpringBoot's study notes

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about what SpringBoot study notes are like. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

Java configures vs traditional xml configuration, taking DataSource configuration as an example

(1) introducing dependency

Com.alibaba

Druid

1.0.9

(2) add application.properties profile (spring default profile)

Jdbc.driverClassName=com.mysql.jdbc.Driver

Jdbc.url=jdbc:mysql://127.0.0.1:3306/atcrowdfunding

Jdbc.username=root

Jdbc.password=123

(3) write an entity class whose member attributes are url, username, driverClassName, and password that DataSource needs to configure, and add the annotation @ ConfigurationProperties (prefix = "jdbc"). Prefix represents the prefix of attributes in application.properties, such as jdbc.password=123.

@ ConfigurationProperties (prefix = "jdbc")

Public class JdbcProperty {

Private String url;private String username;private String driverClassName;private String password;public String getUrl () {return url;} public void setUrl (String url) {this.url = url;} public String getUsername () {return username;} public void setUsername (String username) {this.username = username;} public String getDriverClassName () {return driverClassName;} public void setDriverClassName (String driverClassName) {this.driverClassName = driverClassName;} public String getPassword () {return password;} public void setPassword (String password) {this.password = password;}

}

(3) call in other classes, add bean annotation, and return the object as DataSource, so that DataSource is put into the ioc container of spring.

/ / automatic injection

@ Bean

Public DataSource getDataSource () {

DruidDataSource dataSource = new DruidDataSource ()

Return dataSource

}

In addition, you can also use @ Autowired injection, constructor injection

(4) the most elegant configuration

Instead of writing the entity class JdbcProperty yourself, add the @ ConfigurationProperties (prefix = "jdbc") annotation and specify the attribute prefix.

@ Bean

@ ConfigurationProperties (prefix = "jdbc")

Public DruidDataSource getDataSource () {

Return new DruidDataSource ()

After reading the above, do you have any further understanding of the SpringBoot study notes? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report