随着越来越多地使用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. apache mpms和php概述

    当设置一个Apache + PHP服务器来运行你的PHP应用程序时,有许多配置参数需要处理. 最重要的是php服务器Api(server api:sapi),它决定了Apache将如何运行PHP脚本. ...

  2. CodeForces999A-Mishka and Contest

    A. Mishka and Contest time limit per test 1 second memory limit per test 256 megabytes input standar ...

  3. POJ2528---Mayor's posters

    The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign h ...

  4. Jomoo的模板

    目录 1 杂类算法 1.1 快读模板 1.2 O(1) int64 乘法 2 图论算法 2.1 树类 - Trie 2.2 树类 - 并查集(NB version) 2.3 树类 - LCA 2.4 ...

  5. jenkins持续集成工作原理、功能、部署方式等介绍

    超详细的jenkins持续集成工作原理.功能.部署方式等介绍 原创 波波说运维 2019-08-29 00:01:00 概述 今天简单整理了一下jenkins的一些概念性内容,归纳如下: 1.概念 j ...

  6. nmap扫描进阶、msfconsole攻击入门(网安全实训第二天)

    本期内容:nmap扫描.msfconsole攻击入门 1. nmap扫描进阶 2.msfconsole攻击入门 1.nmap扫描进阶 (1)nmap命令 nmap --sP -iL abin.txt ...

  7. 【MobX】391- MobX 入门教程(下)

    点击上方"前端自习课"关注,学习起来~ 三.MobX 常用 API 介绍 3. 修改可观察数据 在上一部分内容中,我们了解到,对可观察的数据做出反应的时候,需要我们手动修改可观察数 ...

  8. Java_计算1-100的和,奇数和

    public class Work1{ public static void main(String[] args){ // 定义和并赋值 int sum = 0; for(int i = 1;i & ...

  9. docker-compose部署

    一.部署compose docker compose可以方便我们快捷高效地管理容器的启动.停止.重启等操作,它类似于linux下的shell脚本,基于yaml语法,在该文件里我们可以描述应用的架构,比 ...

  10. 【Mock MVC】使用学习

    [Mock MVC]使用学习 转载:https://www.cnblogs.com/yangchongxing/p/10658311.html