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

How to achieve applicationContext.xml effect with Spring annotations

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

Share

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

Editor to share with you how to achieve Spring comments to achieve applicationContext.xml effect, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

Spring annotations-- achieving applicationContext.xml effect

With the increasing use of Springboot agile development, more annotations are used to configure Spring rather than Spring's applicationContext.xml files.

Configuration note: Spring is parsed to configuration class, which is equivalent to spring configuration file

Bean note: the container registers Bean components. The default id is the method name.

@ Configurationpublic class AppConfig {@ Bean public MyService myService () {return new MyServiceImpl ();}}

Equivalent to beans.xml file

1) applicationContext.xml file-package scan

@ ComponentScans (value = {@ ComponentScan (value = "com.self", excludeFilters = {@ Filter (type = FilterType.ANNOTATION,classes = {Controller.class})}) @ Configurationpublic class RootConfig {/ / Test Bean @ Bean public Person person () {return new Person ("Zhang Li", 22, "engineer");}}

2) Import properties file

PropertySource (value = {"classpath:person.properties"}) @ Configurationpublic class MainConfigOfProperty {@ Bean public Person person () {return new Person ();}}

Assignment

Public class Person {@ Value ("${person.name}") / / profile properties private String name;}

3) data source

@ EnableTransactionManagement// enables annotation-based transaction management @ ComponentScan ("com.self.ds") @ Configurationpublic class TxConfig {/ / data source @ Bean public DataSource dataSource () throws Exception {ComboPooledDataSource dataSource = new ComboPooledDataSource (); dataSource.setUser ("root"); dataSource.setPassword ("000111") DataSource.setDriverClass ("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl ("jdbc:mysql://localhost:3306/self"); return dataSource;} @ Bean public JdbcTemplate jdbcTemplate () throws Exception {JdbcTemplate jdbcTemplate = new JdbcTemplate (dataSource ()); return jdbcTemplate } / / transaction Manager @ Bean public PlatformTransactionManager transactionManager () throws Exception {return new DataSourceTransactionManager (dataSource ());}}

Unit testing

Public class IOCTest {AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext (MainConfig.class); @ Test public void test02 () {Object bean1 = applicationContext.getBean ("person"); Object bean2 = applicationContext.getBean ("person"); System.out.println (bean1 = = bean2) } @ Test public void test01 () {Object bean = applicationContext.getBean ("person01"); System.out.println ("result:" + bean);} @ Test public void test () {String [] beanDefinitionNames = applicationContext.getBeanDefinitionNames () For (String beanDef:beanDefinitionNames) {System.out.println ("output:" + beanDef);}

Execution result

The above is all the content of the article "how Spring annotations achieve applicationContext.xml effect". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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.

Share To

Development

Wechat

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

12
Report