那些年、一起追过的Spring--(7)----如何让Servlet被Spring框架管理
来源:互联网 发布:apache webservice 编辑:程序博客网 时间:2024/06/06 02:14
配置spring.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" 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.xsd"> <context:component-scan base-package="com.gxa.spring.day02"></context:component-scan><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/mybatis" p:username="root" p:password="root" /> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean></beans>
servlet:
package com.gxa.spring.day02;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;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.beans.factory.config.AutowireCapableBeanFactory;import org.springframework.stereotype.Controller;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;@Controller@WebServlet("/user.do")public class UserServlet extends HttpServlet { @Autowired private UserService userService; @Override public void init() throws ServletException { /** * 利用init方法来调用Spring容器BeanFactory 看看UserServlet是否能够通过Spring容器获取对象 */ WebApplicationContext wc = WebApplicationContextUtils .getRequiredWebApplicationContext(getServletContext()); // 通过Web容器去得到BeanFactory对象 AutowireCapableBeanFactory autowireCapableBeanFactory = wc .getAutowireCapableBeanFactory(); autowireCapableBeanFactory.autowireBean(this); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { userService.getUser(); }}
service:
package com.gxa.spring.day02;public interface UserService { public void getUser();}
package com.gxa.spring.day02;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Service("userService")public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public void getUser() { userDao.getUser(); }}
dao:
package com.gxa.spring.day02;public interface UserDao { public void getUser();}
package com.gxa.spring.day02;import java.util.List;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Repository;@Repository("userDao")public class UserDaoImpl implements UserDao { @Autowired private JdbcTemplate jdbcTemplate; @Override public void getUser() { String sql = "select * from student"; List<Map<String,Object>> list = jdbcTemplate.queryForList(sql); System.out.println(list); }}
配置web.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_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringTest09-Servlet</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></web-app>
运行:
浏览器输入:
http://localhost:8080/SpringTest09-Servlet/user.do
[{sid=1, sname=asfsaf, t_id=1}, {sid=2, sname=dsagdhfa, t_id=2}, {sid=2, sname=ad, t_id=1}]
阅读全文
0 0
- 那些年、一起追过的Spring--(7)----如何让Servlet被Spring框架管理
- 那些年、一起追过的Spring--(4)----注解
- 那些年、一起追过的Spring--(5)----AOP
- 那些年、一起追过的Spring--(6)----Spring JDBC框架操作数据库(JdbcTemplate的使用)
- 那些年、一起追过的Spring--(1)----初识Spring
- 那些年、一起追过的Spring--(2)----IOC、两种依赖注入方式
- 那些年我们一起一步一步追过的SSM框架
- 那些年、一起追过的Spring--(3)----Bean的配置项及作用域,Bean装配的生命周期,配置集合对象
- 在servlet中如何使用被Spring管理的service
- 《那些年,我们一起追过的女孩》观后感
- 那些年,我们一起追过的女孩~~
- 那些年,我们一起追过的VC++ 6.0/MFC
- 那些年,我们一起追过的球队
- VAVA:那些年,我们一起追过的ZERO
- 那些年,我们一起追过的Code......
- 那些年,我们一起追过的Code......
- 那些年,我们一起追过的C++
- 那些年,我们一起追过的Linux
- linux常用shell命令
- spark中的共享变量
- 运算符
- javascript实现私有属性与实例属性
- 【C++】int转换为string的两种方法(to_string、字符串流)
- 那些年、一起追过的Spring--(7)----如何让Servlet被Spring框架管理
- ionic3 ionic cordova build ios报错 Cannot read property 'mergeAndUpdateDir' of undefined
- Ubuntu下使用virt-manager创建虚拟机
- Sring Cloud 的 Eureka 安全认证
- USB学习笔记连载(二十一):CY7C68013A进行数据传输(一)
- 树莓派(一)——系统烧录
- eclipse, Log4j配置(真心的详细~)
- 【TX1学习与开发系列】(三)Kintinuous移植到TX1的一些问题总结
- 连接redis,报错Permission denied