3
DAOクラスのAutowiredがTestクラスでうまく動作するが、サーブレットではうまく動作しない理由がわかりません。 web.xmlファイルは、私がログで見ることができるように正しくコンテキスト設定をロードするので、問題は私のapplicationContextファイルになければなりません。サーブレットでAutowireが動作しない
これは私のサーブレットです:
@Controller
public class TestAutowired extends HttpServlet {
private static final long serialVersionUID = 1L;
@Autowired
private IUserDao uDao;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
uDao.getUserById(1);
}
}
そして、これは私のApplicationContext(configuration.xmlの)です:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring configuration -->
<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:jee="http://www.springframework.org/schema/jee"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation=...>
<context:component-scan base-package="com.firststepteam.dao">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<context:component-scan base-package="com.firststepteam.services">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<context:component-scan base-package="com.firststepteam.servlet">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- View resolver -> JSP -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- PropertyPlaceholderConfigurer -->
<bean id="placeHolder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:hostingData.conf" />
</bean>
<!-- DataSource -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.db.driverClassName}" />
<property name="url" value="${jdbc.db.url}" />
<property name="username" value="${jdbc.db.username}" />
<property name="password" value="${jdbc.db.password}" />
</bean>
<!-- JPA E HIBERNATE -->
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="MYSQL" />
<property name="generateDdl" value="true" />
</bean>
<!-- ENTITY MANAGER FACTORY -->
<bean id="dbEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="fs_db" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="dbEntityManagerFactory" />
</bean>
<bean class = "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
</beans>
任意のヒント?
私は何をすべきですか?私はweb.xmlに変更を加える必要がありますか? – Fabio
生のサーブレットではなく、春のコントローラを使用します。 Spring DispatcherServletを使用して適切に委譲します。また、DAOはサーブレットに属していません。 Springイディオムには、DAOを所有し、トランザクションと作業単位を知っているサービスレイヤがあります。 DAOをサービスに、サービスをコントローラに接続し、Spring DispatcherServletにリクエストをルーティングさせます。 – duffymo