<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                [TOC] # 解釋 1. 自定義注解的作用:在反射中獲取注解,以取得注解修飾的“類、方法、屬性”的相關解釋。 2. java內置注解 ~~~ @Target 表示該注解用于什么地方,可能的 ElemenetType 參數包括: ElemenetType.CONSTRUCTOR 構造器聲明 ElemenetType.FIELD 域聲明(包括 enum 實例) ElemenetType.LOCAL_VARIABLE 局部變量聲明 ElemenetType.METHOD 方法聲明 ElemenetType.PACKAGE 包聲明 ElemenetType.PARAMETER 參數聲明 ElemenetType.TYPE 類,接口(包括注解類型)或enum聲明 @Retention 表示在什么級別保存該注解信息。可選的 RetentionPolicy 參數包括: RetentionPolicy.SOURCE 注解將被編譯器丟棄 RetentionPolicy.CLASS 注解在class文件中可用,但會被JVM丟棄 RetentionPolicy.RUNTIME JVM將在運行期也保留注釋,因此可以通過反射機制讀取注解的信息。 ~~~ # 實現 ## 定義自定義注解 ~~~ package com.annotation; import org.springframework.stereotype.Component; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target({ ElementType.TYPE })//注解用在類上 @Retention(RetentionPolicy.RUNTIME)//VM將在運行期也保留注釋,因此可以通過反射機制讀取注解的信息 @Component public @interface RpcService { String value(); } ~~~ ## 使用類 接口 ~~~ package com.service; public interface HelloService { String hello(String name); } ~~~ 實現類 ~~~ package com.service.impl; import com.annotation.RpcService; import com.service.HelloService; @RpcService("HelloService") public class HelloServiceImpl implements HelloService { @Override public String hello(String name) { return "hello "+name; } public void test() { System.out.println("test"); } } ~~~ ## spring配置文件 component掃描 ~~~ <context:component-scan base-package="com" /> ~~~ ## 通過ApplicationContext獲取所有標記這個注解的類 ~~~ @Component public class MyServer implements ApplicationContextAware { public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { //獲取使用這個注解的類 Map<String, Object> serviceBeanMap = applicationContext.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", String.class); Object invoke = method.invoke(serviceBean, "jdxia"); System.out.println(invoke); }catch (Exception e) { e.printStackTrace(); } } } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看