Java 为什么我的Spring@Autowired field num位于非控制器&x27;什么课?
我有这样的服务课:Java 为什么我的Spring@Autowired field num位于非控制器&x27;什么课?,java,spring-mvc,Java,Spring Mvc,我有这样的服务课: @Service public class ReminderServiceImpl implements ReminderService { @Autowired private RemindRepository repository; public Remind save(Remind remind) { return repository.saveAndFlush(remind); } public List<Remind> getAll() {
@Service
public class ReminderServiceImpl implements ReminderService {
@Autowired
private RemindRepository repository;
public Remind save(Remind remind) {
return repository.saveAndFlush(remind);
}
public List<Remind> getAll() {
return repository.findAll();
}
}
My mvc-dispatcher-servlet.xml:
<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:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="server"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<jpa:repositories base-package="server.repository"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="myprovider"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:jboss/TransactionManager"/>
</bean>
我做错了什么 XMLConverter类在哪个包中?如果它不在基本包“服务器”的下方,@Autowired注释将不起作用,因为将不会扫描注释。此类位于“服务器”包内。
@组件是否正确地从Spring包导入?可能不是。。。我刚刚添加了一个注释。如何导入?请检查导入语句以确保导入的是正确的。我以前输入了错误的东西,花了很长时间试图了解发生了什么。嗯。。。我不明白导入是什么意思…@Component annotation刚刚导入:import org.springframework.stereotype.Component;仅此而已……就是这样,这是正确的答案
@Component
public class XMLConverter implements ResourceLoaderAware {
@Autowired
private ReminderService service;
...
public void convertFromXMLToObject() throws XmlPullParserException, IOException {
...
Remind r=new Remind();
r.setTitle(parser.getText());
service.save(r);
...
}
}
<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:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="server"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<jpa:repositories base-package="server.repository"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="myprovider"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
<property name="transactionManagerName" value="java:jboss/TransactionManager"/>
</bean>