Spring MVC中使用@Autowired实现自动注入
2015-09-15 16:46
721 查看
本文在上篇中已经实现的基于注解的Spring MVC程序中使用@Autowired自动注入一个java bean的service对象。
主要新增的java类和配置文件结构如图所示:
public interface IDao {
public void show();
}
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!");
}
}
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");
}
}
<?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对象。
其中地址需要按照实际工程路径进行修改配置。访问成功后可以看到控制台输出IDaoImpl中的信息。如图:
1、下载并导入之前已经实现的基于注解的Spring MVC工程
下载地址为http://download.csdn.net/detail/yxtouch/91085992、新建测试用的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中的信息。如图:
相关文章推荐
- java-WEB中的监听器Lisener
- GUI - Web前端开发框架
- Extjs4.0 最新最全视频教程
- MyEclipse Web Project转Eclipse Dynamic Web Project
- axis备忘
- Erlang实现的一个Web服务器代码实例
- 防止网页脚本病毒执行的方法-from web
- 自学成才的秘密:115个 web Develop 资源
- 使用批处理修改web打印设置笔记 适用于IE
- Apache Web让JSP“动”起来
- web下载的ActiveX控件自动更新
- 推荐六款WEB上传组件性能测试与比较第1/10页
- 关于三种主流WEB架构的思考
- 使用 Iisext.vbs 列出 Web 服务扩展文件的方法
- 使用 Iisext.vbs 删除 Web 服务扩展文件的方法
- 使用 iisext.vbs 禁用 Web 服务扩展的方法
- 用vbs 实现从剪贴板中抓取一个 URL 然后在浏览器中打开该 Web 站点
- web标准知识——从p开始,循序渐进
- web标准知识――用途相似的标签
- Shell脚本实现的一个简易Web服务器例子分享