随着越来越多地使用Springboot敏捷开发,更多地使用注解配置Spring,而不是Spring的applicationContext.xml文件。

  • Configuration注解: Spring解析为配置类,相当于spring配置文件
  • Bean注解:容器注册Bean组件,默认id为方法名
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}

等同于beans.xml文件

<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>

1)applicationContext.xml文件-包扫描

@ComponentScans(value = {@ComponentScan(value = "com.self",excludeFilters = {
@Filter(type = FilterType.ANNOTATION,classes = {Controller.class})
})
})
@Configuration
public class RootConfig { //测试Bean
@Bean
public Person person() {
return new Person("张励",22,"工程师");
}
}

2)导入properties文件

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

赋值

public class Person {

   @Value("${person.name}")//配置文件属性
private String name; }

3)数据源

@EnableTransactionManagement//开启基于注解的事务管理功能
@ComponentScan("com.self.ds")
@Configuration
public 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效果的更多相关文章

  1. Spring的配置文件ApplicationContext.xml配置头文件解析

    Spring的配置文件ApplicationContext.xml配置头文件解析 原创 2016年12月16日 14:22:43 标签: spring配置文件 5446 spring中的applica ...

  2. Spring注解配置和xml配置优缺点比较

    Spring注解配置和xml配置优缺点比较 编辑 ​ 在昨天发布的文章<spring boot基于注解方式配置datasource>一文中凯哥简单的对xml配置和注解配置进行了比较.然后朋 ...

  3. 【转载】Spring中的applicationContext.xml与SpringMVC的xxx-servlet.xml的区别

    一直搞不明白两者的区别. 如果使用了SpringMVC,事实上,bean的配置完全可以在xxx-servlet.xml中进行配置.为什么需要applicationContext.xml?一定必须? 一 ...

  4. Spring中,applicationContext.xml 配置文件在web.xml中的配置详解

    一.首先写一下代码结构. 二.再看web.xml中的配置情况. <?xml version="1.0" encoding="UTF-8"?> < ...

  5. 【Spring学习笔记-3.1】让bean获取spring容器上下文(applicationContext.xml)

    *.hl_mark_KMSmartTagPinkImg{background-color:#ffaaff;}*.hl_mark_KMSmartTagBlueImg{background-color:# ...

  6. spring加载ApplicationContext.xml的四种方式

    spring 中加载xml配置文件的方式,好像有4种, xml是最常见的spring 应用系统配置源.Spring中的几种容器都支持使用xml装配bean,包括: XmlBeanFactory , C ...

  7. spring容器扩展功能之一:spring加载ApplicationContext.xml的四种方式

    容器加载Bean的常见两个类ApplicationContext和BeanFactory, 一.首先,看看spring中加载配置在xml中的Bean对象到容器 spring 中加载xml配置文件的方式 ...

  8. Spring加载applicationContext.xml实现spring容器管理的单例模式

    package com.etc.pojo; import org.springframework.context.ApplicationContext; import org.springframew ...

  9. Spring加载applicationContext.xml实现spring容器管理的几种方式

    package com.etc.test; import org.junit.Test; import org.springframework.beans.factory.BeanFactory; i ...

随机推荐

  1. [TimLinux] django context_processor介绍

    1. context django里面 render 函数,HttpResponse,都有一个参数,context={},这个参数用于将视图层处理得到的数据传递到模板层. 2. context_pro ...

  2. Geoserver2.15.1 配置自带 GeoWebCache 插件发布 ArcGIS Server 瓦片(附配置好的 Geoserver2.15.1 下载)

    之前写过一篇关于 Geoserver2.8.5 版本的部署配置发布 ArcGIS Server 瓦片点击查看,那是下载 Geoserver2.8.5 源码编译,重新打包 jar 来部署配置思路的,版本 ...

  3. Python3 常用的几个内置方法

    目录 max()/min() filter() 过滤 map() 映射 sorted筛选 reduce()减少 max()/min() 传入一个参数 (可迭代对象), 返回这个可迭代对象中最大的元素 ...

  4. Python3 网络基础基础2

    目录 subprocess 粘包问题 问题原因 解决问题 上传大文件 UDP协议 SocketServer subprocess 可以通过代码执行操作系统的终端命令, 并返回终端执行命令后的结果 im ...

  5. postman设置全局变量

    //处理token var jsn = JSON.parse(responseBody) console.log(jsn.access_token) //把access_token设置到全局变量中 p ...

  6. 判断浏览器是否启用cookie

    <!DOCTYPE html> <html> <body onload="checkCookies()"> <script> fun ...

  7. 阿里云linux镜像发布web项目时候 tomcat与域名映射

    tomcat 与 域名映射 一  准备工作: 阿里云服务器Linux镜像 及 域名备案和将域名解析至服务器,通过<暂不说,网上一大把或者参考阿里官方实例手册> 在Linux镜像中安装tom ...

  8. 百万级高并发mongodb集群性能数十倍提升优化实践

    背景 线上某集群峰值TPS超过100万/秒左右(主要为写流量,读流量很低),峰值tps几乎已经到达集群上限,同时平均时延也超过100ms,随着读写流量的进一步增加,时延抖动严重影响业务可用性.该集群采 ...

  9. net core 3.1 跨域 Cors 找不到 “Access-Control-Allow-Origin”

    首先在ConfigureServices添加 public void ConfigureServices(IServiceCollection services) { services.AddCors ...

  10. BOM对象——History

    BOM对象--History <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...