簡體   English   中英

自動裝配不起作用

[英]Autowiring does not work

<?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>

現在,當我嘗試調用接口方法時,它不...我不知道。 我完全遵循了這篇文章,我不確定為什么它沒有發生。

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

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

而不是接口,您需要在xml中提供實現(您可以隨意更改ID名稱)

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

在應用程序類中,您無需更改任何內容,因為您已經在使用該接口並對其進行自動布線。

當然,您意識到需要將接口的具體實現作為該bean的類,對嗎? 看起來您只是將接口包含為類,這是非常不正確的。 選擇某種實現,可能會更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM