当前位置: 首页 > 知识库问答 >
问题:

没有符合条件的bean。。。SpringWebMVC:applicationContext中定义的bean在ServletContext中不可见

洪宏硕
2023-03-14

我对APplicationContext中定义的bean有问题。(可能是我的问题是理解应用程序~Servlet上下文?)

所以,我定义

应用程序上下文。xml

    <beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc 

http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.
<!-- // configure manage of transactions using annotations @Transactional // -->

<tx:annotation-driven />

<!-- // SPRING-SECURITY // -->
<import resource="security-servlet.xml" />

<!-- // DB1 - Database Configuration // -->
<context:property-placeholder location="/WEB-INF/spring/db.config/jdbc-db1.properties" />

<import resource="./db.config/DataSource-db1.xml" />
<import resource="./db.config/jpa-config-db1.xml" />

<!-- Auto scan the components -->
<context:annotation-config />

<!-- // common beans for rest & mvc // -->
<context:component-scan base-package="abc.def.data"  />

<bean
    id="messageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource"
>
    <property
        name="basename"
        value="validation" />
</bean>

我还有MVCServlet。xml在web中定义。xml格式:

    <!-- // MVC // -->
<servlet>
    <servlet-name>mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/spring/mvc-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>mvc</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

在我的@controller中,我使用bean messageSource的方式如下:

@Controller
@RequestMapping( value = "/admin/su" )
public class AdminSuController {
private final Logger LOG = LoggerFactory.getLogger( getClass() );
    .....
    @Autowired
    private ResourceBundleMessageSource messageSource;

当我在MVCServlet中定义bean时。xml-只要我将其移动到(正如我理解servletContext的根上下文)ApplicationContext,就没有问题。xml(我这样做是为了在REST-servlet.xml和其他@controller-s中使用这个bean),我得到:

#19:41:26.594 [localhost-startStop-1] ERROR 

共有1个答案

岑驰
2023-03-14

谢谢https://stackoverflow.com/users/2843242/oerlien 将messageSource移动到applicationContext会导致默认messageSource在dispatcher servlet上下文中不可见

In your applicationContext.xml:

<bean id="baseMessageSource" class="org.springframework...YourMessageSourceClass">
   ...
</bean>

In your xxx-servlet.xml:

<bean id="messageSource" parent="baseMessageSource" />
 类似资料:
  • 我已经浏览了http://docs.spring.io/spring-data/redis/docs/current/reference/html/,对我来说没有什么新内容。我想知道我错过了什么,我将感谢任何投入。 我使用Spring Data Redis 1.7.2.Release,Spring Boot 1.3.6.Release

  • 我正在用Spring Boot实现Rest API。因为我的实体类来自另一个包,所以我必须用注释来指定它。另外,我使用来指定定义JPA存储库的包。下面是我的项目的样子: 在我的controller类中,我有一个SeqService对象Autowired。 是一个接口,我从它为创建了一个Bean类。在中,我对JPA存储库进行了描述: 我不明白这个错误。和排位赛bean有什么关系?

  • 我正在写一个Spring MVC,并坚持以下错误: 18:34:44999警告[org.springframework.web.context.support.XmlWebApplicationContext](MSC服务线程1-1)在上下文初始化过程中遇到异常-取消刷新尝试:org。springframework。豆。工厂BeanCreationException:创建名为“org”的bean时

  • 问题内容: 我已经用实体管理器配置了两个持久性单元,如下所示: 然后,我将事务管理器配置为 我最初只有一个配置,它被称为“ transactionManager”。Addint一个附加的持久性单元似乎会生成错误。我不明白的一件事,如果我配置了两个持久性单元(每个持久性单元用于一个单独的数据库),是否还需要为每个数据源配置一个单独的实体管理器和一个事务管理器? 我得到的错误如下所示:(我搜索了所有文

  • 更多的细节: 我正在尝试创建第一个没有注释的Spring MVC应用程序。下面是我的URL请求'http://localhost:8080/firstspringmvc/welcome.html',它调用了我的代码。 以下是我的错误列表供参考:

  • 我一直在尝试使用Spring3.1的bean定义概要文件和嵌套bean。我希望可以根据活动配置文件定义不同的bean。请考虑以下非常简化的示例,使我的Spring上下文包含如下内容 我得到以下错误: 线程“main”org.springframework.beans.factory.BeanCreationException中出现异常:创建类路径资源[applicationcontext.xml]