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. ...
随机推荐
- 5-Java-C(位平方和)
题目描述: 把一个整数的每个数位都平方后求和,又得到一个整数,我们称这个整数为:位平方和. 对新得到的整数仍然可以继续这一运算过程. 比如,给定整数为4,则一系列的运算结果为: 16,37,58,89 ...
- CREATE DOMAIN - 定义一个新域
SYNOPSIS CREATE DOMAIN name [AS] data_type [ DEFAULT expression ] [ constraint [ ... ] ] where const ...
- Zend Studio 服务器根目录设置
在 Apache 服务器根目录里查找 \conf\httpd.conf 例如:C:\AppServ\Apache24\conf\httpd.conf 打开后查找 DocumentRoot 标记 修改调 ...
- [WDS] Warnings while compiling. vue项目运行控制台输出太多警告信息
vue项目运行控制台输出太多警告信息,我们需要vue 忽略警告,如Expected indentation of 0 spaces but found 2 这种警告都提示,很影响视觉体验! 解 ...
- Domain Adaptation论文笔记
领域自适应问题一般有两个域,一个是源域,一个是目标域,领域自适应可利用来自源域的带标签的数据(源域中有大量带标签的数据)来帮助学习目标域中的网络参数(目标域中很少甚至没有带标签的数据).领域自适应如今 ...
- 16第一章 ASP.Net编程基础知识
第一章 ASP.Net编程基础知识 第一章 ASP.Net编程基础知识 本章首先介绍用ASP.Net技术编制服务器端动态网页所需的网络和HTML标记语言方面的有关知识.然后 ...
- “完美”解决微信小程序购物车抛物动画,在连续点击时出现计算错误问题,定时器停不下来。
最近做,微信点餐小程序,遇到添加商品时出现抛物动画,参考借鉴了这位大神的方法 https://www.cnblogs.com/greengage/p/7815842.html 但出现了一个问题,连续点 ...
- extjs传递参数
以前我是这么传的: 先获取表Form,再通过表找组件,然后用gradeCode=0&groupCode=1传 现在只需要先获得表,然后通过form.getValues()取得所有的值,再使用E ...
- 树莓派 -- oled 续(1) wiringPi
在上文中,分析了wiringPi 的oled demo是使用devfs来控制spi master和spi slave通讯. https://blog.csdn.net/feiwatson/articl ...
- mysql数据库导出导入
MYSQL的命令行模式设置: 我的电脑->属性->高级系统设置->环境变量->系统变量-> 选择Path,在后面添加“;path\mysql\bin;”其中path为MY ...