随着越来越多地使用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. 设计模式GOF23(创建型模式)

    • 创建型模式:  单例模式.工厂模式.抽象工厂模式.建造者模式.原型模式.   • 结构型模式: –适配器模式.桥接模式.装饰模式.组合模式.外观模式.享元模式.代理模式.   • 行为型模式: 模 ...

  2. IO 单个文件的多线程拷贝

    package FileCopyThread; //自建的包,根据个人调整 import java.io.File; import java.io.FileNotFoundException; imp ...

  3. 2017 CCPC秦皇岛 G题 Numbers

    DreamGrid has a nonnegative integer . He would like to divide  into nonnegative integers  and minimi ...

  4. 2017 ACM/ICPC 沈阳 F题 Heron and his triangle

    A triangle is a Heron’s triangle if it satisfies that the side lengths of it are consecutive integer ...

  5. iSensor APP 之 摄像头调试 OV5642 续集2

    参考上一篇博客 作为续集,主要测试 RAW格式下的 不同分辨率效果 iSensor APP 之 摄像头调试 OV5642 直接上图吧 720p 拍照效果

  6. linux-touch 、mkdir、rm、pwd、which、locate、whatis

    1.touch: 创建空文件,修改文件时间戳信息 atime(access time):最近访问文件内容时间 mtime(modify time):最近修改文件内容时间 ctime(change ti ...

  7. 02 | Java内存模型:看Java如何解决可见性和有序性问题

    什么是 Java 内存模型? 导致可见性的原因是缓存,导致有序性的原因是编译优化,那解决可见性. 有序性最直接的办法就是禁用缓存和编译优化,但是这样问题虽然解决了,我们程序的性能可就堪忧了.   合理 ...

  8. 【Java Web开发学习】Servlet、Filter、Listener

    [Java Web开发学习]Servlet 转发:https://www.cnblogs.com/yangchongxing/p/9274739.html 1.Servlet package cn.y ...

  9. Spring Boot 打成的 jar 和普通的 jar 有什么区别 ?

    Spring Boot 打成的 jar 和普通的 jar 有什么区别 ? Spring Boot 打成的 jar Spring Boot的项目终止以jar包的形式进行打包,这种jar包可以通过可以通过 ...

  10. JVM系列二(垃圾收集算法).

    一.标记-清除算法(Mark-Sweep) 这种算法分为"标记"和"清除"两个阶段:首先标记出所有需要回收的对象,在标记完成后统一回收所有被标记的对象. Mar ...