gpt4 book ai didi

java - Autowiring 拒绝 Autowiring 的类的成员

转载 作者:行者123 更新时间:2023-11-30 05:51:44 25 4
gpt4 key购买 nike

我有一个类“ReportEverythingForm”,它拒绝通过自动连接注释实例化类“ReportQueryDetailsDAO”。如果我添加一个通过“new”实例化类“ReportQueryDetailsDAO”的黑客,那么类“reportQueryDetailsDAO”将反过来实例化其 Autowiring 字段,如实体管理器,这将导致空异常。

@Component
public class ReportEverythingForm
{
@Autowired
ReportQueryDetailsDAO reportQueryDetailsDAO;

public ReportEverythingForm() {
//reportQueryDetailsDAO = new ReportQueryDetailsDAO();//HACK FIXME Autowired is null
}

类“ReportEverythingForm”由 Controller 类“MonitorReportController”实例化并调用。 Autowiring 似乎在这里起作用。

@Controller
public class MonitorReportController
{
@Autowired
private ReportEverythingForm reportEverythingForm;

@Transactional
@RequestMapping(value = "/reportEverything", method = RequestMethod.POST)
public ModelAndView getReportEverything(HttpServletRequest request,
HttpServletResponse response, HttpSession session, Locale locale)
{
//code which calls methods of "reportEverythingForm"

如何让 Autowiring 工作?它适用于代码的其他部分,因此我认为这不是一个普遍问题。

一些相关的导入如下:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

请求的“ReportQueryDetailsDao”的内容:

@Repository
public class ReportQueryDetailsDAO
{

private static final Logger logger = LoggerFactory
.getLogger(ReportQueryDetailsDAO.class);

@PersistenceContext(unitName = "RATOR_MONITORING_CONFIGURATION", type = PersistenceContextType.TRANSACTION)
private EntityManager entityManager;


/**
* @param sql
* @return
*/
@Transactional
public ReportQueryDetail findbySQLId(Long sqlId)
{
try
{
//Query query = entityManager.createNativeQuery("select * from REPORT_QUERY_DETAILS where REPORT_QUERY_DETAILS_ID = :sqlId");
logger.info("select rqd from ReportQueryDetail rqd where rqd.reportQueryDetailsId = " + sqlId);
Query query = entityManager.createQuery("select rqd from ReportQueryDetail rqd where rqd.reportQueryDetailsId = :sqlId");
query.setParameter("sqlId", sqlId);

List<ReportQueryDetail> results = query.getResultList();
ReportQueryDetail reportQueryDetail = (ReportQueryDetail) results.get(0);

return reportQueryDetail;
} catch (Exception e)
{
logger.debug("Exception:" + e.getMessage(), e);
return null;
}
}

摘自pom.xml:

<java-version>1.6</java-version>
<org.springframework-version>3.1.1.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>

最佳答案

尝试改变

public ReportEverythingForm() {
//reportQueryDetailsDAO = new ReportQueryDetailsDAO();//HACK FIXME Autowired is null
}

@Autowired
public ReportEverythingForm(ReportQueryDetailsDAO reportQueryDetailsDAO) {
this.reportQueryDetailsDAO = reportQueryDetailsDAO;
}

(我认为您需要该构造函数在 ReportEverythingForm 中设置一些初始状态)

同时从 ReportQueryDetailsDAO reportQueryDetailsDAO 字段中删除 @Autowired 注释

编辑

另一种解决方案是使用 @PostConstruct注释,并删除您的构造函数。这样,您就可以在 @PostConstruct 带注释的方法中设置初始状态

关于java - Autowiring 拒绝 Autowiring 的类的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53780470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com