简体   繁体   English

无法通过XML配置自动装配Spring Session bean

[英]Cannot autowire spring session bean by XML config

I have a simple app which is contains xml config, 1 spring session bean, controller. 我有一个简单的应用程序,其中包含xml配置,1个spring session bean,控制器。 Everything works fine with an annotations, but looks like spring cannot see xml config because it cannot find Person bean?! 一切都可以通过注释很好地工作,但是看起来spring无法看到xml config,因为它找不到Person bean ?!

The question is how can i autowire beans through xml only? 问题是我如何仅通过xml自动连接bean?

Exception message: 异常消息:

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;
    }
}

The head of the controller 控制器的负责人

@Controller
public class HelloController {

    @Autowired
    private Person person;

This answer is based on your comment that you want to know how to do it without the use of annotations. 该答案基于您想知道如何不使用批注的注释。

You can do it without annotations. 您可以不带批注。 You need to use the autowire attribute on your bean declaration. 您需要在bean声明上使用autowire属性。

  autowire="byName"

This may be a bit trickier as the @Controller annotation is not configured from xml but this stack overflow post helps explain how you can configure your controller to do so. 这可能有点棘手,因为未从xml配置@Controller批注,但是堆栈溢出文章有助于解释如何配置控制器。

This tutorial helps explain the different ways you can autowire from the context file directly. 教程有助于说明可以直接从上下文文件自动装配的不同方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM