Spring注解如何实现applicationContext.xml效果
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,小编给大家分享一下Spring注解如何实现applicationContext.xml效果,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起
千家信息网最后更新 2025年11月07日Spring注解如何实现applicationContext.xml效果
小编给大家分享一下Spring注解如何实现applicationContext.xml效果,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
Spring注解--实现applicationContext.xml效果
随着越来越多地使用Springboot敏捷开发,更多地使用注解配置Spring,而不是Spring的applicationContext.xml文件。
Configuration注解: Spring解析为配置类,相当于spring配置文件
Bean注解:容器注册Bean组件,默认id为方法名
@Configurationpublic class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); }}等同于beans.xml文件
1)applicationContext.xml文件-包扫描
@ComponentScans(value = {@ComponentScan(value = "com.self",excludeFilters = { @Filter(type = FilterType.ANNOTATION,classes = {Controller.class}) })})@Configurationpublic class RootConfig { //测试Bean @Bean public Person person() { return new Person("张励",22,"工程师"); }}2)导入properties文件
@PropertySource(value = {"classpath:person.properties"})@Configurationpublic class MainConfigOfProperty { @Bean public Person person() { return new Person(); }}赋值
public class Person { @Value("${person.name}")//配置文件属性 private String name;}3)数据源
@EnableTransactionManagement//开启基于注解的事务管理功能@ComponentScan("com.self.ds")@Configurationpublic class TxConfig { //数据源 @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; } //事务管理器 @Bean public PlatformTransactionManager transactionManager() throws Exception{ return new DataSourceTransactionManager(dataSource()); } }单元测试
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("结果: " + bean); } @Test public void test() { String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames(); for(String beanDef:beanDefinitionNames) { System.out.println("输出: " + beanDef); } }}执行结果
以上是"Spring注解如何实现applicationContext.xml效果"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
注解
文件
配置
效果
篇文章
事务
事务管理
内容
数据
数据源
更多
结果
测试
管理
敏捷
不怎么
功能
单元
大部分
容器
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
开学第一课网络安全教育教案
东城区网络软件开发联系方式
ftp服务器win7
临沂大学春考网络技术专业
simcity连接服务器
网络安全包含的四层含义
电脑数据库文件可以删吗
仿真软件开发前景
网络安全考研有前途吗
什么系统可以做服务器稳定
南通软件开发薪资
网络技术与应用子网划分
dellamd入门服务器
如何界定互联网高科技企业
计算机网络安全隐患主要内容
怎样下载软件开发项目
眼镜行业软件开发厂家哪家好
soul为什么连接不了服务器
成都服务器报废推荐
优粉网络技术总监
oracle停止数据库步骤
织梦数据库恢复
小鹅网络技术有限公司
引进了数据库技术后的计算机系统
南宁APP软件开发招聘
发生网络安全事件的责任单位
互联网企业金融科技有限公司
数云高弹性数据库怎么用
网络技术工作表现评价
金秋十月网络安全宣传致辞