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 ...
随机推荐
- day23-python操作数据库三
创建表import MySQLdb def connect_mysql(): db_config = { 'host': '192.168.1.5', 'port': 3306, 'user': 'w ...
- 前端小菜鸡使用Vue+Element笔记(二)
记录一下在使用Vue和Element做项目时遇到过的难点... 1.在 <el-table>表格中嵌入 select下拉选择框,以及 tooltip提示框的使用 主要定义格式如红色标记代码 ...
- java项目性能测试过程记录
一 准备环境和指标 1.主机三台(linux,这里显示的都是伪主机地址):最好用干净的机子,做性能测试的时候别装其他东西在上面跑. 100.22.0.98 部署自己的项目服务 100.22.0.9 ...
- 人生苦短,我用python,为什么选择python,python简介
认识 Python 人生苦短,我用 Python —— Life is short, you need Python  目标 Python 的起源 为什么要用 Python? Python 的特点 ...
- Spring异步调用注解@Async的使用
1.pom依赖 <dependency> <groupId>org.springframework</groupId> <artifactId>spri ...
- Unity中资源打包成Assetsbundle的资料整理
最近在研究Unity中关于资源打包的东西,网上看了一堆资料,这里做个整合,说整合,其实也就是Ctrl-C + Ctrl-V,不是原创 首先为了尊重原创,先贴出原创者的文章地址: http://blog ...
- EffecJava Method
坚持使用Overrider注解,可以预防我们并没有重载的情况出现. 除非使用者绝对安全,不然使用保护性拷贝,可以使程序安全. public class Period {//没有拷贝安全 private ...
- linux 断网 扫描基本命令
kali使用arpspoof命令进行ARP欺骗. 做法是获取目标主机IP镜像流量,再进行ARP欺骗. 此次测试实在局域网中进行,使用kali虚拟机和Windows10物理机测试. 最终效果是利用kal ...
- for循环遍历改用map函数
# for url in urls:# url = response.urljoin(url)# print(url)urls = map(lambda url:response.urljoin(ur ...
- python day14--内置函数二
主要内容: 1.lamda匿名函数 2.sorted() 3.filter() 4.map() 5.递归函数 一.Lamda匿名函数 语法:lamda:参数:返回值 # 计算n的n次⽅方 def fu ...