Home > database >  is there a problem with profiles? NoSuchBeanDefinitionException: No bean named 'entityManagerFa
is there a problem with profiles? NoSuchBeanDefinitionException: No bean named 'entityManagerFa

Time:07-08

For the fourth day I have been trying to cope with this error.

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:872)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1344)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:309)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:330)
    ... 21 more

this problem occurs when I run SpringMain in tests:

package topjava.quest;

import org.springframework.context.support.GenericXmlApplicationContext;

import java.util.Arrays;

public class SpringMain {
    public static void main(String[] args) {
        try(GenericXmlApplicationContext appCtx = new GenericXmlApplicationContext()) {
            appCtx.getEnvironment().setActiveProfiles("postgres", "datajpa");
            appCtx.load("spring/inmemory.xml");
            appCtx.refresh();

            System.out.println("Bean definition names "   Arrays.toString(appCtx.getBeanDefinitionNames()));
        }
    }
}

there may be several profiles in the application, there is an assumption that this is where the error is, but I can't find it. While using one profile "postgres,datajpa".

web.xml:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <display-name>Graduation_Project</display-name>

    <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>postgres,datajpa</param-value>
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring/spring-app.xml
            classpath:spring/spring-db.xml
        </param-value>
    </context-param>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

spring-db.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <jdbc:initialize-database data-source="dataSource" enabled="${database.init}">
        <jdbc:script location="${jdbc.initLocation}"/>
        <jdbc:script encoding="utf-8" location="classpath:db/populateDB.sql"/>
    </jdbc:initialize-database>

    <tx:annotation-driven/>

    <beans profile="postgres">

        <bean >
            <property name="staticMethod" value="org.slf4j.bridge.SLF4JBridgeHandler.install"/>
        </bean>

        <context:property-placeholder location="classpath:db/postgres.properties" system-properties-mode="OVERRIDE"/>

        <bean id="dataSource"
              
              p:driverClassName="org.postgresql.Driver"
              p:url="${database.url}"
              p:username="${database.username}"
              p:password="${database.password}"/>
    </beans>

    <beans profile="datajpa">
        <bean id="entityManagerFactory" 
              p:dataSource-ref="dataSource"
              p:packagesToScan="topjava.quest.model">
            <property name="jpaPropertyMap">
                <map>
                    <entry key="#{T(org.hibernate.cfg.AvailableSettings).FORMAT_SQL}" value="${hibernate.format_sql}"/>
                    <entry key="#{T(org.hibernate.cfg.AvailableSettings).USE_SQL_COMMENTS}" value="${hibernate.use_sql_comments}"/>
                    <entry key="#{T(org.hibernate.cfg.AvailableSettings).JPA_PROXY_COMPLIANCE}" value="${hibernate.jpa_proxy_compliance}"/>
                </map>
            </property>

            <property name="jpaVendorAdapter">
                <bean  p:showSql="${jpa.showSql}"/>
            </property>
        </bean>

    </beans>

    <beans profile="datajpa">
        <context:component-scan base-package="topjava.quest.repository.datajpa"/>
        <jpa:repositories base-package="topjava.quest.repository.datajpa"/>
    </beans>
</beans>

pom.xml:

<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 https://maven.apache.org/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>topjava.quest</groupId>
    <artifactId>graduationProject</artifactId>
    <packaging>war</packaging>

    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>17</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <tomcat.version>9.0.59</tomcat.version>

        <!--Spring-->
        <spring-version>5.3.15</spring-version>
        <spring-data-jpa.version>2.6.2</spring-data-jpa.version>

        <!-- Logging -->
        <logback.version>1.2.10</logback.version>
        <slf4j.version>1.7.35</slf4j.version>

        <!--DB-->
        <postgresql.version>42.3.3</postgresql.version>

        <!--Test-->
        <junit.version>4.13.2</junit.version>
        <assertj.version>3.22.0</assertj.version>

        <!--Hibernate-->
        <hibernate.version>5.6.5.Final</hibernate.version>
        <hibernate-validator.version>6.2.2.Final</hibernate-validator.version>
        <javax-el.version>3.0.1-b12</javax-el.version>

        <!--Web-->
        <jstl.version>1.2</jstl.version>

    </properties>

    <build>
        <finalName>graduationProject</finalName>
        <defaultGoal>package</defaultGoal>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>

            <!--JDK17-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>

            <!--Test-->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                </configuration>
            </plugin>

        </plugins>
    </build>

    <dependencies>
        <!--Logging with SLF4J & LogBack-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>${logback.version}</version>
            <scope>runtime</scope>
        </dependency>

        <!--JDK17-->
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!--Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>${spring-data-jpa.version}</version>
        </dependency>

        <!--ORM-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>${hibernate-validator.version}</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.el</artifactId>
            <version>${javax-el.version}</version>
            <scope>provided</scope>
        </dependency>

        <!--Web-->
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-servlet-api</artifactId>
            <version>${tomcat.version}</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>${jstl.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>

        <!--Test-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>${assertj.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <profiles>
        <profile>
            <id>postgres</id>
            <dependencies>
                <dependency>
                    <groupId>org.postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>${postgresql.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.tomcat</groupId>
                    <artifactId>tomcat-jdbc</artifactId>
                    <version>${tomcat.version}</version>
                    <scope>provided</scope>
                </dependency>
                <dependency>
                    <groupId>org.slf4j</groupId>
                    <artifactId>jul-to-slf4j</artifactId>
                    <version>${slf4j.version}</version>
                    <scope>runtime</scope>
                </dependency>
            </dependencies>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
    </profiles>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>${spring-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

in spring-app, spring-db and spring-mvc, the active profiles "postgres,datajpa" are selected, I can't understand what the problem is?

when I try to start Tomcat, I get another exception

20-Jun-2022 13:45:05.104 SEVERE [http-nio-8080-exec-6] org.apache.catalina.core.StandardWrapperValve.invoke Ошибка выделения для сервлета [mvc-dispatcher]
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'topjava.quest.service.RestaurantService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

my code in classes:

@Service
public class RestaurantService

@Autowired
private RestaurantService restaurantService;

the beans are not duplicated anywhere, the service is @autowired and marked with the @service annotation. What's wrong?

CodePudding user response:

the problem was here, not enough org.springframework.web.context.ContextLoaderListener in web.xml

<listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    
    </listener>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <display-name>Graduation_Project</display-name>


    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:spring/spring-app.xml
            classpath:spring/spring-db.xml
        </param-value>
    </context-param>


    **<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>**

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>
  • Related