我有2个服务类,它们都使用自动装配的bean扩展相同的父类,我可以在一个类中使用bean但在另一个类中不使用[重复]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我有2个服务类,它们都使用自动装配的bean扩展相同的父类,我可以在一个类中使用bean但在另一个类中不使用[重复]相关的知识,希望对你有一定的参考价值。

这个问题在这里已有答案:

我仍然是Spring的新手,但我目前正面临着一个我不知道原因的问题,我只能通过nullpointerexception来解决这个问题。

我有两个类都实现相同的Abstract类。 InventoryService和InventoryMailService。所有3个都在使用组件扫描成功扫描的同一个包中。我正在尝试使用我的globalPrefService bean,我可以在InventoryService中成功使用但不知道我的InventoryMailService。

这是我用来连接我的服务的AbstractService:

package com.paperfoam.inventory.service;

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

@Service("abstractService")
public abstract class AbstractService {

    protected InventoryService inventoryService;
    protected FinancialCalculationService financialCalculationService;
    protected SupplierService supplierService;
    protected UserService userService;
    protected GlobalPrefService globalPrefService;

    @Autowired
    public void setGlobalPrefService(GlobalPrefService globalPrefService) {
        this.globalPrefService = globalPrefService;
    }

    @Autowired
    public void setInventoryService(InventoryService inventoryService) {
        this.inventoryService = inventoryService;
    }

    @Autowired
    protected void setFinancialCalculationService(FinancialCalculationService financialCalculationService) {
        this.financialCalculationService = financialCalculationService;
    }

    @Autowired
    protected void setSupplierService(SupplierService supplierService) {
        this.supplierService = supplierService;
    }

    @Autowired
    protected void setUsersService(UserService userService) {
        this.userService = userService;
    }


}

这是InventoryService(减去一些非相关函数),当我尝试调用我的GlobalPrefService时,它工作得很好:

package com.paperfoam.inventory.service;

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

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

import com.paperfoam.inventory.dao.InventoryDao;
import com.paperfoam.inventory.dao.InventoryLogItemDao;
import com.paperfoam.inventory.objects.InventoryItem;
import com.paperfoam.inventory.objects.InventoryLogItem;

@Service("inventoryService")
public class InventoryService extends AbstractService {

    private InventoryDao inventoryDao;
    private InventoryLogItemDao inventoryLogItemDao;

    public InventoryDao getInventoryDao() {
        return inventoryDao;
    }

    @Autowired
    public void setInventoryDao(InventoryDao inventoryDao) {
        this.inventoryDao = inventoryDao;
    }

    @Autowired
    public void setInventoryLogItemDao(InventoryLogItemDao inventoryLogItemDao) {
        this.inventoryLogItemDao = inventoryLogItemDao;
    }
    // This is the method where I succesfully use my GlobalPrefService
    public List<InventoryItem> getAllInventoryItems() {
        System.out.println(globalPrefService.getPreferenceValue("mailusername")); // this succesfully prints my preferenceValue to the console
        return inventoryDao.getInventoryList();

    }
} 

但是,我有我的另一个类(相同的包)扩展相同的AbstractService,当我尝试使用它时返回NullPointerException:

package com.paperfoam.inventory.service;

import java.io.File;
import java.util.logging.Logger;

import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.mail.MailParseException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.paperfoam.inventory.config.JavaBeanConfig;
import com.paperfoam.inventory.objects.InventoryItem;
import com.paperfoam.inventory.objects.Supplier;

@Service("inventoryMailService")
public class InventoryMailService extends AbstractService {


    public void sendMessageWithAttachment(String to, String subject, String text, String pathToAttachment) {

        System.out.println(globalPrefService.getPreferenceValue("mailusername")); // NullPointerException on the globalPrefService!
        /*
        JavaBeanConfig javaBean = new JavaBeanConfig();
        JavaMailSender mailSender = javaBean.getJavaMailSender(globalPrefService);

        try {

        MimeMessage message = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(message, true);


            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text);

        Resource file = new ClassPathResource(pathToAttachment);
        helper.addAttachment("hoera.png", file);

        mailSender.send(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
        */
    }
} 

我得到的错误:

SEVERE: Servlet.service() for servlet [paperfoam] in context with path [/bulletjournal] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
    at com.paperfoam.inventory.service.InventoryMailService.sendMessageWithAttachment(InventoryMailService.java:30) // This is the system.out line
    at com.paperfoam.inventory.controllers.InventoryController.showInventoryStatistics(InventoryController.java:165)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:506)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:962)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1115)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source) 

我已经盯着这一段时间,我无法弄清楚是什么原因造成的。所有内容都以相同的方式注释,它们扩展了自动装入bean的同一个类,它们位于正在扫描的同一个包中,访问修饰符也是相同的。

我也试过清理/重建我的项目几次,有没有人知道我在这里缺少什么?

谢谢你的阅读。

回应评论:

我的InventoryService在控制器中获得Autowired。

protected InventoryService inventoryService;

@Autowired
    public void setInventoryService(InventoryService inventoryService) {
        this.inventoryService = inventoryService;
    } 

但我手动实例化我的InventoryMailService(我开始认为这是错误)进行调试。

    @RequestMapping(value = { "/statistics", "/" })
    public String showInventoryStatistics(Model model) {

        Map<Integer, Long> calculated = financialCalculationService.getSuppliersWithTotalWorth();
        List<Supplier> allSuppliers = supplierService.convertSupplierHashmapToObjectList(calculated);

        List<InventoryItem> lowItems = inventoryService.getLowInventoryItems();
        List<InventoryLogItem> recentChanges = inventoryService.getRecentChanges();

        model.addAttribute("suppliers", allSuppliers);
        model.addAttribute("lowitems", lowItems);
        model.addAttribute("recentchanges", recentChanges);

        InventoryMailService mail = new InventoryMailService();
        System.out.println(mail.toString());
        mail.sendMessageWithAttachment("Redacted@gmail.com", "betere mail", "test123", "com/paperfoam/inventory/mail/success.png");

        return JspPage.INVENTORY_STATISTICS;
    } 

我甚至没有停下来认为这可能会影响到路上的自动装配,这对春天有影响吗?

答案

要使spring处理依赖项注入,需要自动连接整个依赖关系链,因此您还需要在InventoryController中自动装配InventoryMailService。

以上是关于我有2个服务类,它们都使用自动装配的bean扩展相同的父类,我可以在一个类中使用bean但在另一个类中不使用[重复]的主要内容,如果未能解决你的问题,请参考以下文章

使用@SpringBootTest时如何在测试类中自动装配bean

Spring:无法从父上下文自动装配 bean

在方法上添加@Transactional后,这个类(服务)不能自动装配bean(存储库)?

Spring Boot:自动装配具体类时“找不到类型的合格bean ...”

使用 application-test.properties 自动装配 Bean

springboot自动装配