我尝试从#34; Spring in action 3"中做例子。但是有这个问题并且不明白为什么。 我有下一堂课: 我的实体:
package core;
import javax.persistence.*;
import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="users")
public class SplitterImpl{
@Id
@GeneratedValue(generator="increment")
@GenericGenerator(name="increment", strategy="increment")
@Column(name="id")
private int id;
@Column(name="name")
private String name;
@Column(name="password")
private String password;
@Column(name="fullname")
private String fullname;
@Column(name="email")
private String email;
@Column(name="updatebyemail")
private String updatebyemail;
//****************
//public Setters/getters
//****************
}
我的道:
package core;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository
public class HibernateSplitterDao implements SplitterDAO {
@Autowired
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public void addSplitter(SplitterImpl splitter) {
sessionFactory.getCurrentSession().persist(splitter);
}
}
我的主要课程:
package core;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static String getBean(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("core\\spring.xml");
return ctx.getBean(HibernateSplitterDao.class).toString();
}
}
在xml spring配置中我写了这个:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
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.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="core"></context:component-scan>
<jee:jndi-lookup id="dataSource"
jndi-name="jdbc/splitter"/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="Splitter"
class="core.SplitterImpl">
<property name="name" value="TGSH"/>
<property name="password" value="ua"/>
<property name="fullname" value="Taras Shevchenko"/>
<property name="email" value="tgsh@gmail.com"/>
<property name="updatebyemail" value="false" />
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="core"/>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
</bean>
<bean id="dao" class="core.HibernateSplitterDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
当我尝试获得另一种豆时,一切都还可以。但是当我尝试使用HibernateSplitterDao bean时,我有这样的问题:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [core.HibernateSplitterDao] is defined
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:318)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:985)
at core.Main.getBean(Main.java:10)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:68)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
P.S。抱歉我的英文。
答案 0 :(得分:0)
您正在尝试获取SplitterDAO
类型的bean,但您还没有定义任何类。我在拆分器中看到的只有core.SplitterImpl
。尝试创建core.SplitterImpl
类型的bean。
答案 1 :(得分:0)
<context:annotation-config>
用于在已注册的bean上启用注释。(例如@Autowired
)
<context:component-scan>
更进一步,扫描以在上下文中注册bean。 (例如@Repository
)
看起来SplitterDao没有注册(不是在xml中或不使用组件扫描)
答案 2 :(得分:0)
在Spring xml中,请参阅Impl类而不是Interface。
<bean id="dao" class="core.HibernateSplitterDaoImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
清理项目并再次运行。