【问题标题】:Spring unable to read bean property from XMLSpring 无法从 XML 读取 bean 属性
【发布时间】:2013-11-14 15:21:59
【问题描述】:

我在从我的 bean 配置 XML 设置成员值时遇到问题。我已将其归结为最简单的用例;这里的课程并不打算实际做任何事情。我注意到文本值从未被设置(总是为空),所以我在 setter 方法上打了一个@Required。这会产生 BeanCreationException。详情在这里:

https://gist.github.com/arwagner/7268989

服务类:

@Service
public class FoobarService {
private static final Logger logger = LoggerFactory.getLogger(FoobarService.class);
private String text;

public void report() {
    logger.info("text: " + getText());
}

public String getText() {
    return text;
}

@Required
public void setText(String text) {
    logger.info("setText called");
    this.text = text;
}
}

控制器类:

@Controller
public class HomeController {
@Autowired
private FoobarService foobarService;

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {    
    foobarService.report();
    return "home";
}
}

根上下文.xml:

...

<bean id="foobarService" class="com.agilex.onboardservices.services.FoobarService">
    <property name="text" value="foobar"/>
</bean>
...

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false">

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/root-context.xml</param-value>
</context-param>

<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Processes application requests -->
<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

servlet-context.xml:

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

<annotation-driven />
<resources mapping="/resources/**" location="/resources/" />

<beans:bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <beans:property name="mediaTypes">
        <beans:map>
            <beans:entry key="html" value="text/html" />
            <beans:entry key="json" value="application/json" />
        </beans:map>
    </beans:property>
    <beans:property name="viewResolvers">
        <beans:list>
            <beans:bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
                <beans:property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
                <beans:property name="prefix" value="/WEB-INF/views/" />
                <beans:property name="suffix" value=".jsp" />
            </beans:bean>
        </beans:list>
    </beans:property>
</beans:bean>

<context:component-scan base-package="com.agilex.onboardservices" />
</beans:beans>

【问题讨论】:

  • 你能展示你的整个上下文吗?
  • 你的意思是xml文件?
  • 是的。还有你的 web.xml。
  • 上下文xml中唯一的其他bean是我的数据库连接bean,带有用户名和密码;对不起,我不会发布那个。不过,可以说,数据库连接工作正常。不过,我会发布 web.xml 文件
  • 你在哪里引用/导入根上下文?

标签: java xml spring javabeans


【解决方案1】:

可能创建了两个 FoobarService 实例。一个在 servlet 的应用程序上下文和主应用程序上下文中。移动

<bean id="foobarService" class="com.agilex.onboardservices.services.FoobarService">
  <property name="text" value="foobar"/>
</bean>

到 servlet-context.xml 以便通过组件扫描创建的控制器可以看到在同一上下文中创建的 FooBarService 实例。

【讨论】:

  • 哇,这确实有效。我完全不确定我是否理解为什么,但确实如此。谢谢!
  • 好的,我一直在做一些阅读,我理解两个不同定义的概念。所以 servlet 上下文中的 bean 定义完全覆盖了根上下文中的定义,即使它们不太具体?
【解决方案2】:

看起来没有加载 Spring 上下文。您可以通过以下几种方式做到这一点:

<servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

那么 WEB-INF 需要一个 myservlet-servlet.xml 与您的配置。

或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>

您需要在 WEB-INF 中使用applicationContext.xml。

【讨论】:

  • 我认为可能是这种情况,但我在同一个 XML 文件中配置了数据库连接 bean,而且似乎工作正常。
  • 我添加了我的 web.xml 文件,其中包含对我引用的 root-context.xml sn-p 的引用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-24
  • 2020-11-24
  • 1970-01-01
  • 2016-08-12
相关资源
最近更新 更多