gpt4 book ai didi

java - 使用 NoSuchBeanDefinitionException Autowiring 独立应用程序

转载 作者:行者123 更新时间:2023-12-01 13:32:27 28 4
gpt4 key购买 nike

我有一个应用程序,但无法让它在 Eclipse 或其他地方运行。

这是我的类(class):

package fr.aaa;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component
public class Computation {

public Computation() {
}

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");
Computation computer = context.getBean(Computation.class);
}
}

和我的 applicationContext.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:security="http://www.springframework.org/schema/security" xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">

<import resource="classpath:spring/persistence.xml"/>

<context:annotation-config />
<context:component-scan base-package="fr.aaa.*, com.bbb.*"/>

<util:properties id="jdbcProps" location="jdbc.properties" />

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:configuration.properties</value>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>

我得到这个异常:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [fr.aaa.Computation] is defined: expected single bean but found 0:

我在这里遗漏了什么吗?

最佳答案

改变

fr.aaa.*

fr.aaa

对所有包执行相同的操作。

base-package 属性是

The comma-separated list of packages to scan for annotated components.

不涉及通配符。您必须使用包的特定名称。 Spring 将负责扫描子包。

关于java - 使用 NoSuchBeanDefinitionException Autowiring 独立应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21485914/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com