首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@Autowired春季注释

@Autowired春季注释
EN

Stack Overflow用户
提问于 2014-09-06 18:06:56
回答 2查看 305关注 0票数 0

我在春季相对较新,我在理解spring的基本原理方面有问题。

我的控制器

代码语言:javascript
复制
 @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;
 }
 ...

联系ManagerImpl及接口方法的实现

代码语言:javascript
复制
public class ContactManagerImpl implements ContactManager {
private DataSource dataSource;
private JdbcTemplate jdbcTemplate;

public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
    this.jdbcTemplate = new JdbcTemplate(dataSource);
}
  ...

ContactManager

代码语言:javascript
复制
public interface ContactManager {
public void saveOrUpdate(Contact contact);
public void delete(int contactId);
public Contact get(int contactId);
public List<Contact> list();
 }

和root-context.xml

代码语言:javascript
复制
<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>

我的问题是我犯了错误:

代码语言:javascript
复制
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: {}

当然,问题在于@Autowired注解。我怎么才能修好它?当我删除@Autowired注释时,我得到了另一个错误: NullPointerException (HomeController中的管理器)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-06 19:16:25

你的财务总监应该是..。

代码语言:javascript
复制
@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;
     }
     ...

在您的配置中,还要添加<context:annotation-config />,以便能够使用@Autowired注释。

<context:annotation-config />的一个例子

代码语言:javascript
复制
<?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>
票数 0
EN

Stack Overflow用户

发布于 2014-09-06 18:12:24

将@Autowired注释移动到ContactManager的声明中。

你不需要策划人,把它删掉。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25703257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档