day05 Spring中自定义注解的用处-之获取自定义的Servie
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的更多相关文章
- Spring中异步注解@Async的使用、原理及使用时可能导致的问题
前言 其实最近都在研究事务相关的内容,之所以写这么一篇文章是因为前面写了一篇关于循环依赖的文章: <面试必杀技,讲一讲Spring中的循环依赖> 然后,很多同学碰到了下面这个问题,添加了S ...
- Spring中@Import注解的使用
Spring中@Import注解的使用 @Import注解算是SpringBoot自动配置原理中一个很重要的注解 认识@Import注解 先看一下源码 @Target(ElementType.TYPE ...
- 【进阶】Spring中的注解与反射
[进阶]Spring中的注解与反射 目录 [进阶]Spring中的注解与反射 前言 一.内置(常用)注解 1.1@Overrode 1.2@RequestMapping 1.3@RequestBody ...
- Spring中@相关注解的意义
1.@controller 控制器(注入服务) 用于标注控制层,相当于struts中的action层 2.@service 服务(注入dao) 用于标注服务层,主要用来进行业务的逻辑处理 3.@rep ...
- Spring中@Autowired注解、@Resource注解的区别 (zz)
Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@ ...
- Spring中Value注解的使用
Spring中Value注解的使用 分类: Spring2014-08-16 17:28 2985人阅读 评论(0) 收藏 举报 有的时候我们定义了Properties文件,并且使用Spring的Pr ...
- 全面解析Spring中@ModelAttribute注解的用法
本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:全面解析Spring中@ModelAttribute注解的用法: @ModelAttribute注解用于将方法的参数或方法的返回值绑定到 ...
- EnableAutoConfiguration注解 Spring中@Import注解的作用和使用
EnableAutoConfiguration注解 http://www.51gjie.com/javaweb/1046.html springboot@EnableAutoConfiguration ...
- Spring中常用注解的介绍
spring中使用注解时配置文件的写法: <?xml version="1.0" encoding="UTF-8"?> <span style ...
随机推荐
- bzoj3997
题解: dp f[i][j]=max(f[i-1][j+1]+a[i][j],max(f[i-1][j],f[i][j+1])); 代码: #include<bits/stdc++.h> ...
- 第三篇 功能实现(1) (Android学习笔记)
第三篇 功能实现(1) 第8章 Android应用程序组成 ●Android的一些中.底层基础知识 ※ Android Framework 启动过程 Android手机系统本质上是一个基于Linux的 ...
- window.setTimeout和window.setInterval的区别,及用其中一个方法记录时间。
window.setTimeout(语句,时间)是在多久之后执行语句,语句只执行一次. window.setInterval(语句,时间)是每隔多久执行一次语句,语句循环执行. <!DOCTYP ...
- 2019.3.22 Week 12 : ZigBee and T/H chamber test
Test purposes Remove backside center ventilation holes, pls help to conduct climatic chamber test of ...
- 2019-02-25 EST 科技文翻译
The Definition of Theme and Rheme The point of departure is equally presented to the speaker and to ...
- TCCSuperPlayerView让Delphi支持app视频播放!
今天ChinaCock发布了新版,完美支持视频播放!新版本中,发布了新的控件TCCSuperPlayerView,以支持视频播放. 这是一个可视控件,拖放到Form上,调整好大小与位置,就可以调用他的 ...
- 58-63用ssh远程连接linux系统
一.注意 在CentOS7.6中,ifconfig来查询IP已经不再适用,要用ip add 二.vim编辑器 编辑:按[i]键可以进入到编辑状态,显示insert这样的字眼就对了 退出:按esc退出编 ...
- FileInputStream类与FileOutputStream类
FileInputStream类是InputStream类的子类.他实现了文件的读取,是文件字节输入流.该类适用于比较简单的文件读取,其所有方法都是从InputStream类继承并重写的.创建文件字节 ...
- 引用google字体
1.很简单直接 @import url(http://fonts.font.im/css?family=Shadows+Into+Light); 引入页面即可 2. 使用: font-fami ...
- Centos7 使用Docker搭建Oracle测试环境
1.更新yum yum update 2.安装Docker yum install docker 安装完成后查看Docker的版本: docker version 查看Docker的信息: docke ...