自定义注解:

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);
} }
05-15 11:33