Spring中JavaConfig特性
2024-09-05 10:45:48
从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();
}
}
输出结果例如以下:
最新文章
- 面试复习(C++)之直接插入排序
- UrlRewriter实现.NET的URL重写
- mysql for windows之my.ini优化
- ZPPR016-在制品清单报表
- gulp&;gulp-less
- 转载Agile Development 敏捷软件开发介绍
- CAS 4.0 配置开发手册(转)
- 《Programming WPF》翻译 第7章 4.转换
- ASP.NET MVC 4.0 学习4-Code First
- 使用c#操作txt
- Sublime Text编辑远程Linux服务器上的文件
- Java线程同步锁
- [Swift]LeetCode435. 无重叠区间 | Non-overlapping Intervals
- Chapter 5 Blood Type——1
- UML用例关系一览
- python全栈开发day101-认证组件、权限组件、频率组件
- sfc /scannow命令如何能用虚拟光驱完成修复?(xp下的办法)
- 实习培训——Java异常处理(8)
- 第十章&#160;优先级队列 (a2)基本实现
- [Php] Deprecated: Function ereg_replace() is deprecated
热门文章
- WebSocket 的一些简单页面推送使用
- eigenface资料整合
- Windos无法验证文件数组签名
- Python之__class__.__module__,__class__.__name__
- Adobe Dreamweaver CC 2014 代码颜色目录 dw
- spark版本不支持(降版本打包)
- Stack in c#
- 递归删除N天前的文件夹及子文件夹下的特定文件
- JAVA基础——设计模式之简单工厂模式
- [JOYOI] 1052 没有上司的舞会