繁体   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