Java Spring自动连线服务在部署时设置,但调用时为空
抱歉,如果我的问题搞砸了,我只是刚刚开始配置MVC、Spring等 我遇到的问题是,在我的Spring MVC项目中,当我的应用程序部署时(通过在Spring工具套件中设置断点进行验证),所有的自动连线服务都被成功地自动连线,但当我尝试调用自动连线服务的方法时,我遇到了NullPointerException。我已经查看了所有不同的配置文件,似乎我的servelt上下文、根上下文和web XML文件应该可以(不要引用我的话,它们包含在下面) 我想知道我是否不知何故没有调用在部署时创建的服务实例?似乎奇怪的是,可以成功调用自动连线设置器,但服务最终为空。非常感谢您提供的任何帮助 root-context.xml:Java Spring自动连线服务在部署时设置,但调用时为空,java,spring,model-view-controller,annotations,Java,Spring,Model View Controller,Annotations,抱歉,如果我的问题搞砸了,我只是刚刚开始配置MVC、Spring等 我遇到的问题是,在我的Spring MVC项目中,当我的应用程序部署时(通过在Spring工具套件中设置断点进行验证),所有的自动连线服务都被成功地自动连线,但当我尝试调用自动连线服务的方法时,我遇到了NullPointerException。我已经查看了所有不同的配置文件,似乎我的servelt上下文、根上下文和web XML文件应该可以(不要引用我的话,它们包含在下面) 我想知道我是否不知何故没有调用在部署时创建的服务实例?
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan" value="com.byteslounge.spring.tx.model" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDrive" />
<property name="url"
value="jdbc:oracle:thin:@**********" />
<property name="username" value="*******" />
<property name="password" value="*******" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="dataSource" ref="dataSource" />
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven />
<context:annotation-config />
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<context:component-scan base-package="com.clm.billing" />
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
</beans:beans>
保险注意事项服务:
public interface InsuranceNoteDBService {
void insertNote(BillerNote note);
BillerNote getNoteById(int noteId);
}
保险注意事项:
(设置会话工厂的init()方法也会发生同样的情况)
保险注意事项:
public interface InsuranceNoteDAO {
void insertNote(BillerNote note);
BillerNote getNoteById(int noteId);
}
删除init方法并注释setter:
@Autowired
public void setSessionFactory(SessionFactory factory) {
this.sessionFactory = factory;
}
iNodeDao的二传似乎不正确。它应该被设置为。您是自动生成setter还是手工编写的?@VaibhavRaj-我是手工编写的,而不是让STS生成它,但是名称应该无关紧要,它是用InsuranceNoteDAO实例调用的。@TheNewIdiot-我不必在spring上下文xml中定义bean,它们应该由Spring的预处理器根据给定给类的注释来提取。这是由设置的,您的意思是您使用新操作符创建了InsuranceNoteDao实例并调用setiNodeDao吗?您是否知道自动连线将只在Spring的bean工厂创建的实例上工作?如果您这样定义servlet,它将被servlet容器实例化。谢谢您的建议!正如您所说,我删除了init方法,并用@Autowired注释了setter,但仍然存在与以前相同的问题。
@Service
public class InsuranceNoteDAOImpl implements InsuranceNoteDAO {
public SessionFactory sessionFactory;
@Autowired
public void init(SessionFactory factory) {
setSessionFactory(factory);
}
public void setSessionFactory(SessionFactory factory) {
this.sessionFactory = factory;
}
@Override
public void insertNote(BillerNote note) {
sessionFactory.getCurrentSession().save(note);
}
@Override
public BillerNote getNoteById(int noteId) {
return (BillerNote) sessionFactory.
getCurrentSession().
get(BillerNote.class, noteId);
}
}
public interface InsuranceNoteDAO {
void insertNote(BillerNote note);
BillerNote getNoteById(int noteId);
}
@Autowired
public void setSessionFactory(SessionFactory factory) {
this.sessionFactory = factory;
}