简体   繁体   中英

Null pointer exception for Autowired object in Spring

I'm building a new project for SOAP web services. Earlier I was using JDBC layer for opening and closing connections. Now, I'm converting it into Spring with JDBC template. I've configured all the layers and annotated the components. When I try to use the dao bean in my service impl class, it throws me null pointer exceptions

@Service
@WebService
public interface Transaction { // Web methods here for SOAP Web service
}

Impl class

@Component
@WebService
public class TransactionImpl implements Transaction{        

    @Autowired
    BBDao dao;  --> This is coming as null when I use it in the method

}

BBDao interface is as follows

public interface BBDao { /* Methods in it */ }

The implementation class which is implementing BBDao interface is

public class BBDaoImpl extends JdbcDaoSupport implements BBDao {    

@Autowired
ServletContext ctx; 

@Autowired
DataSource dataSource;

// Methods overriding here 

}

Servlet defined in web.xml

<servlet>
    <servlet-name>spring-web</servlet-name>
    <servlet-class>
                org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring-web</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

And finally the spring-web-servlet.xml is

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:component-scan base-package="com.bb.controller,com.bb.dao,com.bb.service" />
<context:property-placeholder location="classpath:datasource-cfg.properties" />


<bean id="bbDAO" class="net.bb.dao.BBDaoImpl">
    <property name="dataSource" ref="dataSource" />
</bean> 

<!-- AS400 Data source Bean -->
<bean id="dataSource"
     class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName" value="com.ibm.as400.access.AS400JDBCDriver" />
    <property name="url" value="${as400.url}" />
    <property name="username" value="${as400.username}" />
    <property name="password" value="${as400.password}" />
</bean>

<mvc:annotation-driven />

</beans>

The BBDao bean object is coming as null.

Is there any mistake in my configuration? Any advice would be greatly appreciated.

PS : I have followed other posts as well, as most of the posts talk about component scan only and the packages are correct

We cant autowire an interface without Implementation as we cant create instance for interface. But you can try the ones below and see if it works.

Few things what you could do.

Add to your spring-webservlet.xml

And also refer this

user annotation like @ConditionalOnMissingBean(BBDao.class)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM