什么是JavaConfig

java config是指基于java配置的spring。传统的Spring一般都是基本xml配置的,后来spring3.0新增了许多java config的注解,特别是spring boot,基本都是清一色的java config。

@Configuration

    在类上打上这一标签,表示这个类是配置类

@ComponentScan

   相当于xml的<context:componentscan basepakage=>

@Bean

     bean的定义,相当于xml的

     <bean id="objectMapper" class="org.codehaus.jackson.map.ObjectMapper" /> 

@EnableWebMvc

    相当于xml的<mvc:annotation-driven>

@ImportResource

   相当于xml的 <import resource="applicationContext-cache.xml">

@PropertySource

spring 3.1开始引入,它是基于java config的注解,用于读取properties文件

 @Profile

spring3.1开始引入,一般用于多环境配置,激活时可用@ActiveProfiles注解,@ActiveProfiles("dev")

等同于xml配置

<beans profile="dev">
    <bean id="beanname" class="com.pz.demo.ProductRPC"/>
</beans>

激活该profile spring.profiles.active,也可设置默认值 spring.profiles.default
<context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>dev</param-value>
</context-param>

Spring IOC有一个非常核心的概念——Bean。由Spring容器来负责对Bean的实例化,装配和管理。XML是用来描述Bean最为流行的配置方式。Spring可以从XML配置文件中读取任何类型的元数据并自动转换成相应的Java代码。Spring改变了java的编程模式,Spring的下载已经超过了1亿次,可见Spring已经有多流行。

随着Spring的日益发展,越来越多的人对Spring提出了批评。“Spring项目大量的烂用XML”就是最为严励的一个批评。由于Spring会把几乎所有的业务类都以Bean的形式配置在XML文件中,造成了大量的XML文件。使用XML来配置Bean失去了编译时的类型安全检查。大量的XML配置使得整个项目变得更加复杂。Rod Johnson也注意到了这个非常严重的问题。

当随着JAVA EE 5.0的发布,其中引入了一个非常重要的特性——Annotations(注释)。注释是源代码的标签,这些标签可以在源代码层进行处理或通过编译器把它熔入到class文件中。在JAVA EE 5以后的版本中,注释成为了一个主要的配置选项。Spring使用注释来描述Bean的配置与采用XML相比,因类注释是在一个类源代码中,可以获得类型安全检查的好处。可以良好的支持重构。

JavaConfig就是使用注释来描述Bean配置的组件。JavaConfig 是Spring的一个子项目, 比起Spring,它还是一个非常年青的项目。目前的版本是1.0 M2。使用XML来配置Bean所能实现的功能,通过JavaConfig同样可以很好的实现。

考虑使用JavaConfig替代XML配置

对于总是固执地使用Spring的XML配置方式,同事们总是讥讽我。是的,这看起来太二太过时了,不过XML还是有它的优势:

1.集中式配置。这样做不会将不同组件分散的到处都是。你可以在一个地方看到所有Bean的概况和他们的装配关系。

2.如果你需要分割配置文件,没问题,Spring可以做到。它可以在运行时通过<import>标签或者上Context文件对分割的文件进行重新聚合。

3.相对于自动装配(autowiring),只有XML配置允许显示装配(explicit wiring)

4.最后一点并不代表不重要,XML配置完全和JAVA文件解耦:两种文件完全没有耦合关系,这样的话,类可以被用作多个不同XML配置文件。

XML唯一的问题是,只有在运行时环境时你才能发现各种配置及语法错误,但是如果使用Spring IDE Plugin(或者STS)的话,它会在编码时提示这些问题。

在XML配置和直接注解式配置之外还有一种有趣的选择方式-JavaConfig,它是在Spring 3.0开始从一个独立的项目并入到Spring中的。它结合了XML的解耦和JAVA编译时检查的优点。JavaConfig可以看成一个XML文件,只不过是使用Java编写的。相关文档在官方网站是可以找到的,这篇文章只是带你了解JavaConfig。下面的例子,让我们将从XMl文件移植到JavaConfig。

XML配置文件:

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.  
     
  6.  
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
  7.  
     
  8.  
    <bean id="button" class="javax.swing.JButton">
  9.  
    <constructor-arg value="Hello World" />
  10.  
    </bean>
  11.  
     
  12.  
    <bean id="anotherButton" class="javax.swing.JButton">
  13.  
    <property name="icon" ref="icon" />
  14.  
    </bean>
  15.  
     
  16.  
    <bean id="icon" class="javax.swing.ImageIcon">
  17.  
    <constructor-arg>
  18.  
    <bean class="java.net.URL">
  19.  
    <constructor-arg value="http://morevaadin.com/assets/images/learning_vaadin_cover.png" />
  20.  
    </bean>
  21.  
    </constructor-arg>
  22.  
    </bean>
  23.  
    </beans>
  1.  
    import java.net.MalformedURLException;
  2.  
    import java.net.URL;
  3.  
    import javax.swing.Icon;
  4.  
    import javax.swing.ImageIcon;
  5.  
    import javax.swing.JButton;
  6.  
    import org.springframework.context.annotation.Bean;
  7.  
    import org.springframework.context.annotation.Configuration;
  8.  
     
  9.  
    @Configuration
  10.  
    public class MigratedConfiguration {
  11.  
    @Bean
  12.  
    public JButton button() {
  13.  
    return new JButton("Hello World");
  14.  
    }
  15.  
     
  16.  
    @Bean
  17.  
    public JButton anotherButton(Icon icon) {
  18.  
    return new JButton(icon);
  19.  
    }
  20.  
     
  21.  
    @Bean
  22.  
    public Icon icon() throws MalformedURLException {
  23.  
    URL url = new URL(
  24.  
    "http://morevaadin.com/assets/images/learning_vaadin_cover.png");
  25.  
    return new ImageIcon(url);
  26.  
    }
  27.  
    }

用法非常简单:

1.用@Configuration注解JavaConfig类,

2.用每个方法来表示Bean并使用@Bean注解方法。

3.每个方法名代表XML配置文件中的name

注意在Web环境中,需要在web.xml中加入如下代码:

  1.  
    <context-param>
  2.  
    <param-name>contextClass</param-name>
  3.  
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
  4.  
    </context-param>
  5.  
    <context-param>
  6.  
    <param-name>contextConfigLocation</param-name>
  7.  
    <param-value>com.packtpub.learnvaadin.springintegration.SpringIntegrationConfiguration</param-value>
  8.  
    </context-param>
 
 原文:https://blog.csdn.net/AlbenXie/article/details/82633775
原文地址:https://www.cnblogs.com/coder-ahao/p/14225868.html