gpt4 book ai didi

java - 将 *-servlet.xml 与 applicationContext.xml 混合

转载 作者:行者123 更新时间:2023-12-04 06:02:54 25 4
gpt4 key购买 nike

我有一个现有的 Spring 应用程序,它执行一些服务器端处理。我正在尝试为这个特定的应用程序创建一个 webapp,并选择 SpringMVC 来实现我的目的。

我创建了一个显示 Controller ,如下所示:

@Controller
@RequestMapping("/items")
public class ItemDisplayController {
private static final Logger LOGGER = Logger.getLogger(ItemDisplayController.class);
private static final String ITEMS_REDIRECT = "redirect:/item/items";

@Autowired
private ItemDisplay itemDisplay;

@RequestMapping
public String listItems(ModelMap model) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("List all items");
}

List<ItemDetail> itemDetails = itemDisplay.getAllItems();
model.addAttribute("itemDetails",itemDetails);
return "items";
}
}

我已经有一个包含以下定义的 applicationContext 文件:
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<bean id="props" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<array>
<value>classpath:item.properties</value>
<value>file:#{systemEnvironment['ITEM_HOME']}/item.properties</value>
<value>file:#{systemProperties['ITEM_HOME']}/item.properties</value>
</array>
</property>
<property name="ignoreResourceNotFound" value="true"/>
</bean>


<bean id="itemDisplay" class="com.acme.itemDisplayImpl">
<property name="itemDisplayDAO" ref="jdbcItemDisplayDAO"/>
</bean>

<bean id="jdbcItemDisplayDAO" class="com.acme.database.dao.JdbcItemDisplayDAO">
<property name="dataSource" ref = "dataSource"/>
</bean>


<bean id="realDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="#{props['itemds.jdbc.driver']}"/>
<property name="url"><value><![CDATA[#{props['itemds.jdbc.url']}]]></value></property>
<property name="username" value="#{props['itemds.username']}"/>
<property name="password" value="#{props['itemds.password']}"/>
<property name="testOnBorrow" value="#{props['itemds.test.on.borrow']}"/>
<property name="testWhileIdle" value="#{props['itemds.test.while.idle']}"/>
<property name="poolPreparedStatements" value="#{props['itemds.pool.prepared.statements']}"/>
<property name="validationQuery" value="#{props['itemds.validation.query']}"/>
<property name="validationQueryTimeout" value="#{props['itemds.validation.query.timeout']}"/>
<property name="timeBetweenEvictionRunsMillis" value="#{props['itemds.time.between.eviction.runs.millis']}"/>
<property name="maxActive" value="#{props['itemds.max.active']}"/>
<property name="maxIdle" value="#{props['itemds.max.idle']}"/>
<property name="initialSize" value="#{props['itemds.initial.size']}"/>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
<property name="targetDataSource" ref="realDataSource"/>
</bean>

</beans>

在我的 *servlet.xml 中,我定义了 ViewResolver,如下所示:
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<mvc:annotation-driven/>
<context:annotation-config/>
<context:component-scan base-package="com.acme.item"/>

<bean id="primaryViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
<property name="contentType" value="text/html; charset=UTF-8"/>
<property name="order" value="1"/>
</bean>
</beans>

在 web.xml 中,我定义了上下文参数:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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_2_5.xsd"
version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>items</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>items</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

当我运行我的应用程序时,itemDisplay 没有连接,在调试器上它显示为空值。

有人可以指出我在这里可能做错了什么吗?在理想情况下,我假设在 Controller 中使用 @Autowired 注释 itemDisplay 将有助于解决接口(interface)的实现。

最佳答案

我唯一能想到的是,如果找不到applicationContext.xml,ContextLoaderListener不会提示,让我们试试这个添加类路径*:applicationContext.xml 还要确保 applicationContext.xml 是服务器的运行时类路径

这是一篇很好的文章,可以很好地了解 spring classpath资源。

关于java - 将 *-servlet.xml 与 applicationContext.xml 混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8729326/

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