2017-03-02 60 views
3

我想了解SpringMVC Web应用程序中的setter注入,我可以找到的所有示例使用xml显示。不过,我被告知xml已被弃用,所有新的应用程序都应该使用java配置完成。这是错误的,我应该使用XML来配置我的应用程序?如何在Spring中对java配置使用setter注入?

我应该在哪里声明bean,我该怎么做?

这是我见过的例子之一,但它是用xml实现的。

<?xml version="1.0" encoding="UTF-8"?> 
<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.xsd"> 

    <bean id="message" 
     class="org.springbyexample.di.xml.SetterMessage"> 
    <property name="message" value="Spring is fun." /> 
    </bean> 

</beans> 

回答

2

我会建议先看看普通的Spring配置,以了解基本事物(如注入)的工作原理。如果你设法在Spring中获得它的悬挂,这个过程在Spring MVC/Spring Boot /等中将非常类似。就我个人而言,我发现它试图一次性处理多个概念(视图解析器,不同的配置文件,视图,存储库,多个注释,多种配置方式等),所以我倾向于从最简单的概念开始并构建我的一路攀升。一旦您对注射的工作方式感到满意,您可以在其他地方轻松应用这些知识。

至于java配置和注释,它们允许更快更清洁的开发。 XML非常冗长,很难维护,并且很容易出错,部分原因是IDE在处理基于Java的配置时通常更有帮助。也许这就是为什么你阅读XML已被弃用。我会推荐去java/auto配置而不是XML,除非你真的需要(或者对它感兴趣)。

现在谈谈如何做到这一点。一个完整的(但最少)工作弹簧例如:

/* Bean definition 

@Component tells Spring that this is a bean. There are a few similar annotations. 
It will be discovered during the component scan, as it has @Component annotation */ 

package main.java.org.example; 
import org.springframework.stereotype.Component; 

@Component 
public class Greeting { 
    private String greeting = "Hello"; 

    public String getGreeting() { 
     return this.greeting; 
    } 

    public void setGreeting(String greeting) { 
     this.greeting = greeting; 
    } 
} 


/* Another bean definition. 
It has another bean as a dependency, which we inject with a setter. */ 

package main.java.org.example; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 

@Component 
public class GreetingCollector { 
    private Greeting greeting; 

    /* This is how you do setter injection */ 
    @Autowired 
    public void setGreeting(Greeting greeting) { 
     this.greeting = greeting; 
    } 

    public String getGreeting() { 
     return greeting.getGreeting(); 
    } 
} 


/* This is a minimal config class. 
@ComponentScan instructs to look for classes that are 
annotated with @Component annotation (in other words, beans) */ 

package main.java.org.example;  
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 

@ComponentScan 
@Configuration 
public class Config {} 

如果你想这样做明确:

package main.java.org.example; 
import main.java.org.example.GreetingCollector; 
import main.java.org.example.Greeting; 
import org.springframework.context.annotation.Configuration; 

@Configuration 
public class Config { 
    @Bean 
    public Greeting greeting() { 
     return new Greeting(); 
    } 

    @Bean 
    public GreetingCollector greetingCollector(Greeting greeting) { 
     return new GreetingCollector(greeting); 
    } 
} 

如果你想运行它只是为了看看它是如何工作的:

import main.java.org.example.Config; 
import main.java.org.example.GreetingCollector; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 

public class AppContext { 
    public static void main(String args[]) { 
     System.out.println("Configuring application context..."); 
     ApplicationContext context = new AnnotationConfigApplicationContext(Config.class); 
     GreetingCollector collector = (GreetingCollector) context.getBean("greetingCollector"); 
     System.out.println(collector.getGreeting()); 
    } 
} 

当然,Spring的web应用会有点不同,但基本的注​​入思路是一样的。首先,您需要声明bean(通过使用@Bean@Component或任何其他注释:有关差异,请参阅herehere)。你用@Autowired注解一个setter或构造函数(或者甚至是一个字段),指定参数(不一定需要具体类 - 接口,抽象类也可以),将它们分配到适当的字段。创建一个需要处理bean实例化的配置类。您不需要将组件放在与config类相同的文件夹中,因为您始终可以指定在哪里查找组件。最后,如果你想要一个更细粒度的控件,你总是可以在配置类中明确声明bean(所谓的JavaConfig,而基于@ComponentScan的配置有时可能被称为autoconfig)。这应该足以让你开始,并给你词汇搜索更先进的东西。

当然,使用Spring Boot时,所有东西都会更加抽象/更快。

1

然而,有人告诉我,XML已被废弃,所有新的应用,Java应该配置

XML不会被弃用做,但注释是有它让生活变得简单。那么为什么要使用普通的旧XML。记住“Convention over Configuration”

我应该在哪里声明bean,我该如何做?

我想你应该搜索谷歌的@Component,@Configuration,@Bean等注释。阅读他们你会明白

这是我见过的例子之一,但它是用xml实现的。

您会发现很多使用XML实现的示例,因为XML在Spring早期被广泛使用。 现在随着Spring 4和Spring Boot的问世。大多数开发人员不大规模地使用XML。

相关问题