这是我的bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Turn on AspectJ @Configurable support -->
<context:spring-configured />
<context:property-placeholder location="classpath*:*.properties" />
<context:component-scan base-package="your.intermedix"/>
<context:annotation-config/>
<!-- Turn on @Autowired, @PostConstruct etc support -->
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="annotatedClasses">
<list>
<value>your.intermedix.domain.Contact</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/spring"/>
<property name="username" value="monty"/>
<property name="password" value="indian"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="mySessionFactory" />
</bean>
</beans>
我的服务类
package your.intermedix.services;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import your.intermedix.domain.Contact;
import your.intermedix.services.IContact;
@Service
@Transactional
public class ContactSerImpl extends HibernateDaoSupport implements IContact {
public Contact saveContact(Contact contact) {
System.out.println(contact);
getHibernateTemplate().saveOrUpdate(contact);
return contact;
}
public void hello() {
System.out.println("Hello Guru");
}
}
我的前端
package your.intermedix;
import java.util.logging.Logger;
import your.intermedix.domain.Contact;
import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Form;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.data.util.BeanItem;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.UserError;
import com.vaadin.ui.Window;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Controller;
import your.intermedix.domain.ContactValdiation;
import your.intermedix.services.IContact;
/**
* The Application's "main" class
*/
@SuppressWarnings("serial")
@Configurable(preConstruction = true)
@Controller
public class MyVaadinApplication extends Application implements Button.ClickListener
{
@Autowired
private transient IContact icontact;
private Window window;
private Button save = new Button("Save", (ClickListener) this);
private Button cancel = new Button("Cancel", (ClickListener) this);
private Contact contact = new Contact();
private Form form = new Form();
private BeanItem item = new BeanItem(contact);
private GridLayout ourLayout;
public void init(){
loadForm();
}
private void loadForm(){
window = new Window("InterMedix Application");
setMainWindow(window);
final Panel panel = new Panel("Contact Information");
panel.addStyleName("panelexample");
// The width of a Panel is 100% by default, make it
// shrink to fit the contents.
panel.setWidth(Sizeable.SIZE_UNDEFINED, 0);
form.setWriteThrough(false);
form.setInvalidCommitted(false);
form.setImmediate(true);
form.setFormFieldFactory(new ContactValdiation());
form.setRequired(true);
form.setItemDataSource(item);
form.setVisibleItemProperties(new Object[] {"name","lastname","email","designation","date","comments"});
form.setValidationVisibleOnCommit(true);
form.setFooter(new VerticalLayout());
// Have a button bar in the footer.
HorizontalLayout buttons = new HorizontalLayout();
form.getLayout().addComponent(buttons);
buttons.addComponent(save);
buttons.addComponent(cancel);
panel.addComponent(form);
window.addComponent(panel);
}
public void buttonClick(ClickEvent event) {
if (event.getSource() == save) {
try {
form.commit();
icontact.hello();
icontact.saveContact(contact);
}
catch (Exception e) {
}
}
}
}
以下错误:
2011-01-04 21:57:46.331:WARN ::上下文启动失败org.mortbay.jetty.plugin.Jetty6PluginWebAppContext@14ce5eb {/ sampleproject,file:/ C:/ projects / sampleproject / src / main / web应用程序/;文件:/ C:/projects/sampleproject/target/sampleproject-1.0/;} org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为'myVaadinApplication'的bean时出错:通过bean属性'user'表示的不满意的依赖关系:没有定义类型为[java.lang.Object]的唯一bean:期望的单个匹配bean但是找到了15:[org.springframework.context.config.internalBeanConfigurerAspect,org.springframework.beans.factory.config.PropertyPlace holderConfigurer#0,contactSerImpl,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework。 context.annotation.CommonAnnotationBea nPostProcessor#0,mySessionFactory,myDataSource,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.config.internalTransactionAdvisor,txManager,messageSource,applicationEventMulticaster] 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1119)
答案 0 :(得分:3)
您得到的错误是由于HibernateDaoSupport
默认情况下不会自动装配SessionFactory
,因此您需要明确配置SessionFactory
的注入,如下所示:
@Service @Transactional
public class ContactSerImpl extends HibernateDaoSupport implements IContact {
@Autowired
public ContactSerImpl(SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
...
}
答案 1 :(得分:2)
我认为你多次做了几件事:
此:
<context:annotation-config/>
已包含此内容:
<!-- Turn on @Autowired, @PostConstruct etc support -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
删除<bean>
定义并重试,也许这就是原因。
参考:Annotation-based container configuration
更新:关于您的新错误:
HibernateDaoSupport
需要HibernateTemplate
或SessionFactory
作为依赖关系。你有一个SessionFactory
,但Spring无法连接它,因为默认情况下,Spring不会自动装配属性,所以你需要手动连接属性或设置<beans default-autowire="byType">
(如果使用{{1} ,你必须将SessionFactory bean定义更改为byName
)