Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you inject a bean into a @Controller class

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 @Controller and @RequestMapping and 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 @Controller annotation.

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?

like image 574
GriffeyDog Avatar asked Sep 17 '10 13:09

GriffeyDog


People also ask

How do you inject specific beans in a Spring boot?

In Spring Boot, we can use Spring Framework to define our beans and their dependency injection. The @ComponentScan annotation is used to find beans and the corresponding injected with @Autowired annotation. If you followed the Spring Boot typical layout, no need to specify any arguments for @ComponentScan annotation.

Does @controller create a bean?

Because @Controller is a specialization of Spring's @Component Stereotype annotation, the class will automatically be detected by the Spring container as part of the container's component scanning process, creating a bean definition and allowing instances to be dependency injected like any other Spring-managed ...

Can we use @bean annotation at class level?

@Willa yes, @Bean can be used in inside a class annotiated with @Component .

How many ways we can inject bean in Spring?

As per Java Annotation Configuration, Dependency Injection can be performed in three different ways.


1 Answers

@Autowired
private YourService yourServiceBean;

(you can also use @Inject)

Of course, YourService has to be declared as a bean - either in applicationContext.xml, or by annotations (@Service for example)

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

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

(For that you will need a PropertyPlaceholderConfigurer)

like image 158
Bozho Avatar answered Oct 22 '22 02:10

Bozho