zoukankan      html  css  js  c++  java
  • Spring3系列3-JavaConfig

     从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

  • 相关阅读:
    I
    H
    G
    F
    E
    论js里面的for循环
    js常见问题之为什么点击弹出的i总是最后一个
    array类型的方法
    string类型的方法
    for in在对象和数组中的应用
  • 原文地址:https://www.cnblogs.com/jcomet/p/5570437.html
Copyright © 2011-2022 走看看