我正在尝试集成 Hibernate 和 Struts 2。
这是
applicationContext.xml
:
<bean id="user" class="com.caveofprogramming.service.imp.UserImp" />
<bean id="userSpringAction" class="com.caveofprogramming.actions.UserSpringAction">
<property name="user" ref="user" /> </bean>
<bean id="customerDAO" class="com.caveofprogramming.entity.CustomerDAO">
<property name="transactionManager" ref="transactionManager" /> </bean>
<bean id="customerEntity" class="com.caveofprogramming.entity.Customer" />
<bean id="customerAction" class="com.caveofprogramming.actions.CustomerAction">
<property name="customerEntity" ref="customerEntity" />
<property name="customerDAO" ref="customerDAO" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/jdbc.properties" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
p:password="${jdbc.password}" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${jdbc.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
我已经有了我的
jdbc.properties
并将我的 Customer
保存在 DAO 中:
CustomerDAO
:
public class CustomerDAO {
@Autowired
private SessionFactory sessionFactory;
public void persistAuthor(Customer customer) {
sessionFactory.getCurrentSession().save(customer);
}
}
这是
Entity
课程:
@Table(name="customer")
@Entity
public class Customer {
@Id
@GeneratedValue
private Integer id;
private String name;
private String password;
public Integer getId() {
return id;
}
//Getter/Setters//
}
这是我的
Action
班级:
public class CustomerAction extends ActionSupport {
private static final Logger logger = Logger.getLogger(CustomerAction.class);
Customer customer;
CustomerDAO customerdao;
private Integer id;
private String name;
private String password;
//GETTERS/SETTERS//
@Action(value="/customer", results={
@Result(name="success",location="/customerSuccess.jsp"),
})
public String execute() throws Exception {
logger.debug("Hello");
customer.setId(getId());
customer.setName(getName());
customer.setPassword(getPassword());
customerdao.persistAuthor(customer);
return SUCCESS;
}
但是,它向我展示了这个异常
例外:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is
java.lang.IllegalStateException: AnnotationTransactionAttributeSource is only available on Java 1.5 and higher
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:420)
TxNamespaceUtils
这可能是一个错误,也报告为 Bug 1090968。不建议您在最新的 Java 8 中使用旧库。尝试升级一些库,尤其是 Spring 和 Hibernate,并更改配置以不使用已弃用的 API。