Spring中JavaConfig特性
从Spring3開始,增加了JavaConfig特性。JavaConfig特性同意开发人员不必在Spring的xml配置文件里定义bean,能够在Java Class中通过凝视配置bean,假设你讨厌XML,那么这样的特性肯定是让你感到愉悦的。
当然,你仍然能够用经典的XML方法定义bean。JavaConfig仅仅是还有一个替代方案。
1) 编辑pom.xml引入依赖包CGLIB
在建立一个project后,编辑pom.xml文件要想使用JavaConfig特性。必须引入CGLIB包,引入后,才干够在Class配置bean(Class前加凝视@Configuration表示是一个Spring配置类)
<!-- JavaConfig need cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
2)编写几个Java Bean例如以下:
package com.spring.hello;
public interface IAnimal {
public void makeSound();
}
package com.spring.hello;
public class Dog implements IAnimal{
public void makeSound(){
System.out.println("汪。汪。汪!");
}
}
package com.spring.hello;
public class Cat implements IAnimal{
public void makeSound(){
System.out.println("喵!喵!喵!
");
}
}
3)用JavaConfig特性配置Spring
<?
xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="cat" class="com.spring.hello.Cat"/>
<bean id="dog" class="com.spring.hello.Dog"/>
</beans>
然而在JavaConfig方法,则通过使用凝视@Configuration 告诉Spring,这个Class是Spring的核心配置文件,相似于XML文件里的beans。而且通过使用凝视@Bean定义bean
我们在 com.spring.hello包内建立一个类。类名为AppConfig
package com.spring.hello;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean(name="dog")
public IAnimal getDog(){
return new Dog();
}
@Bean(name="cat")
public IAnimal getCat(){
return new Cat();
}
}
4)接下来使用App.java来測试
package com.spring.hello;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.output.OutputHelper;
public class App {
private static ApplicationContext context;
public static void main(String[] args)
{
context = new AnnotationConfigApplicationContext(AppConfig.class);
IAnimal obj = (IAnimal) context.getBean("dog");
obj.makeSound();
IAnimal obj2 = (IAnimal) context.getBean("cat");
obj2.makeSound();
}
}
输出结果例如以下:
Spring中JavaConfig特性的更多相关文章
- spring中JavaConfig相关的注解
在spring3.0中增加配置spring beans的新方式JavaConfig,可以替换spring的applicataion.xml配置.也即@Configuration对等<beans/ ...
- 180531-Spring中JavaConfig知识小结
原文链接:Spring中JavaConfig知识小结/ Sring中JavaConfig使用姿势 去掉xml的配置方式,改成用Java来配置,最常见的就是将xml中的 bean定义, scanner包 ...
- 【原创】spring中的事务传播特性
关于spring的传播特性,我对其进行了详细的叙述了下: PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务.这是最常见的选择. 比如方法A调用方法B,如果方法 ...
- 事务特性,事务的隔离级别以及spring中定义的事务传播行为
.katex { display: block; text-align: center; white-space: nowrap; } .katex-display > .katex > ...
- Spring中使用JDBC
Spring中的数据库异常体系 使用JDBC(不使用Spring)的时候,我们需要强制捕获SQLException,否则无法使用JDBC处理任何事情.SQLException表示尝试访问数据库的时候出 ...
- Spring中文文档-第一部分
一. Spring 框架概述 Spring是为了构建企业应用的轻量级框架.然而,Spring是模块化的,允许你只是使用其中的一部分,不需要引入其他的.你可以在任何web框架上使用IoC容器,也可以只使 ...
- Spring中文文档
前一段时间翻译了Jetty的一部分文档,感觉对阅读英文没有大的提高(*^-^*),毕竟Jetty的受众面还是比较小的,而且翻译过程中发现Jetty的文档写的不是很好,所以呢翻译的兴趣慢慢就不大了,只能 ...
- spring中各jar功能及jar包之间的依赖关系
(1) spring-core.jar 这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心,当然你也可以在自己的应用系统中使用这些工 ...
- Spring中的jar包详解
下面给大家说说spring众多jar包的特点吧,无论对于初学spring的新手,还是spring高手,这篇文章都会给大家带来知识上的收获,如果你已经十分熟悉本文内容就当做一次温故知新吧.spring. ...
随机推荐
- Farseer.net轻量级ORM开源框架 V1.x 入门篇:表实体类映射
导航 目 录:Farseer.net轻量级ORM开源框架 目录 上一篇:Farseer.net轻量级ORM开源框架 V1.x 入门篇:数据库上下文 下一篇:Farseer.net轻量级ORM开源框 ...
- CREATE SCHEMA - 定义一个新的模式
SYNOPSIS CREATE SCHEMA schemaname [ AUTHORIZATION username ] [ schema_element [ ... ] ] CREATE SCHEM ...
- 所有的工作目录 都要svn_开头,并且要进行svn同步,你能保证你不删除,你保证不了非你!
所有的工作目录 都要svn_开头,并且要进行svn同步,你能保证你不删除,你保证不了非你! 血的代价啊~
- Oracle使用plsql连不上本地数据库,cmd中使用sqlplus连的上的可能解决方案
1.无监听程序 原因: 最有可能是oracle监听的服务没有启动起来. 2.ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 原因: 1.服务没有配置127.0.0.1或监听程序没 ...
- PHP 数组相加 和 数组合并array_merge
$arr1 = array("a"=>"PHP","b"=>"java","python" ...
- Bootstrap table的基础用法
一.官方文档 Bootstrap 中文网:http://www.bootcss.com/ Bootstrap Table 中文网 : http://bootstrap-table.wenzhixin. ...
- 题解 洛谷P1903/BZOJ2120【[国家集训队]数颜色 / 维护队列】
对于不会树套树.主席树的本蒟蒻,还是老老实实的用莫队做吧.... 其实这题跟普通莫队差不了多远,无非就是有了一个时间,当我们按正常流程排完序后,按照基本的莫队来,做莫队时每次循环对于这一次操作,我们在 ...
- mxnet.base.MXNetError: src/imperative/./imperative_utils.h:70: Check failed: inputs[i]->ctx().dev_mask() == ctx.dev_mask() (1 vs. 2)
mxnet 训练错误: mxnet.base.MXNetError: [14:42:22] src/imperative/./imperative_utils.h:70: Check failed: ...
- 树莓派 -- oled 续(1) wiringPi
在上文中,分析了wiringPi 的oled demo是使用devfs来控制spi master和spi slave通讯. https://blog.csdn.net/feiwatson/articl ...
- Buffer.alloc()
Buffer.alloc(size[, fill[, encoding]]) Node.js FS模块方法速查 size {Number} fill {Value} 默认:undefined enco ...