参照博文Spring入门一,以理解注解的含义。

项目结构:

实现类:SpringHelloWorld

  1. package com.yibai.spring.helloworld.impl;
  2.  
  3. import org.springframework.stereotype.Component;
  4.  
  5. import com.yibai.spring.helloworld.HelloWorld;
  6.  
  7. @Component
  8. public class SpringHelloWorld implements HelloWorld{
  9.  
  10. @Override
  11. public void sayHello() {
  12. System.out.println("spring hello world");
  13.  
  14. }
  15.  
  16. }

实现类:strutsHelloWorld

  1. package com.yibai.spring.helloworld.impl;
  2.  
  3. import org.springframework.stereotype.Component;
  4.  
  5. import com.yibai.spring.helloworld.HelloWorld;
  6.  
  7. @Component
  8. public class StrutsHelloWorld implements HelloWorld{
  9.  
  10. @Override
  11. public void sayHello() {
  12. System.out.println("struts hello world");
  13. }
  14.  
  15. }

service类:

  1. package com.yibai.spring.helloworld;
  2.  
  3. import javax.annotation.Resource;
  4. import org.springframework.stereotype.Service;
  5. import com.yibai.spring.helloworld.impl.SpringHelloWorld;
  6. import com.yibai.spring.helloworld.impl.StrutsHelloWorld;
  7.  
  8. @Service
  9. public class HelloWorldService {
  10.  
  11. @Resource
  12. private SpringHelloWorld helloWorld;
  13.  
  14. @Resource
  15. private StrutsHelloWorld helloWorld2;
  16.  
  17. public HelloWorldService(){
  18.  
  19. }
  20.  
  21. public void show(){
  22. helloWorld.sayHello();
  23. helloWorld2.sayHello();
  24. }
  25.  
  26. }

主方法类:

  1. package com.yibai.spring;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import com.yibai.spring.helloworld.HelloWorldService;
  6.  
  7. public class HelloProgram {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. // 创建上下文对象
  12. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  13.  
  14. HelloWorldService service = (HelloWorldService) context.getBean("helloWorldService");
  15.  
  16. service.show();
  17.  
  18. }
  19. }

配置类:web.xml不变,spring配置applocationContext.xml改为支持注解

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
  5. -->
  6. <beans xmlns="http://www.springframework.org/schema/beans"
  7. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  8. xmlns:context="http://www.springframework.org/schema/context"
  9. xsi:schemaLocation="http://www.springframework.org/schema/beans
  10. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  13.  
  14. <context:component-scan base-package="com.yibai.spring"/>
  15.  
  16. </beans>

service中,通过@resource注解需要注入的bean,实现类通过@Component完成spring配置,service类通过@Service完成spring配置。

附包名:多了一个aop包

补充:

有四种类型的组件自动扫描注释类型

  @Component  指示自动扫描组件,这四种注解最终都被注解为Component,故可以用这个注解代替所有的组件扫描注解

  @Repository  表示在持久层DAO组件

  @Service  表示在业务层服务组件

  @Controller  表示在表示层控制器组件

过滤器组件扫描spring配置:自动扫描类文件名中包含 Service和 Impl 的类

  1. <context:component-scan base-package="com.yibai.spring">
  2. <context:include-filter type="regex" expression=".*Service.*"/>
  3. <context:include-filter type="regex" expression=".*Impl.*"/>
  4. </context:component-scan>

Spring入门注解版的更多相关文章

  1. spring mvc注解版01

    spring mvc是基于servlet实现的在spring mvc xml版中已经说过了,注解版相较于xml版更加简洁灵活. web项目的jar包: commons-logging-1.1.3.ja ...

  2. 简单实现Spring框架--注解版

    自己写的Spring框架——简单实现IoC容器功能 前几天在网上看了篇帖子,是用xml的方式实现spring的ioc容器,觉得挺有意思的,这边自己试着用注解的形式造了一套轮子. 工程结构 codein ...

  3. Spring 定时任务 注解版

    Task类: ManageSql.Java对应代码: package com.axb.cheney.task; import java.sql.ResultSet; import java.sql.S ...

  4. spring入门-注解的使用

    说明: 使用注解和使用配置文件实现的功能是一样的,都是为了解耦,但是配置文件语法属于非编程语言法语,无法调试,难以定位bug,使用注解更易定位问题. 配置步骤 编译器必须先安装了STS插件 第一步 导 ...

  5. 菜鸟学习Spring——SpringMVC注解版前台向后台传值的两种方式

    一.概述. 在很多企业的开法中常常用到SpringMVC+Spring+Hibernate(mybatis)这样的架构,SpringMVC相当于Struts是页面到Contorller直接的交互的框架 ...

  6. 菜鸟学习Spring——SpringMVC注解版解析不同格式的JSON串

    一.概述 不同格式的JSON串传到后台来实现功能这个是我们经常要做的一件事,本篇博客就给大家介绍四种不同的JSON串传到后台后台如何用@RequestBody解析这些不同格式的JSON串的. 二.代码 ...

  7. 菜鸟学习Spring——SpringMVC注解版将URL中的参数转成实体

    一.概述 将URL中参数转成实体在我们项目中用的很多比如界面提交表单请求后台的Contorller的时候通过URL传递了一串参数到后台,后台通过Spring让界面的字段与实体的字段映射来实现给后台的实 ...

  8. 菜鸟学习Spring——SpringMVC注解版在服务器端获取Json字符串并解析

    一.概述. SpringMVC在服务端把客户端传过来的JSON字符串,并把JSON字符串转成 JSON对象并取得其中的属性值,这个在项目中经常用到. 二.代码演示. 需要添加的jar包. 2.1 we ...

  9. 菜鸟学习Spring——SpringMVC注解版控制层重定向到控制层

    一.概述. SpringMVC中界面请求Contorller1,Contorller1需要重定向到Contorller2中显示其他页面或者做一些业务逻辑,Spring中提供了这个功能利用"r ...

随机推荐

  1. HDU 5441——Travel——————【并查集+二分查界限】

    Travel Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  2. IIS下不能下载文件的docx文档,XLSX文档的设置方法(转)

    IIS下不能下载文件的docx文档,XLSX文档的设置方法 Office 2007的的界面风格默认格式中都是.DOCX,XLSX,PPTX等等后缀,连结中包含此类文件时,界面风格默认什么打不开的其实只 ...

  3. python面试题——爬虫相关

    1.接触过几种爬虫模块 urllib.requests这两种爬虫模块. 2.robots协议是什么? 它就是一个防君子不防小人的协议,网站有一些数据不想被爬虫程序爬取,可以编写robots协议文件,明 ...

  4. webpack-webpackConfig-配置说明-多页面

    入口文件entry 配置 /* 例子: 项目目录结构: ├─src # 当前项目的源码 ├─pages # 各个页面独有的部分,如入口文件.只有该页面使用到的css.模板文件等 │ ├─alert # ...

  5. 栅格那点儿事(二)---细看Raster属性

    细看Raster属性 如果你已经看完了上一章,你已经了解了栅格数据是由一个个像元(Cell)按照行列方式构成的,每一个像元都有自己的像元值,并且这些像元值根据栅格类型的不同,可以代表反射值,或高程值, ...

  6. Eclipse Action

    Interface IAction package org.eclipse.jface.action; import org.eclipse.core.commands.IHandlerAttribu ...

  7. 如何找到Android app启动activity和页面元素信息

    在实施app自动化的时候,我们需要知道app 的启动activity和页面元素信息,以此启动app和定位页面元素,那么如何在没有源码的情况下找打他们呢?当然是有好的工具啦,有Android sdk自带 ...

  8. Myeclipse与tomcat的运行问题

    在myeclipse中修改自己servlet后,在次运行时,可能会没有变化,这时需要重启tomcat,重新加载servlet

  9. “System.OutOfMemoryException”类型的未经处理的异常在 mscorlib.dll 中发生

    在VS中写程序遇到这样的问题.但数据规模小的时候不出现,但数据规模大的时候就出现.但我的电脑用32G内存.处理的文本也不是很多,在文本alignment时出错.

  10. 字符串查找算法的改进-hash查找算法

    字符串查找即为特征查找: 特征即位hash: 1.将待查找的字符串hash: 2.在容器字符串中找头字符匹配的字符串,并进行hash: 3.比较hash的结果:相同即位匹配: hash算法的设计为其中 ...