簡體   English   中英

Spring無法從XML讀取Bean屬性

[英]Spring unable to read bean property from XML

我在從bean配置XML中設置成員值時遇到麻煩。 我將其簡化為我能做到的最簡單的用例。 這里的課程實際上並不打算做任何事情。 我注意到從未設置過文本值(總是為null),所以我在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";
}
}

根context.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>

看起來好像沒有加載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

可能有兩個創建的FoobarService實例。 一個在servlet的應用程序上下文和主應用程序上下文中。 移動

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

到servlet-context.xml,以便通過組件掃描創建的控制器可以看到在同一上下文中創建的FooBarService實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM