1
<?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"
  xsi:schemaLocation="
   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:annotation-config /> 
<context:property-placeholder location="classpath*:*.properties" />
<context:component-scan base-package="your.intermedix" />


<!-- Turn on @Autowired, @PostConstruct etc support -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

<bean id="icontact" class="your.intermedix.services.IContact"/>
<bean id="MyVaadinApplication" class="your.intermedix.MyVaadinApplication"/>
<bean id="ContactSerImpl" class="your.intermedix.services.ContactSerImpl"/>

    <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>   
</beans>

Now when i try to call the interface method, it does not... i dont know. I followed this post completely and i am not sure why its not happening.

How does autowiring work in Spring?

I did place my @Controller and @Service tags to respective classes and used @Autowire annotation like this.

@Autowired
private transient IContact icontact;

Now when i try to call my icontact.methodname() it does not work.

My Interface

package your.intermedix.services;

import your.intermedix.domain.Contact;

public interface IContact {
    public void saveContact(Contact contact);
    public void hello();

}

This is Service class

package your.intermedix.services;

import org.hibernate.SessionFactory;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

import your.intermedix.domain.Contact;
import your.intermedix.services.IContact;

@Service
public class ContactSerImpl implements IContact {

    private HibernateTemplate hibernateTemplate;

        public void setSessionFactory(SessionFactory sessionFactory) {
            this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

        public void saveContact(Contact contact) {
            System.out.println("Hello Guru");
            //System.out.println(contact);
            //hibernateTemplate.saveOrUpdate(contact);
        }

        public void hello() {
            System.out.println("Hello Guru");
        }
}

Now my Actual implementation class.

@SuppressWarnings("serial")
@Configurable(preConstruction = true)
@Controller
public class MyVaadinApplication extends Application implements Button.ClickListener
{
    @Autowired
private transient IContact icontact;

..........................
...................

public void buttonClick(ClickEvent event) {

            if (event.getSource() == save) {
                try {
                    form.commit();
                    icontact.hello();
                    icontact.saveContact(contact);
                }
                 catch (Exception e)    {

                 }
                }
            }
        }
Community
  • 1
  • 1
theJava
  • 14,620
  • 45
  • 131
  • 172
  • 1
    What does "not work" look like? – duffymo Jan 01 '11 at 00:56
  • I am not able to call my methodname which is inside the interface – theJava Jan 01 '11 at 01:02
  • Did Spring load application context from your xml successfully? Your icontact bean isn't a concrete class but rather just an interface. – gigadot Jan 01 '11 at 02:09
  • should i refer a class and not an interface and yes the appcontext did load – theJava Jan 01 '11 at 02:47
  • 1
    two things - "I am not able to".. does not give any information about that problem. Is it an exception? Is it a compile-time error? What is it exactly.. And second - you don't call method names. You call methods. – Bozho Jan 01 '11 at 12:51

2 Answers2

3
<bean id="icontact" class="your.intermedix.services.IContact"/>

instead of interface, you need to give the implementation in the xml (you may wanna change the id names at your will)

<bean id="contactService" class="your.intermedix.services.ContactSerImpl"/>

In the application class you dont need to change anything, as you are using the interface already and autowired it.

fmucar
  • 14,361
  • 2
  • 45
  • 50
2

You realize, of course, that you need to put a concrete implementation of the interface as the class for that bean, right? Looks like you simply included the interface as the class, which is quite incorrect. Pick an implementation of some kind and it might work better.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • I could not get you sir, could you show me an example... i have updated my post where i am implementing the interface... – theJava Jan 01 '11 at 01:02