gpt4 book ai didi

java - 为什么 Eclipse 向我显示此错误并且找不到类?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:28:38 27 4
gpt4 key购买 nike

我使用 maven spring hibernate 和 eclipse 进行开发,我尝试将数据插入到我的 postgres 基础数据中,但出现此错误

log4j:WARN No appenders could be found for logger  (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'a' is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:568)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1108)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:278)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1117)
at com.App.main(App.java:19)

我不明白这个错误,因为每件事都是示例和逻辑,这是我的配置文件 xml

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">



<context:component-scan base-package="java.com" />
<context:annotation-config />

<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/webservices" />
<property name="username" value="postgres" />
<property name="password" value="1234" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="java.com" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>

这是我的服务

@Service("a")
public class Ws_security_services implements Interface_ws_security_services {

@Autowired
private Interface_ws_security_DAO interface_ws_security_DAO;



@Transactional
public void addws(Ws_security ws_security) {

interface_ws_security_DAO.addws(ws_security);
}

这是我的主课

package com;

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

import com.entity.Ws_security;
import com.services.Interface_ws_security_services;

public class App {




public static void main(String[] args) {

@SuppressWarnings("resource")
ApplicationContext ctx1 = new ClassPathXmlApplicationContext("spring_hibernate.xml");

Interface_ws_security_services service=(Interface_ws_security_services) ctx1.getBean("a");

Ws_security ess=new Ws_security();
ess.setIDws("ess1");
ess.setLogin("ess2");
service.addws(ess);
System.out.println("Done");


}

}

请问我的问题是什么,如果有人找到解决方案请写下来

最佳答案

你配置了你的上下文

<context:component-scan base-package="java.com" />

类在com包中:

package com;

修复上下文配置,并根据约定选择更好的包名称:

package com.mydomainname.myapplication

关于java - 为什么 Eclipse 向我显示此错误并且找不到类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17851696/

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