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 ...
随机推荐
- ORA-30004 错误处理
一.问题情景: 原SQL: select c.CATEGORY_ID, c.CATEGORY_NAME, SYS_CONNECT_BY_PATH(c.CATEGORY_NAME ...
- 捉BUG记(To Catch a Bug)
大约有一年整没有写一篇博客了,由于各种原(jia)因(ban)导致闲暇时间要么拿着IPad看岛国奇怪的片(dong)子(hua).要么拿着kindle看各种各样的资(xiao)料(shuo).本来想写 ...
- Oracle必须死之奇怪的ORA-06502错误
作为熟练.Net码农以及非熟练Oracle用户很多时候Oracle总给我一种这货就是存心恶心我们的感觉. 虽然不得不承认Oracle是个很(an)好(gui)的产品,但是总有那么好几下被恶心到了.比如 ...
- Sqoop2 环境搭建
原文地址:http://www.cnblogs.com/luogankun/p/4209017.html 正在准备做Spark SQL external data source与关系型数据库交互的部分 ...
- [ACM_搜索] Triangles(POJ1471,简单搜索,注意细节)
Description It is always very nice to have little brothers or sisters. You can tease them, lock them ...
- Hive 安装配置
系统:Ubuntu 64 bit系统 step1:创建hive文件目录,并在hive目录下创建tmp,warehouse 和 log目录: Step2:解压hive安装包: Step3:创建配置文件: ...
- 插件~NuGet与packages管理项目的包包
NuGet NuGet是一个工具,已经被集成到VS开发环境里,直接使用它可以快速安装第三方的包包,包包就是一组DLL和资源,开发者把它们打包一个包包,或者叫做package,而我们使用nuget可以将 ...
- Leetcode 234 Palindrome Linked List 链表
判断链表是否是回文. 我直接将链表的一半进行倒置,然后将两半的链表进行比较 /** * Definition for singly-linked list. * struct ListNode { * ...
- 使用Nginx负载均衡搭建高性能.NETweb应用程序二
在文章<使用Nginx负载均衡搭建高性能.NETweb应用程序一>中,让我们对Nginx有了一个初步认识,下面我们将在windows平台下面使用Nginx演示集群部署我们的web应用. 一 ...
- 如何获得WPA握手包&EWSA破解WPA密码教程[zz]
获得WPA\WPA2 握手包的方法有很多,下面介绍通过aircrack-ng工具包来载获握手数据包. 1.下载aircrack-ng工具包. 2.终端里输入 sudo airmon-ng start ...