PS: 在RPC远程调用中,想要获取自定义的service的方法,就得自定义标签遍历拿到方法

PS:在spring中,两个最核心的 概念是aop和ioc,aop其实就是动态代理。 ioc 就是解决对象的创建问题。

1.自定义标签

package cn.itcast_04_springannotation.userdefinedannotation.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.stereotype.Component; @Target({ ElementType.TYPE })//注解用在接口上
@Retention(RetentionPolicy.RUNTIME)//VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息
@Component
public @interface RpcService { String value();
}

2.实现接口中的方法

package cn.itcast_04_springannotation.userdefinedannotation.service.impl;

import cn.itcast_04_springannotation.userdefinedannotation.annotation.RpcService;
import cn.itcast_04_springannotation.userdefinedannotation.service.HelloService; @RpcService("HelloServicebb")
public class HelloServiceImpl implements HelloService {
public String hello(String name) {
return "Hello! " + name;
} public void test(){
System.out.println("test");
}
}

3.主程序测试

package cn.itcast_04_springannotation.userdefinedannotation.test;

import java.lang.reflect.Method;
import java.util.Map; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component; import cn.itcast_04_springannotation.userdefinedannotation.annotation.RpcService;
import cn.itcast_04_springannotation.userdefinedannotation.service.HelloService; @Component //ApplicationContextAware会为Component组件调用setApplicationContext方法; 测试Myserver3时注释
public class MyServer implements ApplicationContextAware {
@SuppressWarnings("resource")
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring2.xml");
} public void setApplicationContext(ApplicationContext ctx)
throws BeansException {
Map<String, Object> serviceBeanMap = ctx
.getBeansWithAnnotation(RpcService.class);
for (Object serviceBean : serviceBeanMap.values()) {
try {
//获取自定义注解上的value
String value = serviceBean.getClass().getAnnotation(RpcService.class).value();
System.out.println("注解上的value: " + value); //反射被注解类,并调用指定方法
Method method = serviceBean.getClass().getMethod("hello",
new Class[] { String.class });
Object invoke = method.invoke(serviceBean, "bbb");
System.out.println(invoke);
} catch (Exception e) {
e.printStackTrace();
}
}
} }

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
注解上的value: HelloServicebb
Hello! bbb

 

---------------------------------下面讲解一下关于spring和junit4的测试方式

package cn.itcast_04_springannotation.userdefinedannotation.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import cn.itcast_04_springannotation.userdefinedannotation.service.HelloService; @RunWith(SpringJUnit4ClassRunner.class)//加载类
@ContextConfiguration(locations = "classpath:spring2.xml")//加载文件
@Component
public class MyServer3 {
@Autowired
HelloService helloService; @Test
public void helloTest1() {
System.out.println("开始junit测试……");
String hello = helloService.hello("ooooooo");
System.out.println(hello);
} }

day05 Spring中自定义注解的用处-之获取自定义的Servie的更多相关文章

  1. Spring中异步注解@Async的使用、原理及使用时可能导致的问题

    前言 其实最近都在研究事务相关的内容,之所以写这么一篇文章是因为前面写了一篇关于循环依赖的文章: <面试必杀技,讲一讲Spring中的循环依赖> 然后,很多同学碰到了下面这个问题,添加了S ...

  2. Spring中@Import注解的使用

    Spring中@Import注解的使用 @Import注解算是SpringBoot自动配置原理中一个很重要的注解 认识@Import注解 先看一下源码 @Target(ElementType.TYPE ...

  3. 【进阶】Spring中的注解与反射

    [进阶]Spring中的注解与反射 目录 [进阶]Spring中的注解与反射 前言 一.内置(常用)注解 1.1@Overrode 1.2@RequestMapping 1.3@RequestBody ...

  4. Spring中@相关注解的意义

    1.@controller 控制器(注入服务) 用于标注控制层,相当于struts中的action层 2.@service 服务(注入dao) 用于标注服务层,主要用来进行业务的逻辑处理 3.@rep ...

  5. Spring中@Autowired注解、@Resource注解的区别 (zz)

    Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@ ...

  6. Spring中Value注解的使用

    Spring中Value注解的使用 分类: Spring2014-08-16 17:28 2985人阅读 评论(0) 收藏 举报 有的时候我们定义了Properties文件,并且使用Spring的Pr ...

  7. 全面解析Spring中@ModelAttribute注解的用法

    本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:全面解析Spring中@ModelAttribute注解的用法: @ModelAttribute注解用于将方法的参数或方法的返回值绑定到 ...

  8. EnableAutoConfiguration注解 Spring中@Import注解的作用和使用

    EnableAutoConfiguration注解 http://www.51gjie.com/javaweb/1046.html springboot@EnableAutoConfiguration ...

  9. Spring中常用注解的介绍

    spring中使用注解时配置文件的写法: <?xml version="1.0" encoding="UTF-8"?> <span style ...

随机推荐

  1. (C/C++学习笔记)附页: C/C++各数据类型的相关说明

  2. 数学软件Matlab的使用感受

    在我一年前的暑假,我们的小学期学习了MATLAB软件.MATLAB是一款数学软件,可以用于算法计算.数据可视化.数据分析以及数据计算. 我们主要学习了MATLAB关于数学上的经常用的一些用法和算法,M ...

  3. 8.2 C++标准输出流对象

    参考:http://www.weixueyuan.net/view/6408.html 总结: iostream头文件,包含了该头文件后,我们就可以直接使用这些对象,包含标准的输出流对象cout.ce ...

  4. CentOS7安装Nginx及配置

    Nginx是一款轻量级的网页服务器.反向代理服务器.相较于Apache.lighttpd具有占有内存少,稳定性高等优势.**它最常的用途是提供反向代理服务.** 安装   在Centos下,yum源不 ...

  5. CSS学习笔记-04 a标签-导航练习

    个人练习,各位大神勿笑  .. <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  6. python 3.x 字典的11种方法

    python 3.x 字典的11种方法2017年11月25日 01:02:11 Milton-Long 阅读数:535 标签: python python字典方法 更多个人分类: python-学习之 ...

  7. WSDL 文档-一个简单的 XML 文档

    WSDL 文档是利用这些主要的元素来描述某个 web service 的: <portType>-web service 执行的操作 <message>-web service ...

  8. 二进制数值Byte [] 转Base64字符串

    将二进制数据转换成Base64字符串: String base64String = new String(byteArray).replaceAll("\n","&quo ...

  9. pycharm 4注册码

    pycharm 4 注册码 (4.x版均可使用) 用户名:pycharmLicense key: ===== LICENSE BEGIN =====2581-D36230T00000QoX2zbDCV ...

  10. sublime text3 key

    Sublime Text 3 3126 注册码 第一个测试通过 —– BEGIN LICENSE —– Michael Barnes Single User License EA7E-821385 8 ...