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的更多相关文章

  1. Spring3系列4-多个配置文件的整合

    Spring3系列4-多个配置文件的整合 在大型的Spring3项目中,所有的Bean配置在一个配置文件中不易管理,也不利于团队开发,通常在开发过程中,我们会按照功能模块的不同,或者开发人员的不同,将 ...

  2. Spring3系列13-Controller和@RequestMapping

    Spring3系列13-Controller和@RequestMapping Controller返回值,String或者ModelAndView @RequestMapping关联url @Requ ...

  3. Spring3系列12- Spring AOP AspectJ

    Spring3系列12- Spring AOP AspectJ 本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代 ...

  4. Spring3系列11- Spring AOP——自动创建Proxy

    Spring3系列11- Spring AOP——自动创建Proxy 在<Spring3系列9- Spring AOP——Advice>和<Spring3系列10- Spring A ...

  5. Spring3系列10- Spring AOP——Pointcut,Advisor拦截指定方法

    Spring3系列10- Spring AOP——Pointcut,Advisor 上一篇的Spring AOP Advice例子中,Class(CustomerService)中的全部method都 ...

  6. Spring3系列9- Spring AOP——Advice

    Spring3系列9- Spring AOP——Advice Spring AOP即Aspect-oriented programming,面向切面编程,是作为面向对象编程的一种补充,专门用于处理系统 ...

  7. Spring3系列8- Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

  8. Spring3系列7- 自动扫描组件或Bean

    Spring3系列7- 自动扫描组件或Bean 一.      Spring Auto Scanning Components —— 自动扫描组件 1.      Declares Component ...

  9. Spring3系列6 - Spring 表达式语言(Spring EL)

    Spring3系列6-Spring 表达式语言(Spring EL) 本篇讲述了Spring Expression Language —— 即Spring3中功能丰富强大的表达式语言,简称SpEL.S ...

随机推荐

  1. poj 1556 The Doors

    The Doors Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u   Java ...

  2. Dynamic CRM 2013学习笔记(二十二)插件里调用WCF服务

      1. 添加service:     2.调用WCF BasicHttpBinding myBinding = new BasicHttpBinding(); myBinding.Name = &q ...

  3. node-webkit教程(14)禁用缓存

    1.在开发者工具中禁用缓存 上面这张图,是在node-webkit 中 在开发工具中配置禁用缓存的选项. 使用这个选项可以有效的禁用所有页面缓存. 2. 在配置文件中,配置webkit 缓存禁用和启用 ...

  4. 互联网上那些excel文件

    互联网上那些excel文件 文/玄魂 目录 互联网上那些excel文件 前言 1.1 查找包含指定值的excel文件 1.2 查找邮箱 1.3 查找身份证号 1.4  查找管理人员联系信息 1.5 获 ...

  5. servlet+jsp+java实现Web 应用

    servlet+jsp+java实现Web 应用 用java来构建一个web应用是特别容易的事情,jsp和php很像,可以嵌套在html中.程序的结构很简单,也很清楚,本文主要记录下大概的开发过程和环 ...

  6. Linux:Vim

    模式介绍: Vim具备6种基本模式和5中派生模式. 普通模式 启动后的默认模式,用于:移动光标.删除文本等待,常用命令: dd:删除当前行. [number]dd:连续执行number对应次数的dd命 ...

  7. [DOS] Net Use

    Please use following command for regist a login user. net use \\server\folder [password]  /user:[use ...

  8. Ubuntu SVN客户端安装

    查看系统版本: uname -a (Linux查看版本当前操作系统内核信息) cat /proc/version (Linux查看当前操作系统版本信息) 1.首先需要安装Ubuntu SVN.Ubun ...

  9. paip.c3p0 nullpointexcept 配置文件根路径读取bug 解决

    paip.c3p0 nullpointexcept 配置文件根路径读取bug 解决 windows ok linux犯错误... 查看loging, 初始化的时候儿jdbcurl,user,pwd都是 ...

  10. paip. http 405 的解决..

    paip.  http  405 的解决.. get>>> POST 或者 syeofe.. 作者Attilax  艾龙,  EMAIL:1466519819@qq.com 来源:a ...