gpt4 book ai didi

java - 构造函数 Autowiring 调用三次(Spring 5)

转载 作者:行者123 更新时间:2023-12-02 08:40:18 27 4
gpt4 key购买 nike

我正在使用 spring 5,我编写了一个 CacheManger 类,我用它来手动缓存数据,我在注释的帮助下 Autowiring 了构造函数。我已将此对象注入(inject)到其他 spring beans 中。问题是,当我启动 tomcat 时,这个构造函数被调用了 3 次。我不知道为什么?请帮帮我。

这是一个用struts构建的旧系统,需要升级它,另请参阅web.xml内容,为什么要为ActionServlet创建上下文

这是我的课:-

@Component
public class CacheManager {

UtilDAO utilDao;

@Autowired
public CacheManager(UtilDAO utilDAO) {
System.out.println("Calling the CacheManager Constructor...");
this.utilDao = utilDAO;
loadValueMaps();
}

private HashMap mapOfValueMaps = null;


@Transactional(propagation = Propagation.SUPPORTS)
public void loadValueMaps(int branchId) {
List tmpLst = this.utilDao.getValueMapsList(branchId);

if (this.mapOfDealerLang == null) {
this.mapOfDealerLang = new HashMap<Integer, ArrayList<ALang>>();
}

ArrayList<AValue> list = this.mapOfValueMaps.get(branchId);
if (list == null) {
list = new ArrayList<ALang>(1);
this.mapOfDealerLang.put(branchId, list);
} else
list.clear();

for (Iterator iter = tmpLst.iterator(); iter.hasNext();) {
AValue element = (AValue) iter.next();
list.add(element);
}
}

这是我的 Dao 层

@Repository
public class UtilDAOImpl extends DatabaseConnection implements UtilDAO {

@Autowired
@Qualifier("sessionFactoryBean")
SessionFactory sessionfactoryBean;

@Autowired
public void setSessionFactoryBean() {
setSessionFactory(sessionfactoryBean);
}

@Override
@Transactional(propagation = Propagation.SUPPORTS)
public List getValueMapsList() {
String query = "from AValue as blk where blk.blkActive = :blval";
List reqList = findbyParam(query, "blval", true);
Set setItems = new LinkedHashSet(reqList);
reqList.clear();
reqList.addAll(setItems);
return reqList;
}
}

这是 DI:-

@Component
public class ListActionManager {


@Autowired
ViewService vwService;

@Autowired
CacheManager cachMang;


public void getList(int pageNo,DisplayListForm form, SessionBean bean) throws Exception{
try{
ListView lstVw=null;
int maxResult=25;
int totalRecords=0;

lstVw = vwService.getList(bean.getBranch_ind(),ssUsrBn.getBranch_main_ind(),pageNo,maxResult,totalRecords);}
form.setdata(lstVw);
}catch(Exception ex){
throw ex;
}
}
}

启动tomcat后控制台日志:-

Apr 25, 2020 11:55:29 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://struts.apache.org/tags-html is already defined
Apr 25, 2020 11:55:29 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://struts.apache.org/tags-logic is already defined
Apr 25, 2020 11:55:29 AM org.apache.catalina.startup.TaglibUriRule body
INFO: TLD skipped. URI: http://struts.apache.org/tags-nested is already defined
Apr 25, 2020 11:55:29 AM org.apache.catalina.startup.TldConfig execute
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Apr 25, 2020 11:55:29 AM org.apache.catalina.core.ApplicationContext log
INFO: No Spring WebApplicationInitializer types detected on classpath
Apr 25, 2020 11:55:29 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.


**Calling the CacheManager Constructor...**


Apr 25, 2020 11:55:35 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring DispatcherServlet 'actionbean'


**Calling the CacheManager Constructor...**


Apr 25, 2020 11:55:37 AM org.apache.catalina.core.ApplicationContext log
INFO: Initializing WebApplicationContext for Struts ActionServlet 'servlet', module ''

**Calling the CacheManager Constructor...**


Apr 25, 2020 11:55:39 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Apr 25, 2020 11:55:39 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Apr 25, 2020 11:55:39 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 12491 ms

这个web.xml

    <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>



<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>2</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>

最佳答案

正在为每个ApplicationContext创建bean。您有三个 ApplicationContext

Spring root WebApplicationContext
Spring DispatcherServlet ApplicationContext
WebApplicationContext for Struts ActionServlet

关于java - 构造函数 Autowiring 调用三次(Spring 5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61421944/

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