`

Spring Bean 注入 Servlet 的方法

 
阅读更多

不使用任何知名MVC框架,仅用Servlet+jsp完成View层的开发。基于接口开发测试,要集成Spring+Hibernate,遇到Spring Bean注入Servlet的问题。

在applicationContext.xml中定义数据层访问Bean:

 

<bean id="userDao" class="test.UserDaoImpl"></bean>

 

 

UserDaoImpl是一个使用Hibernate访问数据库的类,包括了一些简单增删改查的方法,如getAll(),save()等

 

MyServlet.java代码如下:

 

public class MyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private UserDao userDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public MyServlet() {
		super();
	}

	public void init(ServletConfig config) throws ServletException {
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		List<User> list = userDao.getAll();
		out.println("<html>");
		out.println("<body>");
		out.println("<pre>ID\tNAME");
		for (User u : list) {
			out.println(u.getUserId() + "\t" + u.getUserName());
		}
		out.println("</pre>");
		out.println("</body>");
		out.println("</html>");
		out.close();
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}
}

 很显然,如果不加处理,下面这一行一定会抛出java.lang.NullPointerException。

 

 

		List<User> list = userDao.getAll();

 

 

解决方法一

在Servlet的init方法中增加以下代码,即可通知Servlet在启动时,自动查找userDao Bean并装配。

 

	public void init(ServletConfig config) throws ServletException {
		ServletContext servletContext = config.getServletContext();
		WebApplicationContext webApplicationContext = WebApplicationContextUtils
				.getWebApplicationContext(servletContext);
		AutowireCapableBeanFactory autowireCapableBeanFactory = webApplicationContext
				.getAutowireCapableBeanFactory();
		autowireCapableBeanFactory.configureBean(this, "userDao");
	}

 可是下面这一行需要将Bean Name硬编码到java源码中,让我很不爽。

 

 

autowireCapableBeanFactory.configureBean(this, "userDao");

 

 

 

解决办法二(推荐):

还是在Servlet的init方法中增加以下代码。

 

	public void init(ServletConfig config) throws ServletException {
	    SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
	    	      config.getServletContext());
	}

 并且在变量userDao上一行增加@Autowired

	@Autowired
	private UserDao userDao;

 

 

这样,就不用硬编码Bean Name到java源码中了。

分享到:
评论

相关推荐

    Spring.html

    Bean:写方法上,将方法的返回值 Configuration:标记配置类 ComponentScan包扫描 PropertySource:加载配置文件 Import:导入其他配置类 AOP 概念:面向切面编程,在不改变源码的情况下对方法进行增强,抽取横...

    springweb3.0MVC注解(附实例)

    &lt;bean class="org.springframework.web.servlet.mvc.annotation. AnnotationMethodHandlerAdapter"/&gt; &lt;!-- ③:对模型视图名称的解析,即在模型视图名称添加前后缀 --&gt; &lt;bean class="org.springframework.web....

    从零开始学Spring Boot

    1.19 Spring Boot使用Druid(编程注入) 1.20 Spring Boot普通类调用bean 1.21 使用模板(thymeleaf-freemarker) 1.22 Spring Boot 添加JSP支持 1.23 Spring Boot Servlet 1.24 Spring Boot过滤器、监听器 1.25 ...

    Spring攻略(第二版 中文高清版).part1

    3.12 将Spring Bean注入领域对象 147 3.12.1 问题 147 3.12.2 解决方案 147 3.12.3 工作原理 148 3.13 小结 151 第4章 Spring中的脚本 152 4.1 用脚本语言实现Bean 152 4.1.1 问题 152 4.1.2 解决...

    springjdbc

    &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /&gt; &lt;!-- apache.dbcp连接池的配置 --&gt; &lt;bean id="dataSource" class="org.apache.commons.dbcp....

    Spring攻略(第二版 中文高清版).part2

    3.12 将Spring Bean注入领域对象 147 3.12.1 问题 147 3.12.2 解决方案 147 3.12.3 工作原理 148 3.13 小结 151 第4章 Spring中的脚本 152 4.1 用脚本语言实现Bean 152 4.1.1 问题 152 4.1.2 解决...

    springboot学习思维笔记.xmind

    @Bean注解在方法上,声明当前方法的返回值为一个Bean AOP @Aspect 声明是一个切面 拦截规则@After @Before @Around PointCut JoinPoint Spring常用配置 Bean的Scope Singleton ...

    Spring.3.x企业应用开发实战(完整版).part2

    16.7.2 使用Spring Servlet API模拟对象 16.7.3 使用Spring RestTemplate测试 16.7.4 使用Selenium测试 16.8 小结 第17章 实战案例开发 17.1 论坛案例概述 17.1.1 论坛整体功能结构 17.1.2 论坛用例描述 17.1.3 主要...

    spring applicationContext 配置文件

    &lt;bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" p:order="0"&gt; &lt;property name="interceptors"&gt; &lt;list&gt; &lt;ref bean="doubleSubmitInterceptor"/&gt; &lt;/list&gt; ...

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    Web服务启动时自动加载Servlet,并读取数据库内容

    我操作数据库用的是spring的HibernateTemplate和JdbcTemplate,访问数据库是以bean注入的方式,在action里注入service的实现,读取数据库数据,其实现在的关键就是得到这个service的实例化对象,

    Spring3.x企业应用开发实战(完整版) part1

    16.7.2 使用Spring Servlet API模拟对象 16.7.3 使用Spring RestTemplate测试 16.7.4 使用Selenium测试 16.8 小结 第17章 实战案例开发 17.1 论坛案例概述 17.1.1 论坛整体功能结构 17.1.2 论坛用例描述 17.1.3 主要...

    spring_MVC源码

    14. &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /&gt; 15. 16. &lt;!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 --&gt; 17. &lt;bean class="org....

    一个基于SpringCloud的Chatgpt机器人.zip

    SpringBoot采用 JavaConfig的方式对Spring进行配置,并且提供了大量的注解,极大的提高了工作效率,比如@Configuration和@bean注解结合,基于@Configuration完成类扫描,基于@bean注解把返回值注入IOC容器。...

    Springboot、SpringCloud开发脚手架,集合各种常用框架使用案例.zip

    SpringBoot采用 JavaConfig的方式对Spring进行配置,并且提供了大量的注解,极大的提高了工作效率,比如@Configuration和@bean注解结合,基于@Configuration完成类扫描,基于@bean注解把返回值注入IOC容器。...

    基于spring-boot dubbox搭建的java分布式系统的前端管理.zip

    SpringBoot采用 JavaConfig的方式对Spring进行配置,并且提供了大量的注解,极大的提高了工作效率,比如@Configuration和@bean注解结合,基于@Configuration完成类扫描,基于@bean注解把返回值注入IOC容器。...

    JAVA框架:Spring框架、SpringMVC框架、MyBytes框架、SpringBoot框架.zip

    SpringBoot采用 JavaConfig的方式对Spring进行配置,并且提供了大量的注解,极大的提高了工作效率,比如@Configuration和@bean注解结合,基于@Configuration完成类扫描,基于@bean注解把返回值注入IOC容器。...

    基于Spring Boot框架的员工管理系统的设计与实现-初始框架.zip

    SpringBoot采用 JavaConfig的方式对Spring进行配置,并且提供了大量的注解,极大的提高了工作效率,比如@Configuration和@bean注解结合,基于@Configuration完成类扫描,基于@bean注解把返回值注入IOC容器。...

    springboot+activiti+angular 这是spring boot框架集成activiti工作流实现.zip

    SpringBoot采用 JavaConfig的方式对Spring进行配置,并且提供了大量的注解,极大的提高了工作效率,比如@Configuration和@bean注解结合,基于@Configuration完成类扫描,基于@bean注解把返回值注入IOC容器。...

Global site tag (gtag.js) - Google Analytics