簡體   English   中英

無法通過XML配置自動裝配Spring Session bean

[英]Cannot autowire spring session bean by XML config

我有一個簡單的應用程序,其中包含xml配置,1個spring session bean,控制器。 一切都可以通過注釋很好地工作,但是看起來spring無法看到xml config,因為它找不到Person bean ?!

問題是我如何僅通過xml自動連接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>

豆角,扁豆

//@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
public class HelloController {

    @Autowired
    private Person person;

該答案基於您想知道如何不使用批注的注釋。

您可以不帶批注。 您需要在bean聲明上使用autowire屬性。

  autowire="byName"

這可能有點棘手,因為未從xml配置@Controller批注,但是堆棧溢出文章有助於解釋如何配置控制器。

教程有助於說明可以直接從上下文文件自動裝配的不同方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM