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 notes of Spring

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "what are the annotations of Spring". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

# @ Configuration

Used to specify that the current class is a spring configuration class from which annotations are loaded when the container is created. You need to use AnnotationApplicationContext (.class with @ Configuration annotations) to get the container. When the annotated configuration class is used as a parameter created by the AnnotationConfigApplicationContext object, the annotation can be omitted

ApplicationContext ac = new AnnotationConfigApplicationContext (SpringConfiguration.class)

Attribute: value: bytecode used to specify the configuration class

# @ ComponentScan

Lets you specify the packages that spring will scan when initializing the container. The function is the same as the: in spring's xml configuration file.

Attribute: basePackages: used to specify the package to scan. It works the same as the value attribute in this annotation.

# @ Bean

This annotation can only be written on a method and is used to put the return value of the current method into the IOC container of spring. If the annotated method has parameters, the spring framework goes back to the container to see if there is a matching bean object, which is the same as the Autowired annotation.

Attribute: name specifies the id of the current bean, and the default value is the name of the current method.

Xml profile

Convert to comments

@ Configuration@ComponentScan (basePackages = "com.bx") public class SpringConfiguration {@ Bean (name = "runner") public QueryRunner createQueryRunner (DataSource dataSource) {return new QueryRunner (dataSource);} @ Bean ("datasource") public DataSource createDataSource () {try {ComboPooledDataSource ds = new ComboPooledDataSource (); ds.setDriverClass ("com.mysql.jdbc.Driver") Ds.setJdbcUrl ("jdbc:mysql://127.0.0.1:3306/demo?characterEncoding=utf-8"); ds.setUser ("root"); ds.setPassword ("baxiang"); return ds;} catch (Exception e) {e.printStackTrace ();} return null;}}

# @ PropertySource

Used to load the configuration in the .properties file. When you configure a data source, you can write information about connecting to the database to the properties configuration file, and you can use this annotation to specify the location of the properties configuration file.

Attribute: value []: used to specify the location of the properties file. If you are under the classpath, you need to write classpath

JdbcConfig.properties

Setting @ PropertySource annotation requires setting classpath

# @ Import

Used to import other configuration classes, and the @ Configuration annotation can be omitted when introducing other configuration classes.

Attribute: value []: bytecode used to specify other configuration classes. When we use Import annotations, the Import annotated class is the parent configuration class, and the imported child configuration classes

This is the end of the content of "what are the Notes of Spring". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report