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