java - 在Spring Boot

时间:2017-02-07 16:52:44

标签: java mysql spring hibernate spring-boot

所以我现在有一个API客户端类型类,我正在尝试连接到我的存储库,以便我可以将数据存储在MySQL数据库中。

我遇到的问题是API客户端类实例化了自身的新对象,因此自动装配无法正常工作。我已经四处寻找解决此问题的方法,并且我已经看到了几个选项,但我对如何将它们应用于我的问题感到困惑。

供参考,以下是一些相关文件的部分内容:

GeniusApiClient.java:

@Component
public final class GeniusApiClient {
    private final OkHttpClient client = new OkHttpClient();

    @Autowired
    private ArtistDao artistDao;

    public static void main(String[] args) throws Exception {
        GeniusApiClient geniusApiClient = new GeniusApiClient();
        String artistId = (geniusApiClient.getArtistId("Ugly Duckling"));

        ArrayList<String> artistSongIds = geniusApiClient.getArtistSongIds(artistId);
        System.out.println(geniusApiClient.getAllSongAnnotations(artistSongIds, artistId));
    }

    public String getAllSongAnnotations(ArrayList<String> songIds, String artistId) {
        Artist artist = new Artist("test name for now", "string123", "223");
        artistDao.save(artist);
        return "finished";
    }

}

ArtistDao.java:

@Transactional
public interface ArtistDao extends CrudRepository<Artist, Long> {
    public Artist findByGeniusId(String geniusId);
}

ArtistController.java:

@Controller
public class ArtistController {
    @Autowired
    private ArtistDao artistDao;

    /**
     * GET /create  --> Create a new artist and save it in the database.
     */
    @RequestMapping("/create")
    @ResponseBody
    public String create(String name, String annotations, String genius_id) {
        String userId = "";
        try {
            genius_id = genius_id.replaceAll("/$", "");
            Artist artist = new Artist(name, annotations, genius_id);
            artistDao.save(artist);
            userId = String.valueOf(artist.getId());
        }
        catch (Exception ex) {
            return "Error creating the artist: " + ex.toString();
        }
        return "User succesfully created with id = " + userId;
    }

   /**
     * GET /get-by-email  --> Return the id for the user having the passed
     * email.
     */
    @RequestMapping("/get")
    @ResponseBody
    public String getByEmail(String genius_id) {
        String artistId = "";
        try {
            Artist artist = artistDao.findByGeniusId(genius_id);
            artistId = String.valueOf(artist.getId());
        }
        catch (Exception ex) {
            return "User not found";
        }
        return "The user id is: " + artistId;
    }

}

问题是在getAllSongAnnotations方法的GeniusApiClient.java中,当我尝试访问artistDao时,我有一个空指针异常。我知道我对这个课程的实例化正在弄乱自动装配,但我很好奇解决这个问题的最佳方法是什么。

我考虑过让我在课堂上的所有方法都是静态的,这样我就不必实例化一个新方法,但我认为这不会很好。有什么建议吗?

由于

编辑:

为清晰起见删除了一些不相关的代码。

EDIT2:

添加了ArtistController.java

3 个答案:

答案 0 :(得分:3)

为了能够自动装配/注入对象,该对象必须是Spring bean。

在这里你不能自动装配ArtistDao,因为它不是一个bean。有几个注释选项可以使它成为bean,但在这种情况下适合的是@Repository注释。它只是您在@Component类中使用的GeniusApiClient的专用版本。

所以,

@Repository
@Transactional
public interface ArtistDao extends CrudRepository<Artist, Long> {
    public Artist findByGeniusId(String geniusId);
}

应该有用。

我建议您阅读:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html

如果阅读参考文档对您来说听起来很可怕,您还可以查看Spring in Action Core Spring 部分。

答案 1 :(得分:1)

不要让GeniusApiClient.class最终成功。 Spring将使用CGLIB动态扩展您的类以生成代理。 CGLIB的工作要求是让你的课程不是最终的。

有关此内容的更多信息:Make Spring Service Classes Final?

答案 2 :(得分:0)

你在catch块中尝试做的事情对我来说并不清楚,你必须纠正它并用所需的动作替换它以便在任何异常情况下采取。