Note that there are some explanatory texts on larger screens.

plurals
  1. POAutowired object return null
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/4184159/spring-autowired-in-servlet">Spring @Autowired in Servlet</a><br> <a href="https://stackoverflow.com/questions/13461809/configurating-spring-ioc-with-servlets">Configurating Spring Ioc with Servlets</a> </p> </blockquote> <p>I would like to inject the dao into servlet but it return as null. </p> <p>applicationContext.xml</p> <pre><code>&lt;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:tx="http://www.springframework.org/schema/tx" 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-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"&gt; &lt;!-- Enable @Required @Autowired @PreDestroy @PostConstruct @Resource --&gt; &lt;context:annotation-config /&gt; &lt;!-- Scan class file in class path for annotated component -&gt; @Component, @Repository, @Service, and @Controller --&gt; &lt;context:component-scan base-package="com.breeze.bis.core.service.jdbcTemplate" /&gt; &lt;!-- Enable load time weaving for @Configurable --&gt; &lt;context:load-time-weaver aspectj-weaving="autodetect" /&gt; &lt;!-- --&gt; &lt;tx:annotation-driven proxy-target-class="false" transaction-manager="transactionManager"/&gt; &lt;!-- ==================================================================================================== --&gt; &lt;!-- Alternate method to enable @Autowired --&gt; &lt;bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/&gt; &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"&gt; &lt;property name="jdbcUrl" value="jdbc:mysql://localhost:3306/vbossdb" /&gt; &lt;property name="driverClass" value="com.mysql.jdbc.Driver" /&gt; &lt;property name="user" value="root" /&gt; &lt;property name="password" value="root" /&gt; &lt;/bean&gt; &lt;bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"&gt; &lt;constructor-arg type="javax.sql.DataSource" ref="dataSource"&gt;&lt;/constructor-arg&gt; &lt;/bean&gt; &lt;bean id="genericDaoImpl" class="com.breeze.bis.core.service.jdbcTemplate.GenericDAOImpl"&gt; &lt;property name="jdbcDao" ref="jdbcTemplate"&gt;&lt;/property&gt; &lt;/bean&gt; &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt; &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>web.xml</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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" id="WebApp_ID" version="2.5"&gt; &lt;display-name&gt;BISDAO&lt;/display-name&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;WEB-INF/applicationContext.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;display-name&gt;contextLoaderListener&lt;/display-name&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;/webapp&gt; </code></pre> <p>DAO.java</p> <pre><code>@Component @Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.DEFAULT, rollbackFor={Exception.class, SQLException.class}, timeout=9999) public class GenericDAOImpl implements GenericDAO { private JdbcTemplate jdbcDao; /** * */ public GenericDAOImpl() { } /** * @return the jdbcDao */ public JdbcTemplate getJdbcDao() { return jdbcDao; } /** * @param jdbcDao the jdbcDao to set */ public void setJdbcDao(JdbcTemplate jdbcDao) { this.jdbcDao = jdbcDao; } /* (non-Javadoc) * @see com.breeze.bis.core.service.jdbcTemplate.GenericDAO#createObject(java.lang.String, java.lang.Object) */ @Override @SuppressWarnings("unchecked") public int createObject(String sql, Map&lt;Integer, Object&gt; paramMap) { boolean hasResult = true, hasKey = true; IStatementExecutor stmtExecutor = new UpdateStatementExecutor(hasKey); IExtractor integerExtr = new IntegerExtractor(null, hasResult); GenericDatabaseExecutor executor = new GenericDatabaseExecutor(stmtExecutor, integerExtr); GenericPreparedStatementCreator stmtCreator = new GenericPreparedStatementCreator(hasKey, sql, paramMap); GenericPreparedStatementCallback stmtCallback = new GenericPreparedStatementCallback(executor); return (Integer) this.jdbcDao.execute(stmtCreator, stmtCallback); } } </code></pre> <p>Servlet.java</p> <pre><code>@Configurable public class GenericServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Autowired GenericDAO dao; } </code></pre> <p>Please help. </p> <p>Thanks. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload