<?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) {
}
}
}
}
答案 0 :(得分:3)
<bean id="icontact" class="your.intermedix.services.IContact"/>
而不是接口,你需要在xml中给出实现(你可能想要随意更改id名称)
<bean id="contactService" class="your.intermedix.services.ContactSerImpl"/>
在应用程序类中,您不需要更改任何内容,因为您已经使用了界面并自动装配了它。
答案 1 :(得分:2)
当然,您意识到需要将接口的具体实现作为该bean的类,对吧?看起来你只是将接口作为类包含在内,这是非常不正确的。选择某种实现方式,它可能会更好。