Spring中@Configuration注解的使用

@Configuration注解主要用于定义配置类.可替换xml配置文件.被注解的类内部包含一个或多个被@Bean注解的方法.

这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描.并用于构建bean定义.初始化Spring

容器.

注意:带有@Configuration的注解是配置类.在配置类中写Bean.通过@Bean注解创建Bean对象.有这样一个例子:

配置类:相当于一个Spring容器

package com.jin.abc;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.jin.domain.Dog;

@Configuration            //注意写法
public class ApplicationConfig {

    public ApplicationConfig() {
        System.out.println("Spring容器启动");
    }
    
    
    @Bean(name="dog")            //定义一个bean,相当于
    public Dog getDog(){
        return new Dog("小白","白色");
    }
}


这个配置类就相当于下面这个配置文件的写法:



    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://code.alibabatech.com/schema/dubbo 
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        ">
    
        
        
    

    

    

下面是要从该Spring容器中拿到Dog    实例对象


一个具体的应用类:要用到Dog对象.

可以这样拿:


package com.jin.abc;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.jin.domain.Dog;

public class TestApplicationConfig {
    public static void main(String[] args) {
    
        //以注解的方式获取Spring容器中的对象,只不过该Spring容器的生成是在配置类中,而以前的获取Spring容器的方式是:
        
        /*以前获取Spring容器的方式:
        
            ApplicationContext ac = new ClassPathXmlApplicationContext("spring配置文件.xml");
            
            Dog dog = ac.getBean("dog");
            
            
            其实,对比这两种方式,各有各的好,xml方式,传统,比较熟悉.注解的方式,比较新颖,灵活,更加方便.轻盈.更加清晰.
        
        
        */
        
        AnnotationConfigApplicationContext acac = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        
        Dog dog = (Dog)acac.getBean("dog");
        
        System.out.println(dog);
    }
}

运行结果:

Spring容器启动
Dog [name=小白, age=白色]


Dog类:

package com.jin.domain;

import java.io.Serializable;

public class Dog implements Serializable {
    
    
    private String name;
    private String age;
    
    
    
    public Dog(String name, String age) {
        super();
        this.name = name;
        this.age = age;
    }
    public Dog() {
        super();
        // TODO Auto-generated constructor stub
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "Dog [name=" + name + ", age=" + age + "]";
    }
    
    
}

依赖包:


          org.springframework
          spring-webmvc
          4.2.4.RELEASE
      

      
          org.springframework
          spring-web
          4.2.4.RELEASE
      

      
          commons-logging
          commons-logging
          1.2
      

      
          log4j
          log4j
          1.2.12
      

      
          org.springframework
          spring-aop
          4.2.4.RELEASE
      

      
          org.springframework
          spring-aspects
          4.2.4.RELEASE
      

      
          org.springframework
          spring-beans
          4.2.4.RELEASE
      

      
          org.springframework
          spring-context
          4.2.4.RELEASE
      

      
          org.springframework
          spring-core
          4.2.4.RELEASE
      

      
          org.springframework
          spring-expression
          4.2.4.RELEASE
      

      
          org.springframework
          spring-jms
          4.2.4.RELEASE
      

      
          org.springframework
          spring-messaging
          4.2.4.RELEASE
      

      
          org.springframework
          spring-tx
          4.2.4.RELEASE
      

      

你可能感兴趣的:(java,Spring)