Spring - 向注入的对象传递参数

3
我将尝试将一个Bean注入到控制器中,但是似乎Spring没有使用bean.xml文件。
以下是代码:
控制器:
@RestController
public class AppController {

  private final EventService eventService;
  private List<String> categories;

  public AppController(final EventService eventService) {
    this.eventService = eventService;
  }
}

注入对象的接口
public interface EventService {
   // some methods
}

它的实现

public class MyEventService {

  private final String baseURL;

  public MyEventService(String baseURL){
    this.baseURL = baseURL;
  }
}

如果我使用@Service为MyEventService添加注解,Spring将尝试将其注入到控制器中,但是会抱怨没有提供baseURL(没有可用的类型为'java.lang.String'的合格bean)。因此,我在src/main/resources下创建了一个bean.xml文件。

<?xml version = "1.0" encoding = "UTF-8"?>

  <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:util="http://www.springframework.org/schema/util" 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-3.0.xsd">

    <bean id="eventService" class="xyz.MyEventService">
        <constructor-arg type="java.lang.String" value="fslkjgjbflgkj" />
    </bean>

    <bean id="categories" class="java.util.ArrayList">
        <constructor-arg>
            <list>
                <value>music</value>
                <value>comedy</value>
                <value>food</value>
            </list>
        </constructor-arg>
    </bean>
  </beans>

但是如果我从MyEventService中删除@Service注释,似乎不起作用,Spring会抱怨找不到名为eventService的bean。我是否漏掉了什么?
2个回答

2

Spring Boot 严重依赖于 Java 配置。

请查看 文档,了解如何声明 @Component、@Service 等。

在您的情况下:

@Service
public class MyEventService implements EventService {

  private final String baseURL;

  @Autowired
  public MyEventService(Environment env){
    this.baseURL = env.getProperty("baseURL");
  }
}

在你的 /src/main/resources/application.properties 文件中

baseURL=https://baseUrl

然后

@RestController
public class AppController {

  private final EventService eventService;

  @Autowired
  public AppController(final EventService eventService) {
    this.eventService = eventService;
  }
}

@Autowired指示Spring Boot查找您使用@Component、@Service、@Repository、@Controller等声明的组件。

为了注入类别,我建议您声明一个CategoriesService(带有@Service),该服务从配置文件中获取类别或仅在CategoriesService类中硬编码它们(用于原型制作)。


1
从Spring Framework 4.3开始,如果目标bean一开始就定义了一个构造函数,则不再需要在该构造函数上使用Autowired注释。但是,如果有多个构造函数可用且没有主/默认构造函数,则必须在其中至少一个构造函数上注释Autowired,以指示容器使用哪个构造函数。有关详细信息,请参见构造函数解析中的讨论。https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans-autowired-annotation - Siya Sosibo

1

你尝试的内容有两个问题

  1. The reason your xml bean definition is not picked up is because you didn't injected the service in you controller,since you are using annotation for your controller you need to tell you controller how the service needs to be injected, to fix this just autowire your service

    @RestController
    public class AppController {
    
    @Autowired
    private final EventService eventService;
    
    
    }
    
  2. When your are using annotation for construct-arg based spring bean you need to set value as well, like you are doing in your xml.

    public class MyEventService {
    
    private final String baseURL;
    
    public MyEventService(@Value("#{your value here")String baseURL){
         this.baseURL = baseURL;
     }
    }
    

我已经尝试使用Autowired,但Spring说它找不到eventService的bean。我(几乎)让它工作的唯一方法是用@Service注释MyEventService:在这种情况下,问题出现在baseURL上。在以前的项目中,我将所有bean定义在xml文件中,没有注释,它可以正常工作。 - algiogia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接