我对spring和java很新。我一直在使用springsource.org来尝试通过创建spring 3 MVC Web应用程序来工作。我让它工作,我可以获取信息并与控制器和管理器一起显示。
现在我正在研究编辑信息并将其保存回数据库的功能。我在我的服务类中的保存功能中添加了@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);
}
}
修剪了我的Dao课程版本:
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" />
答案 0 :(得分:1)
看起来autowire注释感到困惑,但我无法确定。
您在EventsController中声明的依赖关系是对具体EventsManager类的依赖,而不是IEventsManager接口,这是更好的做法,即代码到接口。 Spring使用一些智能来自动连接bean,例如按类型自动装配和按名称自动装配。例如(我正在简化)spring将检查bean容器中是否匹配的类型或匹配的名称。我会假设按类型自动装配使用具体类型,但可能会有一些特性在这里解雇。
我建议:
此外,看起来你要两次指定一些豆子。使用@Service注释并对该包执行组件扫描时,无需在上下文文件中再次指定它。所以你的“EventsManager”bean已经定义了两次,但我不确定这是否会导致你的依赖性问题。