抱歉,如果我的问题搞砸了,我刚刚开始配置 MVC、Spring 等......
我遇到的问题是,在我的 Spring MVC 项目中,当我的应用程序部署(通过在 Spring Tool Suite 中设置断点进行验证)时,我的所有 Autowired 服务都已成功自动装配,但是当我尝试调用自动装配服务的方法时,我运行进入 NullPointerException。我已经查看了不同的配置文件,似乎我的 servelt-context、root-context 和 web XML 文件应该没问题(不要引用我的话,它们包含在下面)。
我想知道是否以某种方式我没有调用在部署时创建的服务实例?可以成功调用 Autowired 设置器但随后服务最终为空,这似乎很奇怪。非常感谢您提供的任何帮助!
根上下文.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" />
servelt-context.xml:
<?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>
网页.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>OrderList</display-name>
<servlet-name>OrderList</servlet-name>
<servlet-class>com.clm.billing.Oracle.OrderList</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>OrderList</servlet-name>
<url-pattern>/OrderList</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>EditBilling</display-name>
<servlet-name>EditBilling</servlet-name>
<servlet-class>com.clm.billing.EditBilling</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EditBilling</servlet-name>
<url-pattern>/EditBilling</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>UpdateBilling</display-name>
<servlet-name>UpdateBilling</servlet-name>
<servlet-class>com.clm.billing.UpdateBilling</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UpdateBilling</servlet-name>
<url-pattern>/UpdateBilling</url-pattern>
</servlet-mapping>
</web-app>
InsuranceNoteDBServiceImpl:(这里 setiNoteDAO 是用 iNoteDAO 类的实例调用的,但是当 getNoteById 被调用时,iNoteDAO 为空)
@Service
public class InsuranceNoteDBServiceImpl implements InsuranceNoteDBService {
private InsuranceNoteDAO iNoteDAO;
@Autowired
public void setiNoteDAO(InsuranceNoteDAO iNoteDAO) {
this.iNoteDAO = iNoteDAO;
}
@Override
@Transactional
public void insertNote(BillerNote note) {
iNoteDAO.insertNote(note);
}
@Override
@Transactional
public BillerNote getNoteById(int noteId) {
return iNoteDAO.getNoteById(noteId);
}
}
InsuranceNoteDB服务:
public interface InsuranceNoteDBService {
void insertNote(BillerNote note);
BillerNote getNoteById(int noteId);
}
InsuranceNoteDAOImpl:(设置会话工厂的 init() 方法也会发生同样的事情)
@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);
}
}
保险注意DAO:
public interface InsuranceNoteDAO {
void insertNote(BillerNote note);
BillerNote getNoteById(int noteId);
}