<?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>
现在,当我尝试调用接口(interface)方法时,它并没有...我不知道。我完全遵循了这篇文章,我不确定为什么它没有发生。
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) {
}
}
}
}
最佳答案
<bean id="icontact" class="your.intermedix.services.IContact"/>
您需要在 xml 中提供实现而不是接口(interface)(您可能想随意更改 id 名称)
<bean id="contactService" class="your.intermedix.services.ContactSerImpl"/>
在应用程序类中,您不需要更改任何内容,因为您已经在使用接口(interface)并 Autowiring 它。
关于java - Autowiring 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4572850/