Spring MVC中使用@Autowired实现自动注入

原文地址为: Spring MVC中使用@Autowired实现自动注入

本文在上篇中已经实现的基于注解的Spring MVC程序中使用@Autowired自动注入一个java bean的service对象。


1、下载并导入之前已经实现的基于注解的Spring MVC工程

下载地址为http://download.csdn.net/detail/yxtouch/9108599


2、新建测试用的java类

本例中首先定一个接口类IDao,然后再创建一个类用于实现该接口IDaoImpl,在Controller类中包含一个IDaoImpl的对象,该对象通过@Autowired进行注入。

主要新增的java类和配置文件结构如图所示:

2.1、IDao接口定义

package com.landsem.demo.service;


public interface IDao {
public void show();
}

2.2、IDaoImpl类实现

package com.landsem.demo.impl;

import org.springframework.stereotype.Service;

import com.landsem.demo.service.IDao;

@Service
public class IDaoImpl implements IDao {

@Override
public void show() {
// TODO Auto-generated method stub
System.out.println("hello,world!");
}

}

2.3、修改OtherController类的实现

修改工程中OtherController类,修改后的实现如下:

package com.landsem.demo.annotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.landsem.demo.service.IDao;

@Controller
public class OtherController {

@Autowired
private IDao daoService;

@RequestMapping(value = "/otherController")
public ModelAndView welcome(HttpServletRequest request,
HttpServletResponse response) {
System.out.println("OtherController annotation hello.");
if(null != daoService) {
daoService.show();
}
else {
System.out.println("Dao sevice is null!");
}
// TODO Auto-generated method stub
return new ModelAndView("hello");
}

@RequestMapping(value = "/otherController2")
public ModelAndView welcome2(HttpServletRequest request,
HttpServletResponse response) {
System.out.println("OtherController2 annotation hello.");
if(null != daoService) {
daoService.show();
}
else {
System.out.println("Dao sevice is null!");
}
// TODO Auto-generated method stub
return new ModelAndView("hello");
}
}

3、创建自动加载bean的配置文件

在src目录下新建配置文件applicationContext-service.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-lazy-init="true">

<!-- Managers: For accessing Services -->
<context:component-scan base-package="com.landsem.demo.impl" />

</beans>

在该配置文件中使用   context:component-scan  自动扫描包 com.landsem.demo.impl 目录下可以作为java bean加载的类并加载。类IDaoImpl类用了@Service注解,所以能够被扫描为java bean对象。



4、修改web.xml配置文件

修改web.xml配置文件,将applicationContext-service.xml添加到contextConfigLocation中,如图所示:


5、测试

运行工程,打开浏览器输入

http://pc201410272116/SpringMvcDemo2/otherController.shtml

其中地址需要按照实际工程路径进行修改配置。访问成功后可以看到控制台输出IDaoImpl中的信息。如图:




转载请注明本文地址: Spring MVC中使用@Autowired实现自动注入

猜你喜欢

转载自blog.csdn.net/chch998/article/details/80847685