Alex Weavers
Alex Weavers

Reputation: 710

Repository won't Beanify

I have an error that I can't track down. I'm new so sorry if I missed this in the searches, but I tried several things and no luck.

UserApi.java:

package com.jsp.jsp;

import com.jsp.models.User;
import com.jsp.services.UserService;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
class UserApi {
    private final UserService userService;

    public UserApi(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value="/api/user", method=RequestMethod.POST)
    public User createUser(
        @RequestParam(value="name", required=true) String name,
        @RequestParam(value="email", required=true) String email,
        @RequestParam(value="password", required=true) String password,
        @RequestParam(value="confirm", required=true) String confirm)
    {
        User u = this.userService.createUser(new User(name, email, password));
        return u;
    }
}

UserRepository.java:

package com.jsp.repositories;

import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

import com.jsp.models.User;

import org.springframework.data.repository.CrudRepository;

@Repository
public interface UserRepository extends CrudRepository<User, Long> {
    List<User> findByEmail(String email);
    Optional<User> findById(Long id);
    List<User> findAll();
}

Server.java

package com.jsp.jsp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication(scanBasePackages={"com.jsp.models","com.jsp.repositories","com.jsp.services"})
@RestController
public class Server {
    public static void main(String[] args) {
        SpringApplication.run(Server.class, args);
    }
}

UserService.java

package com.jsp.services;

import java.util.List;

import com.jsp.models.User;
import com.jsp.repositories.UserRepository;

import org.springframework.stereotype.Service;

@Service
public class UserService {
    private final UserRepository userRepository;

    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public List<User> allUsers() {
        return this.userRepository.findAll();
    }

    public User createUser(User u) {
        return this.userRepository.save(u);
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jsp</groupId>
    <artifactId>jsp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.mindrot</groupId>
            <artifactId>jbcrypt</artifactId>
            <version>0.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.11.7.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

So in short, when I run the above project in VSCode, it errors out with the following:

2019-02-23 19:07:52.744 WARN 25412 --- [ restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService' defined in file [C:\Users\Alex\Documents\dojo\javatown\everything\target\classes\com\jsp\services\UserService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.jsp.repositories.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

My understanding is that the repository I made should be automatically turned into a bean and then wire itself up to the service I made. However, that seems to be not happening. My example videos are using MySQL -- will that make a difference? Am I using the right driver for Postgres 11? I'm super lost.

Upvotes: 0

Views: 52

Answers (1)

Adina Rolea
Adina Rolea

Reputation: 2109

Some remarks and suggestions:

1.add autowired annotation on your constructor, it is clearer what you need spring to do.

@Autowired
public UserService(UserRepository userRepository) {
    this.userRepository = userRepository;
}

2. I don't understand @RestController annotation on your main class.

@SpringBootApplication(scanBasePackages {"com.jsp.models","com.jsp.repositories","com.jsp.services"})
@RestController    --> THIS CAN BE REMOVED
public class Server {
   public static void main(String[] args) {
       SpringApplication.run(Server.class, args);
   }
}

3.You should add @EnableJpaRepositories("com.jsp.repositories") on your spring boot application => it will scan the repositories in that package.

 @SpringBootApplication(scanBasePackages {"com.jsp.models","com.jsp.repositories","com.jsp.services"})
 @EnableJpaRepositories("com.jsp.repositories")
 public class Server {
    public static void main(String[] args) {
       SpringApplication.run(Server.class, args);
    }
 }

Upvotes: 1

Related Questions