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

}

 

posted on 2017-11-17 15:18  biyangqiang  阅读(218)  评论(0编辑  收藏  举报

导航