gpt4 book ai didi

java - 已创建但未 Autowiring 的 Spring bean

转载 作者:搜寻专家 更新时间:2023-10-31 20:19:15 24 4
gpt4 key购买 nike

我知道有很多类似的问题。我浏览了很多,但问题仍然存在。

我有一个已创建但未 Autowiring 的服务。没有(!)手动启动,无论是在项目中还是在测试中(如 this question)

Tomcat 输出显示,两个 bean 都已找到并创建。至少第一个服务没有注入(inject)到应该注入(inject)的地方。我不知道第二个。

服务(接口(interface)):

public interface SchoolService {
public School getSchool(String id);
}

服务(实现):

@Service
@Transactional
public class SchoolServiceImpl implements SchoolService {

@Autowired
private SchoolDAO schoolDAO;

public School getSchool(String id) {
//database things
return school;
}

}

它在哪里被“调用”

public class SchoolMenu implements Serializable {

@Autowired
private SchoolService schoolService;

public SchoolMenu () {
//here schoolService is null
School school = schoolService.getSchool("id");
}

}

应用程序上下文.xml

<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


<bean id="SchoolServiceImpl" class="content_management.School.service.SchoolServiceImpl"/>
<bean id="SchoolDAOImpl" class="content_management.School.dao.SchoolDAOImpl"/>
</beans>

Tomcat 输出:

org.springframework.beans.factory.support.DefaultListableBeanFactory: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@57003970: defining beans [SchoolServiceImpl,SchoolDAOImpl]; root of factory hierarchy
org.springframework.beans.factory.support.DefaultListableBeanFactory: Creating shared instance of singleton bean 'SchoolServiceImpl'
org.springframework.beans.factory.support.DefaultListableBeanFactory: Creating instance of bean 'SchoolServiceImpl'
org.springframework.beans.factory.support.DefaultListableBeanFactory: Eagerly caching bean 'SchoolServiceImpl' to allow for resolving potential circular references
org.springframework.beans.factory.support.DefaultListableBeanFactory: Finished creating instance of bean 'SchoolServiceImpl'
org.springframework.beans.factory.support.DefaultListableBeanFactory: Creating shared instance of singleton bean 'SchoolDAOImpl'
org.springframework.beans.factory.support.DefaultListableBeanFactory: Creating instance of bean 'SchoolDAOImpl'
org.springframework.beans.factory.support.DefaultListableBeanFactory: Eagerly caching bean 'SchoolDAOImpl' to allow for resolving potential circular references
org.springframework.beans.factory.support.DefaultListableBeanFactory: Finished creating instance of bean 'SchoolDAOImpl'

我的错误在哪里?

最佳答案

为了让@Autowired 工作,SchoolMenu 也必须是一个Spring bean。如果不是,您可以从应用程序上下文中获取 schoolService。像这样

ApplicationContext appContext = new ClassPathXmlApplicationContext("application-context.xml");
SchoolService schoolService = (SchoolService) appContext.getBean(SchoolService.class);

关于java - 已创建但未 Autowiring 的 Spring bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29799172/

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