我使用的是带有JBoss服务器7.1的SpringFramework3.2。我试图用jpa2(Hibernate Provider,MySQL)和Spring MVC创建一个简单的Spring应用程序。
我使用一个简单的DAO,并将其与自动生成的注解一起注入到主控制器中。这个Dao类只有一个字段(Em)来注入sistentContext,它再简单不过了,我不使用Persistence.xml,因为3.1版和更高版本的Spring不需要一个.....请注意,包是正确扫描的,我已经尝试了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:未定义类型为[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).持久性.实体管理器工厂]的唯一Bean:应为单个Bean,但找到0:Java org.springFrawork.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服务(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(线程.
我的应用程序上下文.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>
My Servlet-Conext.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>
我的用户Dao:
@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";
}
}
3条答案
按热度按时间r6l8ljro1#
您有在其中定义了持久化单元的Persistence.xml吗?
5cnsuln72#
最有可能的问题是
servlet-context.xml
文件中的以下行:这实质上是试图在您的Web应用程序上下文中再次创建DAO。您应该只有一个过滤器,它只在这里创建您的控制器,这样:
ny6fqffe3#
尝尝这个
定义EMF Bean的属性,PersistenceUnit应该是您在Persistence.xml上声明的名称