jboss サーバー 7.1 で springframework 3.2 を使用しています。jpa2(hibernate provider, mysql)とspring mvcでシンプルなSpringアプリをセットアップしようとしています。
ホームコントローラーに自動配線された注釈を挿入する単純なDAOを使用します。この dao クラスには、persistenceContext を注入するためのフィールド (em) が含まれているだけです。これは簡単なことではありません。バージョン 3.1 以降の spring では persistence.xml は必要ないため、persistence.xml は使用しません。パッケージがスキャンされることに注意してください。適切に、Webに投稿された他のソリューション(ここを含む)からすべてを試しました...しかし、次のエラーが発生します:
org.springframework.beans.factory.BeanCreationException: フィールドを自動配線できませんでした: com.stko.home.model.daos.UserDao com.stko.home.controllers.HomeController.userDao; ネストされた例外は org.springframework.beans.factory.BeanCreationException: 'userDao' という名前の Bean の作成中にエラーが発生しました: 永続化依存関係の注入に失敗しました。ネストされた例外は org.springframework.beans.factory.NoSuchBeanDefinitionException: タイプ [javax.persistence.EntityManagerFactory] の一意の Bean が定義されていません: 単一の Bean が必要ですが、0 が見つかりました: org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject (AutowiredAnnotationBeanPostProcessor.java:514) org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) org.springframework.beans.factory.
私のapplicationContext.xml:
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mmydb"/>
<property name="username" value="user"/>
<property name="password" value="1234"/>
</bean>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" >
<property name="dataSource" ref="dataSource"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
</property>
<property name="packagesToScan" value="com.stko.home.model.entities"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.max_fetch_depth">3</prop>
<prop key="hibernate.show.sql">true</prop>
<prop key="hibernate.connection.charSet"> UTF-8</prop>
</props>
</property>
</bean>
<!-- tell spring to use annotation based congfigurations -->
<context:annotation-config />
<!-- scan for beans -->
<context:component-scan base-package="com.stko.home.model.daos"/>
<!-- to recognize persistnce annotations like PersistenceContext -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="databaseProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:database.properties"/>
</bean>
私のサーブレット-context.xml:
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.stko.home"/>
私のweb.xml:
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/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/spring/appServlet/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>
私のユーザーダオ:
@Service("userDao")
@Repository
public class UserDao implements Serializable {
@PersistenceContext
private EntityManager em;
@Transactional
public void saveUser(User user) {
if(user!=null)
em.persist(user);
}
}
私のエンティティクラスのユーザー:
@Entity
@Table(name = "user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "userid")
private Integer userid;
@Column(name="username")
private String username;
public User(){}
}
私の典型的なホームコントローラー:
@Controller
public class HomeController {
.....
@Autowired
UserDao userDao;
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {
.....
User newUser = new User();
newUser.setName("blabla");
userDao.saveUser(newUser);
return "home";
}
}