我的一项服务中的自动装配注释存在一些问题。我花了很多时间来寻找解决方案,但我不知道我做错了什么。我的应用看起来像这样。
这是我的控制器:
package control.peso.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import control.peso.data.ResumenMedicionPeso;
import control.peso.service.HomeService;
@Controller
public class HomeController {
@Autowired
private HomeService homeService; //NOPMD
@RequestMapping(value = "json/resumen_mediciones.action")
@ResponseBody
public final ResumenMedicionPeso
dataJsonPeso(final HttpServletRequest req) {
final ResumenMedicionPeso peso = homeService.getResumenMediciones();
return peso;
}
}
我的服务层:
package control.peso.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import control.peso.dao.PesoDAO;
import control.peso.data.ResumenMedicionPeso;
@Service
@Transactional(readOnly = true)
public class HomeService {
@Autowired
private PesoDAO pesoDAO; //NOPMD
public final ResumenMedicionPeso getResumenMediciones() {
final ResumenMedicionPeso resumMedicionPeso = new ResumenMedicionPeso();
resumMedicionPeso.setMaxPeso(pesoDAO.getMaxPeso());
resumMedicionPeso.setMinPeso(pesoDAO.getMinPeso());
resumMedicionPeso.setMaxGrasa(pesoDAO.getMaxGrasa());
resumMedicionPeso.setMinGrasa(pesoDAO.getMinGrasa());
resumMedicionPeso.setMaxPorcenGrasa(pesoDAO.getMaxPorcenGrasa());
resumMedicionPeso.setMinPorcenGrasa(pesoDAO.getMinPorcenGrasa());
resumMedicionPeso.setMaxMusculo(pesoDAO.getMaxMusculo());
resumMedicionPeso.setMinMusculo(pesoDAO.getMinMusculo());
resumMedicionPeso.setMaxPorcenMusculo(pesoDAO.getMaxPorcenMusculo());
resumMedicionPeso.setMinPorcenMusculo(pesoDAO.getMinPorcenMusculo());
return resumMedicionPeso;
}
}
我的道:
package control.peso.dao;
import java.util.List;
import org.hibernate.SessionFactory;
import control.peso.model.MedicionPeso;
public class PesoDAO implements IPesoDAO {
private SessionFactory sessionFactory;
public final SessionFactory getSessionFactory() {
return sessionFactory;
}
public final void setSessionFactory(
final SessionFactory pSessionFactory) {
this.sessionFactory = pSessionFactory;
}
@Override
public final void addPeso(final MedicionPeso peso) {
getSessionFactory().getCurrentSession().save(peso); //NOPMD
}
@Override
public final void updatePeso(final MedicionPeso peso) {
getSessionFactory().getCurrentSession().update(peso); //NOPMD
}
@Override
public final void deletePeso(final Integer idPeso) {
getSessionFactory().getCurrentSession()
.delete(new MedicionPeso(idPeso));
}
@Override
public final MedicionPeso getPesoById(final Integer idPeso) {
@SuppressWarnings("unchecked") //NOPMD
final List<MedicionPeso> list = getSessionFactory() // NOPMD
.getCurrentSession()
.createQuery("from MedicionPeso where idPeso = ?")
.setParameter(0, idPeso).list();
return list.get(0); //NOPMD
}
@Override
public final List<MedicionPeso> getPesos() {
@SuppressWarnings("unchecked")
final List<MedicionPeso> list = getSessionFactory() //NOPMD
.getCurrentSession()
.createQuery("from MedicionPeso medicionPeso "
+ "order by medicionPeso.fechaMedicion desc")
.list();
return list;
}
public final Float getMaxPeso() {
@SuppressWarnings("unchecked")
final List<Float> list = getSessionFactory() //NOPMD
.getCurrentSession()
.createQuery("select max(peso) from MedicionPeso")
.list();
return (Float) list.get(0); //NOPMD
}
public final Float getMinPeso() {
@SuppressWarnings("unchecked")
final List<Float> list = getSessionFactory() //NOPMD
.getCurrentSession()
.createQuery("select min(peso) from MedicionPeso")
.list();
return (Float) list.get(0); //NOPMD
}
public final Float getMaxGrasa() {
@SuppressWarnings("unchecked")
final List<Float> list = getSessionFactory() //NOPMD
.getCurrentSession()
.createQuery("select max(pesoGrasa) from MedicionPeso")
.list();
return (Float) list.get(0); //NOPMD
}
public final Float getMinGrasa() {
@SuppressWarnings("unchecked")
final List<Float> list = getSessionFactory() //NOPMD
.getCurrentSession()
.createQuery("select min(pesoGrasa) from MedicionPeso")
.list();
return (Float) list.get(0); //NOPMD
}
public final Float getMaxPorcenGrasa() {
@SuppressWarnings("unchecked")
final List<Float> list = getSessionFactory() //NOPMD
.getCurrentSession()
.createQuery("select max(pesoGrasa) from MedicionPeso")
.list();
return (Float) list.get(0); //NOPMD
}
public final Float getMinPorcenGrasa() {
@SuppressWarnings("unchecked")
final List<Float> list = getSessionFactory() //NOPMD
.getCurrentSession()
.createQuery("select min(porcentajeGrasa) from MedicionPeso")
.list();
return (Float) list.get(0); //NOPMD
}
/**
* Recupera la medicion de musculo con valor maximo.
* @return El valor maximo de las mediciones de musculo.
*/
public final Float getMaxMusculo() {
@SuppressWarnings("unchecked")
final List<Float> list = getSessionFactory() //NOPMD
.getCurrentSession()
.createQuery("select max(porcentajeGrasa) from MedicionPeso")
.list();
return (Float) list.get(0); //NOPMD
}
public final Float getMinMusculo() {
@SuppressWarnings("unchecked")
final List<Float> list = getSessionFactory() //NOPMD
.getCurrentSession()
.createQuery("select min(pesoMusculo) from MedicionPeso")
.list();
return (Float) list.get(0); //NOPMD
}
public final Float getMaxPorcenMusculo() {
@SuppressWarnings("unchecked")
final List<Float> list = getSessionFactory() //NOPMD
.getCurrentSession()
.createQuery("select max(porcentajeMusculo) from MedicionPeso")
.list();
return (Float) list.get(0); //NOPMD
}
public final Float getMinPorcenMusculo() {
@SuppressWarnings("unchecked")
final List<Float> list = getSessionFactory() //NOPMD
.getCurrentSession()
.createQuery("select max(porcentajeMusculo) from MedicionPeso")
.list();
return (Float) list.get(0); //NOPMD
}
}
我的dao接口:
package control.peso.dao;
import java.util.List;
import control.peso.model.MedicionPeso;
public interface IPesoDAO {
void addPeso(MedicionPeso peso);
void updatePeso(MedicionPeso peso);
void deletePeso(Integer idPeso);
MedicionPeso getPesoById(Integer idPeso);
List<MedicionPeso> getPesos();
}
这是我的调度程序-servlet.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:mvc="http://www.springframework.org/schema/mvc"
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.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!--Routes -->
<mvc:view-controller path="/" view-name="home"/>
<mvc:view-controller path="/home" view-name="home"/>
<mvc:view-controller path="/medicion" view-name="medicion_peso"/>
<!-- Scans the classpath of this application for @Components to deploy as beans -->
<context:component-scan base-package="control.peso" />
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- misc -->
<!--
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
-->
<!-- Tiles Resolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass">
<value>
org.springframework.web.servlet.view.tiles2.TilesView
</value>
</property>
</bean>
<bean id="tilesConfigurer"
class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles.xml</value>
</list>
</property>
</bean>
<!-- Application Message Bundle -->
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<!-- JSON Objets Definition -->
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<!-- Beans Declaration -->
<bean id="MedicionPeso" class="control.peso.model.MedicionPeso" />
<!-- User DAO Declaration -->
<bean id="PesoDAO" class="control.peso.dao.PesoDAO">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
<!-- Data Source Declaration -->
<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/juan" />
<property name="user" value="root" />
<property name="password" value="" />
<property name="maxPoolSize" value="10" />
<property name="maxStatements" value="0" />
<property name="minPoolSize" value="5" />
</bean>
<!-- Session Factory Declaration -->
<bean id="SessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="annotatedClasses">
<list>
<value>control.peso.model.MedicionPeso</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="txManager" />
<!-- Transaction Manager is defined -->
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="SessionFactory" />
</bean>
</beans>
因此,当我启动我的网络应用程序时,我的控制器自动连接服务,但我的服务中的 DAO 对象具有空值(未正确注入)。
有任何想法吗?
这很奇怪,因为从另一个服务中,正确地注入了相同的 DAO。
谢谢