0

I'm using the following POM versions:

<properties>
    <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <springframework.version>4.3.5.RELEASE</springframework.version>
    <springsecurity.version>4.0.4.RELEASE</springsecurity.version>
    <hibernate.version>4.3.11.Final</hibernate.version>
    <mysql.connector.version>5.1.31</mysql.connector.version>
</properties>
.
.
.
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-websocket</artifactId>
    <version>4.2.4.RELEASE</version>
</dependency>
.
.
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>5.1.3.Final</version>
</dependency>
.
.
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>4.0.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.7</version>
</dependency>

and for the test libraries:

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.9.5</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>3.2.3.RELEASE</version>
    <scope>test</scope>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>javax.el</groupId>
    <artifactId>javax.el-api</artifactId>
    <version>2.2.4</version>
</dependency>
<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.el</artifactId>
    <version>2.2.4</version>
</dependency>

And I have included every class annotated with @Configuration in the @ContextConfiguration(classes={...} annotation, yet I still get the same error could not load application context. I know I have to add every dependency here, so I've added all the classes to the ContextConfiguration and still the same.
I tried to use ComponentScan annotation on a newly created configuration class, and it still the same.

Please note that I am using annotation configurations not xml files.
one of the error many lines:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTokenRepositoryImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.hibernate.SessionFactory com.company.projectname.dao.AbstractDao.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.company.api.configuration.spirng.HibernateConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.hibernate4.LocalSessionFactoryBean]: Factory method 'sessionFactory' threw exception; nested exception is java.lang.IllegalStateException: @Bean method HibernateConfiguration.dataSource called as a bean reference for type [javax.sql.DataSource] but overridden by non-compatible bean instance of type [com.company.projectname.model.DataSource]. Overriding bean of same name declared in: null

And here is one of the @Configuration classes:

@Configuration
@EnableWebMvc
@EnableScheduling
@ComponentScan(basePackages = {"com.company.api", "com.company.project"})
public class SpringConfiguration extends WebMvcConfigurerAdapter {

    /**
     * Added @Lazy to prevent using messageSource in UserService before its
     * being created here
     */
    @Autowired
    @Lazy
    RoleConverter roleConverter;
    /**
     * Added @Lazy to prevent using messageSource in UserService before its
     * being created here
     */
    @Autowired
    @Lazy
    ReportConverter reportConverter;

    /**
     * Added @Lazy to prevent using messageSource in UserService before its
     * being created here
     */
    @Autowired
    @Lazy
    GroupConverter groupConverter;

    @Autowired
    @Lazy
    ReportStyleConverter reportStyleConverter;

    @Autowired
    @Lazy
    LanguageConverter languageConverter;

    @Autowired
    @Lazy
    UserProfileNotificationEventConverter userProfileNotificationEventConverter;

    @Autowired
    private ContextHolder context;

    .
    .
    .
    Other beans and functions
}
Tarek
  • 1,904
  • 2
  • 18
  • 36
  • Please show your configuration – akuma8 Apr 24 '19 at 14:10
  • @akuma8 I have updated the question and added one configuration class – Tarek Apr 25 '19 at 08:35
  • 1
    For starters stop mixing jars from different spring framework versions. You are mixing 3.2.3, 4.0.1 and 4.2.4. And the fact that not all your dependencies are listed makes me wonder which other versions you are adding to the mix. Never mix jars of different versions of a framework. – M. Deinum Apr 25 '19 at 08:44
  • I get that, can you suggest me a solution to solve this mix? is there a way to find out the best matching versions? @M.Deinum – Tarek Apr 25 '19 at 10:18
  • 1
    There are no best-matching versions. You should use a single version for all the spring related jars. Dont mix versions of any framework as that will lead to exceptions. You have a `springframework.version` property declared in maven, use that for the `version` of all the spring dependencies. – M. Deinum Apr 25 '19 at 10:33
  • 1
    Did you read your exception? There is a clue here `@Bean method HibernateConfiguration.dataSource called as a bean reference for type [javax.sql.DataSource] but overridden by non-compatible bean instance of type [com.company.projectname.model.DataSource]. Overriding bean of same name declared in: null` You have `javax.sql.DataSource` vs `com.company.projectname.model.DataSource` – akuma8 Apr 25 '19 at 12:55
  • what can be the solution of this problem ? @akuma8 – Tarek Apr 25 '19 at 12:56

0 Answers0