In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this article, the editor introduces in detail "how to create Spring complex objects". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to create Spring complex objects" can help you solve your doubts.
What are complex types, such as Connection objects that connect to the database, and SqlSessionFactory objects in Mybatis.
In the past, we got the Connection object in this way:
Connection conn = null; try {Class.forName ("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/mysql", "root", "123456");} catch (ClassNotFoundException e) {e.printStackTrace ();} catch (SQLException e) {e.printStackTrace ();}
How do you create this type of object using Spring now? There are three ways to create complex objects in Spring
The first is to implement the FactoryBean interface public class ConnectionFactoryBean implements FactoryBean {/ / for writing code @ Override public Connection getObject () throws Exception {Class.forName (driverClassName); Connection conn = DriverManager.getConnection (url, username, password); return conn;} @ Override public Class getObjectType () {return Connection.class;} @ Override public boolean isSingleton () {return true } private String driverClassName; private String url; private String username; private String password; / / setter and getter ellipsis
In the applicationContext.xml configuration file
Interpretation of this usage: there are three abstract methods in the FactoryBean interface
If ○ just wants to get an object of type FactoryBean ctx.getBean ("& conn")
To get is a ConnectionFactoryBean object
○ isSingleton method
Returning true will only create a complex object
Return false each time a new object is created
Question: depending on the characteristics of this object, decide whether to return true (SqlSessionFactory) or false (Connection)
When creating a ○ mysql high version connection, you need to develop a SSL certificate to solve the problem.
Note: several properties that connect to the database in the class are added by yourself to facilitate injection in the configuration file to achieve decoupling.
The second method: the instance factory avoids the intrusion of Spring framework and integrates legacy systems.
Write methods to create complex objects directly in this class without implementing the FactoryBean interface.
Public class ConnectionFactory {public Connection getConnection () {Connection conn = null; try {Class.forName ("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/mysql", "root", "123456");} catch (ClassNotFoundException e) {e.printStackTrace () } catch (SQLException e) {e.printStackTrace ();} return conn;}}
But configure it in the configuration file
The third way: static factory
Similar to the instance factory, except that the instance method is replaced with a static method.
Public class StaticConnectionFactory {public static Connection getConnection () {Connection conn = null; try {Class.forName ("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/mysql", "root", "123456");} catch (ClassNotFoundException e) {e.printStackTrace () } catch (SQLException e) {e.printStackTrace ();} return conn;}}
The configuration of the corresponding profile is as follows:
After reading this, the article "how to create Spring complex objects" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, welcome to follow the industry information channel.
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.