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. ...
随机推荐
- 洛谷 P2341 [HAOI2006]受欢迎的牛
题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...
- MySQL(MMM架构使用)
本案例要求基于普通版的MySQL服务器改造MMM架构,完成以下任务操作:启动MMM集群架构设置集群中服务器为online状态MySQL-MMM架构部署完成后需要启动,数据库端启动mmm-agent进程 ...
- /etc/auto.master - automounter的主映射文件
描述(DESCRIPTION) 当机器启动自动挂载器时, autofs(8) 脚本就会查寻 auto.master 这个主映射文件.文件中的每行分别指明,一个挂载点以及与对应的需要被挂载的文件系统.通 ...
- Parker Gear Pump - Gear Pump Seal Is More O-Ring: Role
Parker Gear Pump introduction Gear pump lip seal is mainly used in reciprocating dynamic seals. C ...
- 基于oauth2.0实现应用的第三方登录
OAuth2 OAuth2所涉及到的对象主要有以下四个: Client 第三方应用,我们的应用就是一个Client Resource Owner 资源所有者,即用户 Authorization Ser ...
- CGContextAddArc
这个函数让我在纸上画了半天才搞明白,把我的理解给大家分享下. void CGContextAddArc(CGContextRef c, CGFloat x, CGFloat y, CGFloat ra ...
- Linux部署Web项目小记
1.安装Tomcat 官网下载 解压缩:tar -zxvf apache-tomcat-8.0.32.tar.gz 配置server.xml 连接池: <Executor name=" ...
- 56.fielddata filter的细粒度内存加载控制
语法: POST /test_index/_mapping/test_type { "properties": { "test_field": { " ...
- 用python爬取一张仓鼠图片
一. 找到一张仓鼠图片并复制一下它的url url='http://img.go007.com/2017/08/16/c407f5b732f4e748_2.jpg' 二. 调用urllib库 impo ...
- 百度地图离线API 2.0(含示例,可完全断网访问)
由于公司需求,自己修改的离线地图API.该压缩包具有如下功能:1.支持使用google地图瓦片(不建议使用,效率不高,缩放级别较高时拖动有些卡顿,建议注释该代码块:overlayTileLayer.g ...