日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

day05 Spring中自定义注解的用处-之获取自定义的Servie

發布時間:2025/3/21 javascript 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 {//獲取自定義注解上的valueString 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 {@AutowiredHelloService helloService;@Testpublic void helloTest1() {System.out.println("開始junit測試……");String hello = helloService.hello("ooooooo");System.out.println(hello);}}

?

轉載于:https://www.cnblogs.com/bee-home/p/7851770.html

總結

以上是生活随笔為你收集整理的day05 Spring中自定义注解的用处-之获取自定义的Servie的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。