2011-06-16 54 views
1

我对spring和java很新。我一直主要使用springsource.org来尝试通过创建Spring 3 MVC Web应用程序来工作。我有它的工作,我可以获取信息,并与控制器和经理显示。Spring 3 MVC:@Transactional添加到Service类,现在变得异常

现在我正在研究编辑信息并将其保存回数据库的功能。我将@Transactional注释添加到我的服务类中的保存功能中,现在我正在接收UnsatisfiedDependencyException。作为新手,我不确定如何清除它。我为我的EventsManager添加了一个bean,但是我不认为这是必要的,因为Annotations假定不需要大量的xml配置文件。我不确定错误在哪里,所以我发布了我的源代码。感谢您花时间帮助我解决问题。

完全例外:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'eventsController' defined in file [$Path\gov\mt\dphhs\epts\web\EventsController.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [gov.mt.dphhs.epts.services.EventsManager]: : No matching bean of type [gov.mt.dphhs.epts.services.EventsManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [gov.mt.dphhs.epts.services.EventsManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 

控制器类:

@Controller 
public class EventsController { 

    private EventsManager eventsManager; 

    @Autowired 
    public EventsController(EventsManager eventsManager){ 
     this.eventsManager = eventsManager; 
    } 

    @RequestMapping("/events/events") 
    public String displayEvents(ModelMap map){ 

     map.addAttribute("allEvents", eventsManager.getAllEvents()); 

     int totalEvents = eventsManager.getAllEvents().size(); 
     map.addAttribute("totalEvents",totalEvents); 

     map.addAttribute("eventsTitle" , totalEvents + " Events"); 

     return "events"; 
    } 

    @RequestMapping("/events/eventForm") 
    public String editEvents(@RequestParam("id") Long eventId, ModelMap map){ 

     Events event = eventsManager.getEventById(eventId); 
     map.addAttribute("pageTitle" , "Edit Event"); 
     map.addAttribute("event",event); 

     return "eventForm"; 
    } 

    @RequestMapping("/events/newEvent") 
    public String newEvent(ModelMap map){ 

     Events event = new Events(); 
     map.addAttribute("pageTitle" , "New Event"); 
     map.addAttribute("event",event); 

     return "eventForm"; 
    } 

    @RequestMapping("/events/submitEvent") 
    public String handleEventForm(@ModelAttribute("event") 
      Events event, BindingResult result){ 

     System.out.println("Event Name:" + event.getEventName() + " ID: " + event.getEvntId()); 

     if(event.getEvntId() == null) 
     { 
      eventsManager.save(event); 
     } 

     return "redirect:/events/events"; 
    } 
} 

服务类:

@Service("EventsManager") 
public class EventsManager implements IEventsManager { 


    private EventsDAO eventsDao; 

    @Autowired 
    public void EventsDAO(EventsDAO eventsDao) { 
     this.eventsDao = eventsDao; 
    } 

    public List<Events> getAllEvents() { 
     return eventsDao.findAll(); 
    } 

    public Events getEventById(Long id) { 

     return eventsDao.findById(id); 
    } 

    public void delete(Events event) { 
     // TODO Auto-generated method stub 

    } 

    @Transactional 
    public void save(Events event) { 
     eventsDao.save(event); 
    } 

    public Events update(Events event) { 

     return eventsDao.update(event); 
    } 

} 

下调吾道类的版本:

public class EventsDAO implements IEventsDAO { 

    @PersistenceContext 
    private EntityManager em; 

    protected final Log logger = LogFactory.getLog(EventsDAO.class); 

    public void save(Events entity) { 
     System.out.println("in Dao to save an event"); 
     logger.info("saving Events instance"); 
     try { 
      em.persist(entity); 
      logger.info("save successful"); 
     } catch (RuntimeException re) { 
      logger.error("save failed", re); 
      throw re; 
     } 
    } 

    public EntityManager getEm() { 
     return em; 
    } 

    public void setEm(EntityManager em) { 
     this.em = em; 
    } 

    public static IEventsDAO getFromApplicationContext(ApplicationContext ctx) { 
     return (IEventsDAO) ctx.getBean("EventsDAO"); 
    } 
} 

应用程序上下文:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:tx="http://www.springframework.org/schema/tx" 
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/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd"> 

<context:component-scan base-package="gov.mt.dphhs.epts" /> 

<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 

<bean id="entityManagerFactory" 
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> 
    <property name="persistenceUnitName" value="EPTSTest3Pu" /> 
</bean> 

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory" /> 
</bean> 

<tx:annotation-driven transaction-manager="transactionManager" /> 
<context:annotation-config /> 

<bean id="EventsManager" class="gov.mt.dphhs.epts.services.EventsManager" /> 

<bean id="SpecialGuestsDAO" class="gov.mt.dphhs.epts.repository.SpecialGuestsDAO" /> 

<bean id="GroupMembersDAO" class="gov.mt.dphhs.epts.repository.GroupMembersDAO" /> 

<bean id="GroupsDAO" class="gov.mt.dphhs.epts.repository.GroupsDAO" /> 

<bean id="EventNotificationsDAO" class="gov.mt.dphhs.epts.repository.EventNotificationsDAO" /> 

<bean id="EventListingsDAO" class="gov.mt.dphhs.epts.repository.EventListingsDAO" /> 

<bean id="EventGroupXrefDAO" class="gov.mt.dphhs.epts.repository.EventGroupXrefDAO" /> 

<bean id="EventsDAO" class="gov.mt.dphhs.epts.repository.EventsDAO" /> 

+1

看看这个问题。我想你可能有同样的问题http://stackoverflow.com/questions/2713033/autowire-strange-problem – Javi 2011-06-17 13:25:27

回答

1

它看起来像自动装配注解感到困惑,但我不能肯定。

您在EventsController中声明的依赖关系是对具体EventsManager类的依赖关系,而不是接口IEventsManager,这是更好的实践,即代码接口。 Spring使用一些智能来自动连接你的bean,比如按类型自动装配和按名称自动装配。例如(我简化了)spring将检查bean容器中匹配的类型或匹配的名称。我会认为按照类型使用具体类型的自动装配,但可能有一些特质在这里开火。

我会建议:

  • 在 控制器使用IEventsManager,并说明构造函数的参数
  • 是 类型依赖以小写开头字母命名你的春天豆,喜欢你的java场/变量命名约定(然后弹簧可以自动按名称连接,匹配“eventsManager”构造函数参数到“eventsManager”spring bean)

另外,它看起来像你指定一些你的bean两次。当您使用@Service注释并对该包进行组件扫描时,无需在上下文文件中再次指定它。所以你的“EventsManager”bean已经定义了两次,但我不确定这是否会导致你的依赖问题。

相关问题