如何在bean作用域上解决这个基本的spring错误

时间:2018-06-29 19:09:35

标签: spring scope javabeans

我面临的错误。 警告:在上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.BeanCreationException:在类路径资源[beanscope-applicationContext2.xml]中定义名称为“ mtapp”的bean创建错误:无法解析匹配的构造函数(提示:为简单参数指定索引/类型/名称参数以避免类型歧义) 线程“主”中的异常org.springframework.beans.factory.BeanCreationException:创建在类路径资源[beanscope-applicationContext2.xml]中定义的名称为“ mtapp”的bean时出错:无法解析匹配的构造函数(提示:指定索引/类型/简单参数的名称参数以避免类型歧义)

下面是代码

<?xml version="1.0" encoding="UTF-8"?>
<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"
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">

<bean id="myFortuneService"
    class="com.adi.ida.HappyFortuneService"></bean>
<!--  Baseballcoach mtapp = new Baseballcoach()-->


<!-- Define your beans here -->
<bean id="mtapp"
    class="com.adi.ida.Trackcoach">
    <constructor-arg ref="myFortuneService"/>
</bean>

主类(springfactory)

package com.adi.ida;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class beanscopedemo {

public static void main(String[] args) {

    ClassPathXmlApplicationContext context = new 
    ClassPathXmlApplicationContext("beanscope-applicationContext2.xml");
    coachInterface theCoach = context.getBean("mtapp",coachInterface.class);
    coachInterface alphaCoach = context.getBean("mtapp",coachInterface.class);
    boolean result =(theCoach == alphaCoach);
    System.out.println("pointing to same object" + result);
    System.out.println(theCoach);
    System.out.println(alphaCoach);
    context.close();

}

}

TrackCoach

公共类Trackcoach实现了coachInterface {

private FortuneServiceInterface fortuneservice;

public Trackcoach(FortuneServiceInterface theFS) {
    fortuneservice = theFS;
}
@Override
public String getdailypractice() {
    return "running 5k";
}

@Override
public String getDailyFortune() {
    return fortuneservice.foturneService();
}

}

HappyFortuneService

公共类HappyFortuneService实现FortuneServiceInterface {

@Override
public String foturneService() {
    return "Today might be lucky day";
}

}

0 个答案:

没有答案