@spring中的自动连线注释
我在spring方面相对较新,在理解spring mvc的基本原理方面存在问题 我的控制器@spring中的自动连线注释,spring,annotations,Spring,Annotations,我在spring方面相对较新,在理解spring mvc的基本原理方面存在问题 我的控制器 @Controller public class HomeController { private ContactManager contactManager; @Autowired public void setContactManager(ContactManager contactManager) { this.contactManager = contactManager; }
@Controller
public class HomeController {
private ContactManager contactManager;
@Autowired
public void setContactManager(ContactManager contactManager) {
this.contactManager = contactManager;
}
@RequestMapping(value="/")
public ModelAndView listContact(ModelAndView model) throws IOException {
List<Contact> listContact = contactManager.list();
model.addObject("listContact", listContact);
model.setViewName("home");
return model;
}
...
联系人管理器
public interface ContactManager {
public void saveOrUpdate(Contact contact);
public void delete(int contactId);
public Contact get(int contactId);
public List<Contact> list();
}
当然,问题在于@Autowired注释。我怎样才能修好它?删除@Autowired注释时,我遇到另一个错误:HomeController中的NullPointerException manager。将@Autowired注释移动到ContactManager的声明中
您不需要设置器,请将其删除。您的控制器应该是
@Controller
public class HomeController {
@Autowired
@Qualifier("managmentService")
private ContactManager contactManager;
@RequestMapping(value="/")
public ModelAndView listContact(ModelAndView model) throws IOException {
List<Contact> listContact = contactManager.list();
model.addObject("listContact", listContact);
model.setViewName("home");
return model;
}
...
在您的配置中,还要添加用于能够使用@Autowired注释的注释
一个例子
我删除了setter并更改为:@Autowired private ContactManager ContactManager;你认为这会有什么不同的效果?好的,我引入了一些变化,我得到了与之相关的错误,比如:错误[org.springframework.web.context.ContextLoader]主上下文初始化失败org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:来自ServletContext资源[/WEB-INF/spring/root Context.xml]的xml文档中的第21行无效;嵌套异常为org.xml.sax.saxpasseeption;行号:21;栏目号:34;cvc complex type.2.4.c:匹配的通配符是严格的,但是找不到元素“context:annotation config”的声明。您添加了元素context的正确名称空间吗?@volly如果一切正常,请告诉我,以便帮助您更好地关闭此主题。它有效;谢谢,现在我需要理解为什么它能与代码中的这些更改一起工作……不需要@Qualifier。
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="sec"/>
</bean>
<bean id="managmentService" class="spring.contact.impl.model.ContactManagerImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of
type [spring.contact.api.ContactManager] found for dependency: expected at least 1
bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
@Controller
public class HomeController {
@Autowired
@Qualifier("managmentService")
private ContactManager contactManager;
@RequestMapping(value="/")
public ModelAndView listContact(ModelAndView model) throws IOException {
List<Contact> listContact = contactManager.list();
model.addObject("listContact", listContact);
model.setViewName("home");
return model;
}
...
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="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">
<context:annotation-config/>
<!-- bean definitions go here -->
</beans>