我有一个简单的应用程序,其中包含 xml 配置、1 个 spring session bean、 Controller 。使用注释一切都可以正常工作,但看起来 spring 无法看到 xml 配置,因为它找不到 Person bean?!
问题是我如何仅通过 xml Autowiring bean?
异常消息:
No qualifying bean of type 'com.spring_beans_scope.beans.Person' available: expected at least 1 bean which qualifies as autowire candidate
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="id1" class="com.spring_beans_scope.beans.WelcomeBean" scope="prototype">
<!--<property name="message" value="Welcome to spring" />-->
</bean>
<bean id="person" class="com.spring_beans_scope.beans.Person" scope="session">
<property name="name" value="Raj" />
<aop:scoped-proxy proxy-target-class="true" />
</bean>
<context:component-scan base-package="com.spring_beans_scope" />
<context:annotation-config />
</beans>
bean
//@Service("person")
//@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class Person {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Controller 的头部
@Controller
public class HelloController {
@Autowired
private Person person;
最佳答案
此答案基于您的评论,您想知道如何在不使用注释的情况下做到这一点。
无需注释即可完成。您需要在 bean 声明中使用 autowire 属性。
autowire="byName"
这可能有点棘手,因为 @Controller
注释不是从 xml 配置的,而是 this堆栈溢出帖子有助于解释如何配置 Controller 来执行此操作。
这个tutorial帮助解释直接从上下文文件 Autowiring 的不同方法。
关于java - 无法通过 XML 配置 Autowiring Spring session bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49722953/