创建名为“activityController”的 bean 时出错

Posted

技术标签:

【中文标题】创建名为“activityController”的 bean 时出错【英文标题】:Error creating bean with name 'activityController' 【发布时间】:2021-11-28 13:19:55 【问题描述】:

我在服务器 (Tomcat) 上运行我的项目时遇到了这个问题。我使用 Spring 创建了一个 Maven 项目。 Java 版本是 Java 1.8。 没有弹簧启动!

Error creating bean with name 'activityController': Unsatisfied dependency expressed through field 'activityService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'activityService': Unsatisfied dependency expressed through field 'dao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.calendar.repository.ActivityDAO' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)

你能帮帮我吗?谢谢!

这是代码。 谢谢!!!

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>

    <groupId>com.calendar</groupId>
    <artifactId>calendar</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>Calendar</name>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <springframework.version>5.1.4.RELEASE</springframework.version>
        <jackson.version>2.11.2</jackson.version>
        <email.version>1.6.2</email.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>$springframework.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>$springframework.version</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>2.4.7</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>$jackson.version</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.1.Final</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.2.0</version>
        </dependency>
    </dependencies>

    <repositories>
    </repositories>

    <build>
        <finalName>company-calendar</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>$java.version</source>
                    <target>$java.version</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Activity.java

package com.calendar.entity;

import java.io.Serializable;
import javax.persistence.*;
import java.sql.Timestamp;
import java.util.List;

/**
 * The persistent class for the activity database table.
 *
 */
@Entity
@NamedQuery(name = "Activity.findAll", query = "SELECT a FROM Activity a")
public class Activity implements Serializable 

    private static final long serialVersionUID = 1L;

    @Id
    private Integer id;

    @Column(name = "create_time")
    private Timestamp createTime;

    @Column(name = "created_by")
    private String createdBy;

    private String description;

    @Column(name = "update_time")
    private Timestamp updateTime;

    @Column(name = "updated_by")
    private String updatedBy;

    //bi-directional many-to-one association to CalendarHeader
    @OneToMany(mappedBy = "activity")
    private List<CalendarHeader> calendarHeaders;

    public Activity() 
    

    public Integer getId() 
        return this.id;
    

    public void setId(Integer id) 
        this.id = id;
    

    public Timestamp getCreateTime() 
        return this.createTime;
    

    public void setCreateTime(Timestamp createTime) 
        this.createTime = createTime;
    

    public String getCreatedBy() 
        return this.createdBy;
    

    public void setCreatedBy(String createdBy) 
        this.createdBy = createdBy;
    

    public String getDescription() 
        return this.description;
    

    public void setDescription(String description) 
        this.description = description;
    

    public Timestamp getUpdateTime() 
        return this.updateTime;
    

    public void setUpdateTime(Timestamp updateTime) 
        this.updateTime = updateTime;
    

    public String getUpdatedBy() 
        return this.updatedBy;
    

    public void setUpdatedBy(String updatedBy) 
        this.updatedBy = updatedBy;
    

    public List<CalendarHeader> getCalendarHeaders() 
        return this.calendarHeaders;
    

    public void setCalendarHeaders(List<CalendarHeader> calendarHeaders) 
        this.calendarHeaders = calendarHeaders;
    

    public CalendarHeader addCalendarHeader(CalendarHeader calendarHeader) 
        getCalendarHeaders().add(calendarHeader);
        calendarHeader.setActivity(this);

        return calendarHeader;
    

    public CalendarHeader removeCalendarHeader(CalendarHeader calendarHeader) 
        getCalendarHeaders().remove(calendarHeader);
        calendarHeader.setActivity(null);

        return calendarHeader;
    


ActivityDAO.java

    package com.calendar.repository;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.stereotype.Repository;
    
    import com.calendar.entity.Activity;
    
    
    @Repository
    public interface ActivityDAO extends JpaRepository<Activity, Integer> 


**ActivityDTO.java**

package com.calendar.dto;

import java.sql.Timestamp;

public class ActivityDTO 

    private Integer id;

    private String description;
    
    private Timestamp createTime;

    private String createdBy;
    
    private Timestamp updateTime;

    private String updatedBy;

    public Integer getId() 
        return id;
    

    public void setId(Integer id) 
        this.id = id;
    

    public String getDescription() 
        return description;
    

    public void setDescription(String description) 
        this.description = description;
    

    public Timestamp getCreateTime() 
        return createTime;
    

    public void setCreateTime(Timestamp createTime) 
        this.createTime = createTime;
    

    public String getCreatedBy() 
        return createdBy;
    

    public void setCreatedBy(String createdBy) 
        this.createdBy = createdBy;
    

    public Timestamp getUpdateTime() 
        return updateTime;
    

    public void setUpdateTime(Timestamp updateTime) 
        this.updateTime = updateTime;
    

    public String getUpdatedBy() 
        return updatedBy;
    

    public void setUpdatedBy(String updatedBy) 
        this.updatedBy = updatedBy;
    
    
    


ActivityService.java

package com.calendar.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.calendar.dto.ActivityDTO;
import com.calendar.repository.ActivityDAO;
import com.calendar.entity.Activity;

@Service
public class ActivityService 
    
    @Autowired
    ActivityDAO dao;
    
    public List<ActivityDTO> findAll() 
        List<ActivityDTO> DTOList = new ArrayList<ActivityDTO>();
        List<Activity> entity = dao.findAll();
        if (entity != null) 
            for (Activity e : entity) 
                DTOList.add(getDTOFromEntity(e));
            
        
        return DTOList;
    ;

        
    
    protected ActivityDTO getDTOFromEntity(Activity entity) 
        ActivityDTO activityDTO = new ActivityDTO();
        
        activityDTO.setId(entity.getId());
        activityDTO.setDescription(entity.getDescription());
        activityDTO.setCreatedBy(entity.getCreatedBy());
        activityDTO.setCreateTime(entity.getCreateTime());
        activityDTO.setUpdatedBy(entity.getUpdatedBy());
        activityDTO.setUpdateTime(entity.getUpdateTime());
        
        return activityDTO;
    


ActivityController.java

package com.calendar.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.calendar.dto.ActivityDTO;
import com.calendar.service.ActivityService;

@RestController
@RequestMapping("activity")
public class ActivityController 
    
    
    @Autowired
    private ActivityService activityService;
    
    private ActivityService getService() 
        System.out.println("service= " + activityService);
        return activityService;
    
    
    @Transactional(readOnly = true)
    @GetMapping("")
    @ResponseBody
    public List<ActivityDTO> findAll() 
        System.out.println("SIL findAll");
        return getService().findAll();
    


AppConfig.java

package com.calendar.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.jpa.LocalEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan(basePackages = "com.calendar")
public class AppConfig implements WebMvcConfigurer 

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) 
        //to see html files
        configurer.enable();
    

    //Entity manager
    @Bean(name = "Calendar_PU")
    public LocalEntityManagerFactoryBean getEntityManagerFactoryBean() 
        LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean();
        factoryBean.setPersistenceUnitName("Calendar_PU");
        return factoryBean;
    

问候, 弗朗切斯科

【问题讨论】:

你在使用 Spring Boot 吗?如果是,你能分享你的主要课程吗?谢谢。 不,我没有使用 Spring-Boot。我有“AppConfig.java”,我现在要添加 【参考方案1】:

尝试将@EnableJpaRepositories 添加到您的AppConfig

@Configuration
@EnableWebMvc
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.calendar.repository")
@ComponentScan(basePackages = "com.calendar")
public class AppConfig implements WebMvcConfigurer 

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) 
        //to see html files
        configurer.enable();
    

    //Entity manager
    @Bean(name = "Calendar_PU")
    public LocalEntityManagerFactoryBean getEntityManagerFactoryBean() 
        LocalEntityManagerFactoryBean factoryBean = new LocalEntityManagerFactoryBean();
        factoryBean.setPersistenceUnitName("Calendar_PU");
        return factoryBean;
    

您还需要更改您的pom.xml。依赖太多了(大部分已经是spring-data-jpa的一部分)并且spring-data-jpa需要Spring 5.3.5

<?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>

<groupId>com.calendar</groupId>
<artifactId>calendar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>Calendar</name>

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <springframework.version>5.3.5</springframework.version>
    <jackson.version>2.11.2</jackson.version>
    <email.version>1.6.2</email.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>$springframework.version</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.4.7</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>$jackson.version</version>
    </dependency>
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-web-api</artifactId>
        <version>7.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.2.0</version>
    </dependency>
</dependencies>

<repositories>
</repositories>

<build>
    <finalName>company-calendar</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <source>$java.version</source>
                <target>$java.version</target>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>

这就是您应该考虑使用 Spring Boot 的原因。您将避免无法协同工作的依赖关系问题。

【讨论】:

现在我有另一个错误:Servlet [SpringDispatcher] 的 Servlet.init() 抛出异常 java.lang.NoSuchFieldError: IMPORT_BEAN_NAME_GENERATOR 检查我的答案,我已经编辑了它;)

以上是关于创建名为“activityController”的 bean 时出错的主要内容,如果未能解决你的问题,请参考以下文章

如何自定义activityController Swift

隐藏 UIActivityViewController 中的底行

隐藏 UIActivityViewController 中的底行

从 iOS 中的 UIActivityController 选项中删除注释

UIActivityViewController 共享 UIActivity 未显示

创建 NSManagedObject 子类,创建名为我的项目的类