[英]How do I use setter injection with java configuration in Spring?
我试图在SpringMVC Web应用程序中学习setter注入,我可以找到的所有示例,使用xml
显示。 但是,我被告知不推荐使用xml,并且所有新应用程序都应该使用java配置完成。 这是错的,我应该使用xml来配置我的应用程序吗?
我应该在哪里宣布豆子,我该怎么做?
这是我见过的一个例子,但它是用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>
我建议首先考虑普通的Spring配置,以了解基本的东西(如注入)是如何工作的。 如果你设法在Spring中获得它,那么Spring MVC / Spring Boot /中的进程将非常相似。 就个人而言,我发现尝试同时处理多个概念(查看解析器,不同的配置文件,视图,存储库,多个注释,多种配置方式等)非常令人沮丧,因此我倾向于从最简单的概念开始并构建我的一路攀升。 一旦您对注射的工作方式感到满意,您就可以轻松地将这些知识应用到其
至于java配置和注释,它们确实允许更快更清洁的开发。 XML非常冗长,难以维护并且非常容易出错,部分原因是在使用基于Java的配置时,IDE通常更有用。 也许这就是为什么你读到XML被弃用的原因。 我建议去java / auto配置而不是XML,除非你真的需要(或者对它感兴趣)。
现在谈谈如何做到这一点。 一个完整的(但最小的)工作Spring示例:
/* 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或任何其他注释:请参阅此处和此处的差异)。 您使用@Autowired注释setter或构造函数(甚至是字段),指定参数(不一定需要具体类 - 接口,抽象类也可以),将它们分配给适当的字段。 创建一个负责bean实例化的配置类。 您不需要将组件与config类放在同一文件夹中,因为您始终可以指定查找组件的位置。 最后,如果您想要更细粒度的控件,您始终可以在配置类中显式声明bean(所谓的JavaConfig,而基于@ComponentScan
的配置有时可能称为autoconfig)。 这应该足以让你开始并为你提供词汇来搜索更高级的东西。
当然,使用Spring Boot,一切都更加抽象/更快。
但是,我被告知不推荐使用xml,并且所有新应用程序都应该使用java配置完成
不推荐使用XML,但注释会使生活变得简单。 那么为什么要使用普通的旧XML。 记住“约定优于配置”
我应该在哪里宣布豆子,我该怎么做?
我认为你应该在谷歌搜索@Component,@ Configuration,@ Bean等注释。 阅读他们会理解的内容
这是我见过的一个例子,但它是用xml实现的。
您会发现许多使用XML实现的示例,因为XML在Spring的早期广泛使用。 现在随着Spring 4和Spring Boot的出现。 大多数开发人员不会大规模使用XML。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.