Java 你如何将 bean 注入 @Controller 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3735762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:06:02  来源:igfitidea点击:

how do you inject a bean into a @Controller class

javaspringspring-mvc

提问by GriffeyDog

I'm somewhat new to Spring (using 3.0), so I'm hoping there is a simple answer. If I have a controller that is annotated with @Controllerand @RequestMappingand I want to set a property via dependency injection, how do I go about doing that? The controller class doesn't have to appear in the Spring configuration file because it gets picked up automatically because of the @Controllerannotation.

我对 Spring(使用 3.0)有点陌生,所以我希望有一个简单的答案。如果我有一个用@Controllerand注释的控制器,@RequestMapping我想通过依赖注入设置一个属性,我该怎么做?控制器类不必出现在 Spring 配置文件中,因为它会因为@Controller注解而被自动选取。

Example Controller class:

示例控制器类:

package gov.wi.dnr.wh.web.spring;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class RehabHomeController {
  private String xxx;

  @RequestMapping(value="/rehab/home", method = RequestMethod.GET)
  public String get() {
    return "whdb.rehabhome";
  }

  public String getXxx() {
    return xxx;
  }

  public void setXxx(String xxx) {
    this.xxx = xxx;
  }
}

Spring configuration:

弹簧配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                            http://www.springframework.org/schema/context 
                      http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
  </bean>

  <context:component-scan base-package="gov.wi.dnr.wh.web.spring"/>
  <mvc:annotation-driven />

</beans>

This works as is, but I would like to inject the "xxx" property. How do I go about doing that?

这按原样工作,但我想注入“xxx”属性。我该怎么做?

采纳答案by Bozho

@Autowired
private YourService yourServiceBean;

(you can also use @Inject)

(你也可以使用@Inject

Of course, YourServicehas to be declared as a bean - either in applicationContext.xml, or by annotations (@Servicefor example)

当然,YourService必须声明为 bean - 无论是在 中applicationContext.xml,还是通过注释(@Service例如)

If you want to inject string properties, you can use the @Valueannotation:

如果要注入字符串属性,可以使用@Value注释:

@Value("${propName}")
private String str;

(For that you will need a PropertyPlaceholderConfigurer)

(为此,您将需要一个PropertyPlaceholderConfigurer