我对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" />
看看这个问题。我想你可能有同样的问题http://stackoverflow.com/questions/2713033/autowire-strange-problem – Javi 2011-06-17 13:25:27