Reputation: 15044
Here is my 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>
My Service class
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");
}
}
My Frontend
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) {
}
}
}
}
The Error below:
2011-01-04 21:57:46.331:WARN::Failed startup of context org.mortbay.jetty.plugin.Jetty6PluginWebAppContext@14ce5eb{/sampleproject,file:/C:/projects/sampleproject/src/main/webapp/;file:/C:/projects/sampleproject/target/sampleproject-1.0/;} org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myVaadinApplication': Unsatisfied dependency expressed through bean property 'user': No unique bean of type [java.lang.Object] is defined: expected single matching bean but found 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] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1119)
Upvotes: 2
Views: 3027
Reputation: 299198
I think you are doing several things multiple times:
This:
<context:annotation-config/>
already includes this:
<!-- Turn on @Autowired, @PostConstruct etc support -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
Remove the <bean>
definitions and try again, perhaps that's the reason.
Reference: Annotation-based container configuration
Update: about your new error:
HibernateDaoSupport
needs either a HibernateTemplate
or a SessionFactory
as dependency. You have a SessionFactory
, but Spring can't wire it because by default, Spring doesn't autowire properties, so you need to either manually wire the property or set <beans default-autowire="byType">
(if you use byName
, you will have to change your SessionFactory bean definition to id="sessionFactory"
)
Upvotes: 2
Reputation: 242786
The error you get is caused by the fact that HibernateDaoSupport
doesn't autowire SessionFactory
by default, so you need to configure injection of SessionFactory
explicitly, like this:
@Service @Transactional
public class ContactSerImpl extends HibernateDaoSupport implements IContact {
@Autowired
public ContactSerImpl(SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
...
}
Upvotes: 3