我正在使用springframework 3.2和jboss server 7.1。 尝试使用jpa2(hibernate提供程序,mysql)和spring mvc设置一个简单的spring应用程序。
我使用一个简单的DAO,我在家庭控制器中注入自动注释。
这个dao类只有一个字段(em)for persistentContext被注入,它可能更简单,我不使用persistence.xml,因为从版本3.1弹出并且不需要一个.....注意包被扫描正确
我已尝试过网上发布的其他解决方案(包括此处)...
但我得到这个错误:
org.springframework.beans.factory.BeanCreationException:无法 autowire字段: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.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522) org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:295) org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607) org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598) org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661) org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517) org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458) org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138) javax.servlet.GenericServlet.init(GenericServlet.java:242) org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:671) org.apache.tomcat.util.net.JIoEndpoint $ Worker.run(JIoEndpoint.java:930) java.lang.Thread.run(Thread.java:722)
我的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";
}
}
答案 0 :(得分:0)
您是否有一个persistence.xml,您已在其中定义了持久性单元?
答案 1 :(得分: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>
答案 2 :(得分: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上声明的名称