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

Case Analysis of Spring Ioc and DI Annotation

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you the relevant knowledge points of Spring Ioc and DI annotation case analysis, the content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

1.Spring configuration data source

1.1 role of data sources (connection pooling) (common data sources (connection pooling): DBCP, C3P0, BoneCP, Druid, etc.)

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

1.1 Development steps for data sources

① import data source coordinates and database-driven coordinates

② creates C3P0 connection pool

Manual creation of data sources

① imports mysql database driver coordinates

② creates C3P0 connection pool

③ extract jdbc.properties profile

④ reads jdbc.properties configuration file to create connection pool

Spring configuration data source

You can leave the creation of DataSource to the Spring container (set injection is complete)

DataSource has no parameter construction method, while Spring instantiates the object by default through no parameter construction method.

To use DataSource, you need to set the database connection information through the set method, while Spring can do string injection through the set method.

4 extract jdbc configuration file (the last method is to implement the configuration data source)

ApplicationContext.xml loads the jdbc.properties configuration file to get connection information.

First, you need to introduce the context namespace and constraint paths:

Namespace: xmlns:context= "http://www.springframework.org/schema/context"

Constraint path:

Key points of knowledge

The Spring container loads the properties file

"="

Spring annotation development

Spring original comment

Spring original annotations are mainly alternative configurations

Note:

When developing with annotations, you need to configure component scanning in applicationContext.xml to specify which package and the Bean under its subpackages

Scanning is needed to identify classes, fields, and methods configured with annotations.

2, new notes on Spring

Using the above annotations does not completely replace the xml configuration file, and the configurations that need to be replaced by annotations are as follows:

Configuration of non-custom Bean:

Load the configuration of the properties file:-> @ PropertySource ("classpath:jdbc.properties") load the properties file needed externally

Configuration for component scanning:-> @ ComponentScan ("com.xxx") scanning required packages

Introduce additional files:

These are all the contents of the article "case Analysis of Spring Ioc and DI annotations". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.

Share To

Development

Wechat

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

12
Report