Spring系列十一:Spring @Configuration注解

2019/10/22 Spring

我欲穿花寻路,直入白云深处,浩气展虹霓。

https://raw.githubusercontent.com/longfeizheng/longfeizheng.github.io/master/images/spring/spring15.jpg

概述

Spring @Configuration注解有助于基于Spring注解的自动装配。@Configuration注解指示一个类声明了一个或多个@Bean方法,Spring容器可以对该类进行处理,以便在运行时为这些bean生成bean定义和服务请求。。

spring 2开始,我们就将bean配置写入xml文件中。但是Spring 3允许将bean定义移出xml文件。我们可以在Java文件本身中给出bean定义。这称为Spring Java Config功能(使用@Configuration注解)

Spring @Configuration注解用法

在任何类顶部使用@Configuration注解来声明该类提供了一个或多个@Bean方法,并且可以由Spring容器进行处理以在运行时为这些bean生成bean定义和服务请求。

@Configuration
public class AppConfig {
 
    @Bean(name="demoService")
    public DemoClass service()
    {
        
    }
}

Spring @Configuration注解示例

@Configuration注解的用法。

创建maven项目

mvn archetype:generate
    -DgroupId=cn.howtodoinjava.core
    -DartifactId=springCoreTest
    -DarchetypeArtifactId=maven-archetype-quickstart
    -DinteractiveMode=false
 
mvn eclipse:eclipse

更新Spring依赖项

<properties>
	<spring.version>3.0.5.RELEASE</spring.version>
  </properties>
 
  <dependencies>
      
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <!-- Spring 3 dependencies -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
		<version>${spring.version}</version>
	</dependency>

	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>${spring.version}</version>
	</dependency>
	
	<!-- For JavaConfig -->
	<dependency>
		<groupId>cglib</groupId>
		<artifactId>cglib</artifactId>
		<version>2.2.2</version>
	</dependency>
	
  </dependencies>

创建spring bean

public interface DemoManager {
    public String getServiceName();
}
 
public class DemoManagerImpl implements DemoManager
{
    @Override
    public String getServiceName()
    {
        return "My first service with Spring 3";
    }
}

Spring配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import cn.howtodoinjava.core.beans.DemoManager;
import cn.howtodoinjava.core.beans.DemoManagerImpl;
 
@Configuration
public class ApplicationConfiguration {
 
    @Bean(name="demoService")
    public DemoManager helloWorld()
    {
        return new DemoManagerImpl();
    }
}

测试

测试Java Config 可配置的bean


package cn.howtodoinjava.core.verify;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
import com.howtodoinjava.core.beans.DemoManager;
import com.howtodoinjava.core.config.ApplicationConfiguration;
 
public class VerifySpringCoreFeature
{
    public static void main(String[] args)
    {
        ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
 
        DemoManager  obj = (DemoManager) context.getBean("demoService");
 
        System.out.println( obj.getServiceName() );
    }
}

https://niocoder.com/assets/images/qrcode.jpg

🙂🙂🙂关注微信公众号java干货 不定期分享干货资料

原文链接:Spring @Configuration annotation example

Show Disqus Comments

Search

    Post Directory