2011-01-01 13 views
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> 

私がインターフェイスメソッドを呼び出そうとすると、わかりません。私はこの投稿に完全に従って、私はなぜそれが起こっているのか分からない。オートワイヤリングが動作しない

How does autowiring work in Spring?

私は、それぞれのクラスに私の@Controller@Serviceタグを配置しなかったし、このよう@Autowireアノテーションを使用。

@Autowired 
private transient IContact icontact; 

私がicontact.methodname()に電話しようとすると、機能しません。

マイインタフェース

package your.intermedix.services; 

import your.intermedix.domain.Contact; 

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

} 

これは、サービスクラス

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"); 
     } 
} 

今私の実際の実装クラスです。

@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) { 

       } 
       } 
      } 
     } 
+1

"機能しない"とはどういうものですか? – duffymo

+0

インタフェース内にあるメソッド名を呼び出すことができません – theJava

+0

XMLのアプリケーション・コンテキストをXMLにロードしましたか? icontact Beanは具体的なクラスではなく、単なるインタフェースです。 – gigadot

答えて

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

、あなたは何も変更する必要がいけないアプリケーションクラスで

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

(あなたはあなたの意志でID名を変更したい場合があります)XMLでの実装を与える必要があり、インターフェイスを既に使用していて、それを自動的に使用しています。

+1

あなたは、私がそれらのすべてを与えたbeanを見れば theJava

+0

なぜそれでも問題が発生するのですか? – theJava

+0

xml設定ファイルからインターフェイスを削除するだけです。彼らはインターフェイスなので、あなたはconfig xmlにどんなインターフェイスも必要としません。 – fmucar

2

もちろん、そのBeanのクラスとしてインターフェイスの具体的な実装を行う必要があることは理解できますか?インターフェイスがクラスとして含まれているように見えますが、これは間違っています。いくつかの種類の実装を選んで、それはより良く動作するかもしれません。代わりに、インターフェースの

+0

私はあなたが私に例を示すことができませんでした...私は私のインターフェイスを実装している私のポストを更新しました... – theJava