0

我正在使用 springframework 3.2 和 jboss server 7.1 。尝试使用 jpa2(hibernate provider, mysql) 和 spring mvc 设置一个简单的 spring 应用程序。

我使用了一个简单的 DAO,我在家庭控制器中注入了自动装配注释。这个 dao 类只是有一个用于注入 persistentContext 的字段(em),它可能更简单,我不使用persistence.xml,因为从 3.1 版及更高版本的 spring 不需要......请注意,包被扫描正确地,我已经尝试了网络上发布的其他解决方案的所有内容(包括这里)......但我收到了这个错误:


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>

我的 servlet-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";
} 

}
4

3 回答 3

0

问题很可能是servlet-context.xml文件中的这一行:

<context:component-scan base-package="com.stko.home"/>

这实际上是试图在您的 Web 应用程序上下文中再次创建您的 DAO。你应该只有一个过滤器,它只在这里创建你的控制器,这样:

<context:component-scan base-package="com.stko.home">
<context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
</context:component-scan>
于 2013-01-28T15:00:24.950 回答
0

您是否有一个在其中定义了持久性单元的 persistence.xml?

于 2013-01-28T14:45:10.730 回答
0

尝试这个

<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="persistenceUnit"/>
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
</bean>

为您的 EMF bean 定义属性,persistenceUnit 应该是您在 persistence.xml 中声明的名称

于 2014-02-05T22:02:50.293 回答