Entrarei em um projeto onde preciso usar Spring 3 com Servlet e JDBC, e estou desenvolvendo uma aplicação para estudo. Ao carregar o Servlet, o Spring não injeta o service, dando nullPointer. Vou postar os arquivos e se alguem puder ajudar agradeço!
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>TestServlet</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>artistaServlet</servlet-name>
<servlet-class>br.com.estudo.servlets.ArtistaServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>artistaServlet</servlet-name>
<url-pattern>/artistaServlet</url-pattern>
</servlet-mapping>
</web-app>
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="br.com.estudo" />
<context:annotation-config />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/campeonato" />
<property name="username" value="root" />
<property name="password" value="1234" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven/>
</beans>
package br.com.estudo.servlets;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import br.com.estudo.beans.Artista;
import br.com.estudo.service.ArtistaService;
/**
* @author Shasqua
*
*/
@Controller("artistaServlet")
public class ArtistaServlet extends HttpServlet {
@Autowired
private ArtistaService artistaService;
private Artista artista = new Artista();
/**
*
*/
private static final long serialVersionUID = -696273495882464291L;
public void service(HttpServletRequest request,
HttpServletResponse response) {
RequestDispatcher rd = null;
try {
artistaService.insert(artista);
rd = request.getRequestDispatcher("/sucesso.jsp");
} catch (Exception e) {
e.printStackTrace();
rd = request.getRequestDispatcher("/failure.jsp");
}
try {
rd.forward(request, response);
} catch (ServletException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ArtistaService getArtistaService() {
return artistaService;
}
public void setArtistaService(ArtistaService artistaService) {
this.artistaService = artistaService;
}
}
package br.com.estudo.service.impl;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import br.com.estudo.beans.Artista;
import br.com.estudo.dao.ArtistaDao;
import br.com.estudo.service.ArtistaService;
@Service("artistaService")
public class ArtistaServiceImpl implements ArtistaService,Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
@Autowired
private ArtistaDao artistaDao;
public void insert(Artista artista){
artistaDao.insert(artista);
}
public void update(Artista artista){
artistaDao.updtae(artista);
}
public void delete(Artista artista){
artistaDao.delete(artista);
}
public Artista findArtista(Artista artista){
return artistaDao.findArtista(artista);
}
}
package br.com.estudo.dao.impl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import br.com.estudo.beans.Artista;
import br.com.estudo.dao.ArtistaDao;
/**
* @author Shasqua
*
*/
@Repository
public class ArtistaDaoImpl implements ArtistaDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void delete(Artista artista) {
String sql = "DELETE FROM Artista WHERE idArtista = ?";
jdbcTemplate.update(sql, new Object[] { artista.getIdArtista()});
}
@SuppressWarnings("unchecked")
@Override
public Artista findArtista(Artista artista) {
String sql = "SELECT * FROM Artista WHERE idArtista = ?";
List resultados = jdbcTemplate.query(sql, new Object[]{artista.getIdArtista()},
new RowMapper() {
public Object mapRow(ResultSet rs, int i) throws SQLException {
Artista art = new Artista();
art.setIdArtista(rs.getInt("idArtista"));
art.setNome(rs.getString("nome"));
art.setGenero(rs.getString("genero"));
art.setNacionalidade(rs.getString("nacionalidade"));
return art;
}
});
Artista art = new Artista();
if (resultados.size() > 0) {
art = (Artista) resultados.get(0);
} else {
art.setIdArtista(artista.getIdArtista());
}
return art;
}
@Override
public void insert(Artista artista) {
String sql = "INSERT INTO Artista(nome,genero,nacionalidade) VALUES(?,?,?)";
jdbcTemplate.update(sql, new Object[] { artista.getNome(),
artista.getGenero(), artista.getNacionalidade() });
}
@Override
public void updtae(Artista artista) {
String sql = "UPDATE Artista SET nome = ?, genero= ?, nacionalidade= ? WHERE idArtista = ?";
jdbcTemplate.update(sql, new Object[]{artista.getNome(), artista.getGenero(), artista.getNacionalidade(),artista.getIdArtista()});
}
}