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

项目结构:

实现类:SpringHelloWorld

package com.yibai.spring.helloworld.impl;

import org.springframework.stereotype.Component;

import com.yibai.spring.helloworld.HelloWorld;

@Component
public class SpringHelloWorld implements HelloWorld{ @Override
public void sayHello() {
System.out.println("spring hello world"); } }

实现类:strutsHelloWorld

package com.yibai.spring.helloworld.impl;

import org.springframework.stereotype.Component;

import com.yibai.spring.helloworld.HelloWorld;

@Component
public class StrutsHelloWorld implements HelloWorld{ @Override
public void sayHello() {
System.out.println("struts hello world");
} }

service类:

package com.yibai.spring.helloworld;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.yibai.spring.helloworld.impl.SpringHelloWorld;
import com.yibai.spring.helloworld.impl.StrutsHelloWorld; @Service
public class HelloWorldService { @Resource
private SpringHelloWorld helloWorld; @Resource
private StrutsHelloWorld helloWorld2; public HelloWorldService(){ } public void show(){
helloWorld.sayHello();
helloWorld2.sayHello();
} }

主方法类:

package com.yibai.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.yibai.spring.helloworld.HelloWorldService; public class HelloProgram { public static void main(String[] args) { // 创建上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorldService service = (HelloWorldService) context.getBean("helloWorldService"); service.show(); }
}

配置类:web.xml不变,spring配置applocationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.yibai.spring"/> </beans>

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

附包名:多了一个aop包

补充:

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

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

  @Repository  表示在持久层DAO组件

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

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

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

<context:component-scan base-package="com.yibai.spring">
<context:include-filter type="regex" expression=".*Service.*"/>
<context:include-filter type="regex" expression=".*Impl.*"/>
</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. httpd编译安装php

    wget http://hk1.php.net/distributions/php-5.6.31.tar.gz yum groupinstall "Development Tools&quo ...

  2. CF 540D——Bad Luck Island——————【概率dp】

    Bad Luck Island time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  3. Visual Studio 要求导入 pfx 密钥以及导入后依然要求导入的解决办法

    本文为个人博客备份文章,原文地址: http://validvoid.net/visual-studio-pfx-import/ 导入密钥 在使用 Visual Studio 生产项目时,使用 pfx ...

  4. 关于 Android Studio 如何连接手机调试

    第一步:设置-> 打开开发者选项,以及USB调试模式 第二步:关于手机->版本号,点击版本号会弹出提示:已经处于开发者模式,无需操作 第三步:设置->在搜索框中输入HDB,此时会弹出 ...

  5. php 编译安装指导

    php 编译安装 下载源码 安装 安装后配置 下载源码 php下载地址:http://php.net/downloads.php php-7.1.11.tar.bz2 安装 安装依赖包 yum ins ...

  6. Mautic-2.2.0 (Ubuntu 16.04)

    平台: Ubuntu 类型: 虚拟机镜像 软件包: mautic-2.2.0 business intelligence commercial ecommerce mautic open-source ...

  7. C#中WinForm程序退出方法技巧总结[转]

      这篇文章主要介绍了C#中WinForm程序退出方法,实例总结了技巧退出WinForm程序窗口的各种常用技巧,非常具有实用价值,需要的朋友可以参考下 本文实例总结了C#中WinForm程序退出方法技 ...

  8. 在西雅图华盛顿大学 (University of Washington) 就读是怎样一番体验?

    http://www.zhihu.com/question/20811431   先说学校.优点: 如果你是个文青/装逼犯,你来对地方了.连绵不断的雨水会一下子让写诗的感觉将你充满. 美丽的校园.尤其 ...

  9. java核心-流基础

    1.流的概念: 1.1输入流:向内存中写入数据 1.2输出流:有内存向指定的地方输出数据 2.

  10. linux挂载和卸载NAS操作

    1.建立准备挂载NFS的目录,例如:cd /home/test,mkdir my_NFS_Catalog 2.挂接NFS至 /home/test/my_NFS_Catalog目录下(nas有两种格式: ...