欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Could not autowire. No beans of 'UserDAO' type found. more...

程序员文章站 2024-03-14 08:38:40
...

intellig idea 使用@Resource或者@Autowire报错,出现红色波浪线;在确定不是代码出错的问题下,

可以隐藏这个提示

报错提示:
Could not autowire. No beans of 'UserDAO' type found。(Ctrl+F1)  Checks autowiring problems in a bean class.

@Mapper    //使用Mapper注解方式,开启扫描
public interface UserDAO {

    String TABLE_NAME = "user";

    String INSERT_FIELDS = " name, password, salt, head_url ";

    String SELECT_FIELDS = " id, name, password, salt, head_url ";

    @Insert({
            "insert into ", TABLE_NAME, "(", INSERT_FIELDS,
            ") Values (#{name}, #{password}, #{salt}, #{headUrl})"
    })
    int addUser(User user);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where id=#{id}"})
    User selectById(int id);

    @Update({"update ", TABLE_NAME, " set password = #{password} where id=#{id}"})
    void updatePassword(User user /*set password= #{password} where id=#{id}*/);

    @Delete({"delete from ", TABLE_NAME, " where id = #{id}"})
    void deleteById(int id);
}
@Mapper
public interface NewsDAO {

    String TABLE_NAME = "news";

    String INSERT_FIELDS = " title, link, image, like_count, comment_count,created_date,user_id ";

    String SELECT_FIELDS = " id, " + INSERT_FIELDS;

    @Insert({
            "insert into ", TABLE_NAME, "(", INSERT_FIELDS,
            ") Values (#{title},#{link},#{image},#{likeCount}, #{commentCount},#{createdDate},#{userId})"
    })
    int addNews(News news);

    @Select({"select ", SELECT_FIELDS, " from ", TABLE_NAME, " where id=#{id}"})
    News selectById(int id);


    List<News> selectByUserIdAndOffset(@Param("userId") int userId, @Param("offset") int offset,
                                       @Param("limit") int limit);
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ToutiaoApplication.class)
//@WebAppConfiguration  这行会修改默认的启动路径需要注释掉
@Sql({"/init-schema.sql"})
public class InitDatabaseTests {

	@Autowired    //报错Could not autowire. No beans of 'UserDAO' type found. more...
	NewsDAO newsDAO;

	@Autowired
	UserDAO userDAO;
        ...
}

解决办法:

 Settings - Editor - Inspections - Spring - Spring Core - Code - Autowiring for Bean Class 勾去掉
    Could not autowire. No beans of 'UserDAO' type found. more...