0

I created two spring boot project with a parent pom. In the project A, i added some Entities-Classes and ServiceImpl:

@Table
@Entity
public class Login implements Serializable {
    ... another column
    @Column(unique = true, columnDefinition = "VARCHAR(50)", nullable = false)
    private String username;
    // getter and setter

The pom.xml of project A:

 <parent>
        <groupId>com.emo.test</groupId>
        <artifactId>emo-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/>
    </parent>

    <artifactId>test-data</artifactId>
    <version>0.1-SNAPSHOT</version>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        // other dependencies
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <configuration>
                            <classifier>exec</classifier>
                        </configuration>
                    </execution>
                </executions>
                <configuration>
                    <excludeGroupIds>org.liquibase</excludeGroupIds>
                </configuration>
            </plugin>
        </plugins>
    </build>

I added the Project A in the project B as dependency like this:

<modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.emo.test</groupId>
        <artifactId>emo-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/>
    </parent>

    <artifactId>emo-oauth</artifactId>
    <version>0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>emo-oauth</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>com.emo.test</groupId>
            <artifactId>test-data</artifactId>
            <version>${test.data.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.liquibase</groupId>
                    <artifactId>liquibase-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

In my Implementation (Project A), i get some data in the database with the Login-Entity:

@Transactional
@Service("userServiceImpl")
public class UserServiceImpl {

    @PersistenceContext
    private EntityManager entityManager;

    public User findByUsername(String username) throws EmptyResultDataAccessException {
        String query = "FROM Login l WHERE l.username = :username";
        final Login l = (Login) entityManager.createQuery(query).setParameter("username", username).getSingleResult();
        return l.getUser();
    }
}

I used this Implementation in the project B:

Service("customUserDetailsService")
public class CustomUserDetailsService implements UserDetailsService {

    private UserServiceImpl userServiceImpl;

    @Autowired
    public CustomUserDetailsService(UserServiceImpl userServiceImpl) {
        this.userServiceImpl = userServiceImpl;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        final User user = userServiceImpl.findByUsername(username);
        if (user == null)
            throw new UsernameNotFoundException(String.format("User %s does not exist!", username));
        return new UserRepositoryUserDetails(user);
    }

    private final static class UserRepositoryUserDetails extends User implements UserDetails {

        ...
    }

I got this error on project B start:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-05-09 20:14:36.429 ERROR 5435 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.oauth.server.security.CustomUserDetailsService required a bean of type 'com.test.api.service.UserServiceImpl' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.test.api.service.UserServiceImpl' in your configuration.

My Parent pom.xml (project B)

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.emo.test</groupId>
    <artifactId>test-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <java.version>1.11</java.version>
        <oauth.version>2.2.2.RELEASE</oauth.version>
        <kazi.data.version>0.1-SNAPSHOT</kazi.data.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>${oauth.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-plugin-releases</id>
            <url>https://repo.spring.io/plugins-release</url>
        </pluginRepository>
    </pluginRepositories>

I already tried the Service with @Component or @Repository Someone knows?

emoleumassi
  • 4,881
  • 13
  • 67
  • 93

0 Answers0