Spring3系列3 -- JavaConfig
Spring3系列3-JavaConfig-1
从Spring3开始,加入了JavaConfig特性,JavaConfig特性允许开发者不必在Spring的xml配置文件中定义bean,可以在Java Class中通过注释配置bean。
当然,你仍然可以用经典的XML方法定义bean,JavaConfig只是另一个替代方案。
一、 环境
spring-framework-3.2.4.RELEASE
jdk1.7.0_11
Maven3.0.5
eclipse-jee-juno-SR2-win32
不必新建项目,仍然沿用之前的项目Spring3-Example(见“Spring3系列1-HelloWord例子”)
二、 编辑pom.xml引入依赖包CGLIB
要想使用JavaConfig特性,必须引入CGLIB包,引入后,才可以在Class配置bean(Class前加注释@Configuration表示是一个Spring配置类)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.lei.demo</groupId>
<artifactId>spring3-Example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>spring3-Example</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring3配置 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<!-- JavaConfig特性需要cglib包 -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
</project>
三、 编写几个Java Bean如下
接口IAnimal.java
package com.lei.demo.java_config;
public interface IAnimal {
public void makeSound();
}
Dog.java实现接口IAnimal
package com.lei.demo.java_config;
public class Dog implements IAnimal {
public void makeSound() {
System.out.println("汪、汪、汪------");
}
}
Cat.java实现接口IAnimal
package com.lei.demo.java_config;
public class Cat implements IAnimal {
public void makeSound() {
System.out.println("喵、喵、喵******");
}
}
四、 用JavaConfig特性配置Spring3
看一下xml配置bean和JavaConfig配置bean的不同。
Xml方法,配置定义bean格式类似如下。
<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-3.0.xsd">
<bean id="animal" class="com.lei.demo.java_config.Dog">
</beans>
JavaConfig方法,通过使用注释@Configuration 告诉Spring,这个Class是Spring的核心配置文件,并且通过使用注释@Bean定义bean
package com.lei.demo.java_config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class AppConfig { @Bean(name="animal")
public IAnimal getAnimal(){
return new Dog();
}
}
App.javar如下:
package com.lei.demo.java_config; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class App { private static ApplicationContext context; public static void main(String[] args) {
context = new AnnotationConfigApplicationContext(AppConfig.class);
IAnimal obj = (IAnimal) context.getBean("animal");
obj.makeSound(); } }
五、 目录结构

六、 输出结果
运行App.java

Spring3系列3 -- JavaConfig的更多相关文章
- Spring3系列4-多个配置文件的整合
Spring3系列4-多个配置文件的整合 在大型的Spring3项目中,所有的Bean配置在一个配置文件中不易管理,也不利于团队开发,通常在开发过程中,我们会按照功能模块的不同,或者开发人员的不同,将 ...
- Spring3系列13-Controller和@RequestMapping
Spring3系列13-Controller和@RequestMapping Controller返回值,String或者ModelAndView @RequestMapping关联url @Requ ...
- Spring3系列12- Spring AOP AspectJ
Spring3系列12- Spring AOP AspectJ 本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代 ...
- Spring3系列11- Spring AOP——自动创建Proxy
Spring3系列11- Spring AOP——自动创建Proxy 在<Spring3系列9- Spring AOP——Advice>和<Spring3系列10- Spring A ...
- Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法
Spring3系列10- Spring AOP——Pointcut,Advisor 上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都 ...
- Spring3系列9- Spring AOP——Advice
Spring3系列9- Spring AOP——Advice Spring AOP即Aspect-oriented programming,面向切面编程,是作为面向对象编程的一种补充,专门用于处理系统 ...
- Spring3系列8- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- Spring3系列7- 自动扫描组件或Bean
Spring3系列7- 自动扫描组件或Bean 一. Spring Auto Scanning Components —— 自动扫描组件 1. Declares Component ...
- Spring3系列6 - Spring 表达式语言(Spring EL)
Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...
随机推荐
- Eclipse 创建Maven项目的问题:a pom xml file already exists in the destination folder
创建过一个Maven项目,删除的时候只在Eclipse中删除了,但是磁盘上的这个项目没有删除,所以报错 方法:重新创建一个不同名称的Maven项目,右键项目,选择Properties,看你的项目目录( ...
- Magicodes.WeiChat——媒体资源选择组件之media-choice(开源)
media-choice为媒体资源选择组件,基于KnockoutJs.支持图片.语音.视频.图文的选择以及预览,支持默认选择类型,支持是否禁用选择类型的更改. 使用示例: <script id= ...
- Dynamic CRM 2013学习笔记(八)过滤查找控件 (类似省市联动)
我们经常要实现类似省市联动一样的功能,常见的就是二个查找控件,一个选择了省后,另一个市的查找控件就自动过滤了,只显示当前省下的市,而不是所有的市.当然这是最简单的,实际工作中还有更复杂的功能要通过过滤 ...
- 【Leetcode】【Hard】Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- C#设计模式(10)——组合模式(Composite Pattern)
一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系统中目录的处理就是这样的一个例子,因为目录可以包括单独的文件,也可以包括文件夹,文件夹又是由文件组成的,由于简单对象 ...
- Kali linux系列之 zmap 安装
Kali linux系列之 zmap 安装 官方文档地址:https://zmap.io/ 准备:保证有比较顺畅的更新源,可以更新系统,下载安装包. 安装 第一步:sudo apt-get insta ...
- NABC模型进行需求分析
N(需求): 图书租售管理系统.图书租售管理系统必须包含有完善的图书出租功能.还租.预订(租).会员管理.积分管理.简单的财务系统.详细分类统计.各数据排行榜.详细权限管理. A(做法): 因为有一些 ...
- 作业七:团队项目——Alpha版本冲刺阶段-04
昨天进展:象棋图片以及窗体完成 今天安排:代码编写.
- libevent (三) 事件注册与循环监听
事件注册与循环监听 在libevent中为了监听某种事件的发生,设置事件触发后的回调函数,也就是说对该事件注册到当前的IO模型中. 事件注册 事件初始化 使用`event_new`函数来对事件进行初始 ...
- 深入浅出OOP(一): 多态和继承(早期绑定/编译时多态)
在本系列中,我们以CodeProject上比较火的OOP系列博客为主,进行OOP深入浅出展现. 无论作为软件设计的高手.或者菜鸟,对于架构设计而言,均需要多次重构.取舍,以有利于整个软件项目的健康构建 ...