1. 程式人生 > >hadoop29---自定義註解

hadoop29---自定義註解

ebean -- exceptio str can 周期 fin spring get

自定義註解:

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 }) //表示@RpcService這個註解是用在類上的,METHOD表示註解用在方法上的。
@Retention(RetentionPolicy.RUNTIME) //註解的生命周期,VM將在運行期保留註解,spring會把這個註解標註的類做實例化。
@Component //讓spring去掃描
public @interface RpcService {
    String value();//@RpcService("HelloServicebb")的HelloServicebb
}

service接口

package cn.itcast_04_springannotation.userdefinedannotation.service;


public interface HelloService {
    String hello(String name);
}

service實現:

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"); } }

測試:

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        //測試Myserver3時註釋
public class MyServer implements ApplicationContextAware {
    //如果某個類實現了ApplicationContextAware接口,會在類初始化完成後調用setApplicationContext()方法進行操作
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring2.xml");
        /*<?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:context="http://www.springframework.org/schema/context"
               xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd">
               
               //掃描包,把自定義和不是自定義的註解都加進來
            <context:component-scan base-package="cn.itcast_04_springannotation.userdefinedannotation"/>
            
        </beans>*/
    }


    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();
            }
        }
    }

}

測試2:

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;

//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations = "classpath:spring2.xml")
//@Component
public class MyServer2 implements ApplicationContextAware {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring2.xml");
        
        /*<?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:context="http://www.springframework.org/schema/context"
               xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context.xsd">
               
            <context:component-scan base-package="cn.itcast_04_springannotation.userdefinedannotation"/>
            
        </beans>*/
    
        Map<String, Object> beans = ctx.getBeansWithAnnotation(RpcService.class);
        for(Object obj: beans.values()){
            HelloService hello=(HelloService) obj;
            String hello2 = hello.hello("mmmm");
            System.out.println(hello2);
        }
    }

    /*@Test
    public void helloTest1() {
    System.out.println("開始junit測試……");
    }*/

    public void setApplicationContext(ApplicationContext ctx)
            throws BeansException {
        Map<String, Object> serviceBeanMap = ctx
                .getBeansWithAnnotation(RpcService.class);
        for (Object serviceBean : serviceBeanMap.values()) {
            try {
                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();
            }
        }
    }

}

測試3:

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);
    }

}

hadoop29---自定義註解