我遵循一个示例 go spring mvc + hibernate,但我遇到了很多问题,现在我有一些看起来像错误但我不知道的东西。我使用 STS 和 spring MVC 项目模板。
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Defines database lookup trough JNDI -->
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/paidos" resource-ref="true" />
<!-- Hibernate configuration -->
<util:properties id="hibernateProperties">
<beans:prop key="hibernate.dialect">
org.hibernate.dialect.MySQL5InnoDBDialect
</beans:prop>
<beans:prop key="hibernate.show_sql">false</beans:prop>
</util:properties>
<beans:bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
p:dataSource-ref="dataSource"
p:hibernateProperties-ref="hibernateProperties" >
<beans:property name="packagesToScan" value="com.carloscortina.paidosTest.domain" />
</beans:bean>
<beans:bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
<context:component-scan base-package="com.carloscortina.paidosTest.dao.hbn"/>
<context:component-scan base-package="com.carlosocortina.paidosTest.service" />
<tx:annotation-driven/>
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.carloscortina.paidosTest" />
<!-- Controlador para redirigir en caso de existo PersonalRegistration -->
<view-controller
path="personal/RegistrySubmit"
view-name="personal/RegistrySubmit" />
</beans:beans>
在
<tx:annotation-driven>
<context:component-scan base-package="com.carloscortina.paidosTest" />
我在行号处得到了这个红色标记:
advises com.carloscortina.paidosTest.service.PersonalServiceImp.registerPersonal(Personal,
Errors)
advised by
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(org.aopalliance.intercept.MethodInvocation)
我不知道这是否会导致下一个错误:
ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personalController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.carloscortina.paidosTest.service.PersonalService com.carloscortina.paidosTest.PersonalController.personalService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.carloscortina.paidosTest.service.PersonalService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.inject.Inject()}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:599)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:518)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:459)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5123)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5407)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3926)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
at java.lang.Thread.run(Thread.java:722)
个人服务.java
package com.carloscortina.paidosTest.service;
import org.springframework.validation.Errors;
import com.carloscortina.paidosTest.domain.Personal;
public interface PersonalService {
boolean registerPersonal(Personal staff,Errors errors);
}
PersonalServiceImpl.java
package com.carloscortina.paidosTest.service;
import javax.inject.Inject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.Errors;
import com.carloscortina.paidosTest.dao.PersonalDao;
import com.carloscortina.paidosTest.domain.Personal;;
@Service
@Transactional(readOnly=true)
public class PersonalServiceImp {
@Inject private PersonalDao personalDao;
@Transactional(readOnly=false)
public boolean registerPersonal(Personal staff,Errors errors){
boolean valid = !errors.hasErrors();
if(valid){ personalDao.create(staff);}
return valid;
}
}
我在定义方法的行也有一个标记
advised by
org.springframework.transaction.interceptor.TransactionInterceptor.invoke(org.aopalliance.intercept.MethodInvocation)
个人控制器.java
package com.carloscortina.paidosTest;
import javax.inject.Inject;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.carloscortina.paidosTest.domain.Personal;
import com.carloscortina.paidosTest.model.PersonalRegistryForm;
import com.carloscortina.paidosTest.service.PersonalService;
@Controller
@RequestMapping(value="/personal")
public class PersonalController {
private static final Logger logger = LoggerFactory.getLogger(PersonalController.class);
private final static String regForm="personal/RegistryForm";
private final static String regSubmit="redirect:RegistrySubmit";
@Inject private PersonalService personalService;
@RequestMapping(value="new",method=RequestMethod.GET)
public String getPersonalRegistrationForm(Model model){
logger.info("desplegando forma");
PersonalRegistryForm form = new PersonalRegistryForm() ;
model.addAttribute("personalRegForm",form);
return regForm;
}
@RequestMapping(value="",method=RequestMethod.POST)
public String submitPersonalRegistrationForm(@ModelAttribute("personalRegForm") @Valid PersonalRegistryForm form,
BindingResult result){
convertPasswordError(result);
personalService.registerPersonal(toPersonal(form), result);
return (result.hasErrors() ? regForm: regSubmit);
}
@InitBinder
public void initBinder(WebDataBinder binder){
binder.setAllowedFields(new String[]{
"username","password","confirmPassword","pNombre",
"sNombre","apellidoP","apellidoM","email","telefono",
"telCel","categoria","acceptTerms"});
}
private static void convertPasswordError(BindingResult result){
for(ObjectError error: result.getAllErrors()){
String msg = error.getDefaultMessage();
if("personal.password.mismatch.message".equals(msg)){
if(!result.hasFieldErrors("password")){
result.rejectValue("password", "error.mismatch");
break;
}
}
}
}
private static Personal toPersonal(PersonalRegistryForm form){
Personal staff = new Personal();
staff.setpNombre(form.getpNombre());
staff.setsNombre(form.getsNombre());
staff.setApellidoP(form.getApellidoP());
staff.setApellidoM(form.getApellidoM());
staff.setTelefono(form.getTelefono());
staff.setTelCel(form.getTelCel());
staff.setCategoria(form.getCategoria());
return staff;
}
}
我不知道这是否足够的信息。我希望有人能给我建议,谢谢提前。