In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the relevant knowledge of "what are the three ways of Spring configuring data sources". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "what are the three ways of Spring configuring data sources" can help you solve the problem.
I. the role of data sources
Data sources (connection pooling) are used to improve program performance as shown in
Instantiate the data source in advance and initialize part of the connected resources
Get from the data source when using the connection resource
Return the connection resource to the data source after use
Common data sources: DBCP, C3P0, BoneCP, Druid and so on. This paper mainly takes Druid data source as a case to realize the development and application of Spring to data source.
Second, the way to develop data sources 1: manual input
First create a maven project to introduce dependencies. For convenience, I also imported Junit dependencies, as well as mysql driver dependencies, Druid data source dependencies and spring dependencies
Junit junit 4.12 mysql mysql-connector-java 8.0.27 com.alibaba druid 1.1.22 org.springframework spring-context 5.3.14
Write a test class directly and start the test
@ Test public void test1 () throws SQLException {/ / create the data source object DruidDataSource dataSource = new DruidDataSource (); / / set the basic connection data for the data source dataSource.setDriverClassName ("com.mysql.cj.jdbc.Driver"); dataSource.setUrl ("jdbc:mysql://localhost:3306/test"); dataSource.setUsername ("root"); dataSource.setPassword ("0315") / / use the data source to obtain the connection resource Connection connection = dataSource.getConnection (); / / print the connection resource information System.out.println (connection); / / close the connection resource connection.close ();}
Analysis: setDriverClassName () fills in the package path of the connection driver class Driver, setUrl () sets the address of the database to be connected, setUsername () its own database user name, and setPassword () database password
Running result:
Mode 2:Properties profile
Create a file called jdbc.properties under resources and fill in the basic connection data of the data source
Jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/testjdbc.username=rootjdbc.password=0315
Write a test class and start testing
@ Test public void test2 () throws SQLException {/ / ResourceBundle is specially used to read files of type properties ResourceBundle bundle = ResourceBundle.getBundle ("jdbc"); / / set the basic connection data of the data source String driver = bundle.getString ("jdbc.driver"); String url = bundle.getString ("jdbc.url"); String username = bundle.getString ("jdbc.username") String password = bundle.getString ("jdbc.password"); DruidDataSource dataSource = new DruidDataSource (); dataSource.setDriverClassName (driver); dataSource.setUrl (url); dataSource.setUsername (username); dataSource.setPassword (password); DruidPooledConnection connection = dataSource.getConnection (); System.out.println (connection); connection.close ();}
This method is much better than method one. If the database we use has changed, we only need to modify it in the Properties file, so that there is no need to modify it from the code, which improves the efficiency of development.
Mode 3:Spring configuration data source
Continuing to use the previous jdbc.properties file, we can leave the creation of the data source to the Spring container, write a spring configuration file called applicationContext.xml, and put the data source in the spring container
Through this spring configuration file, we can get the data source, and then write a code to test
@ Test public void test3 () throws SQLException {ApplicationContext applicationContext = new ClassPathXmlApplicationContext ("applicationContext.xml"); DruidDataSource dataSource = applicationContext.getBean (DruidDataSource.class); DruidPooledConnection connection = dataSource.getConnection (); / / print connection information System.out.println (connection); connection.close ();}
Running result:
I don't know if my friends feel a little uncomfortable when they see the attribute value of value is so long. Anyway, I do. So is there a way to make the configuration clearer? The answer is: yes! So what should be done?
The first thing to do is to put the object of the jdbc.properties configuration file into the spring container, which facilitates future calls, the specific code:
Analysis: first, the namespace shown in the following figure should be introduced into the header file. Finally, the attribute value of value gets the value of jdbc.properties in the way of ${key}, and the running result is the same as above.
III. Summary
What we need to master most is the last method, and we must learn this way of configuration!
This is the end of the introduction to "what are the three ways for Spring to configure data sources". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.