关于Java Annotation的一个例子

推荐一下一个有趣的表情包收集网站,也可以在线制作表情包,欢迎访问:撸表情




在上一篇Jave中的Annotation详解中对Java Annotation作了一个详细的介绍,但是没有例子,不够直观,所以就有了这篇——一个例子:

一、ClassAnnotation修饰类

import java.lang.annotation.Documented;
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)
@Documented
public @interface ClassAnnotation
{
	String value();
}
二、MethodAnnotation修饰方法

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MethodAnnotation
{
	String methodName();
	String methodDestination();
}


三、FieldAnnotation修饰属性

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FieldAnnotation
{
	String value();
}


四、AnnotationClass源码

@ClassAnnotation("Annotation用于类")
public class AnnotationClass
{
	@MethodAnnotation(methodName="method1",methodDestination="Annotation用于方法")
	public String method1()
	{
		return this.id;
	}
	@MethodAnnotation(methodName="method2",methodDestination="Annotation用于方法")
	public String method2()
	{
		return null;
	}
	@FieldAnnotation("Annotation用于属性")
	public String id="123456";
}

五、AnnotationTest测试

import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Set;

public class AnnotationTest {

   public static void main (String[] args) throws ClassNotFoundException
   {
   		String className="AnnotationClass";
   		Class test=Class.forName(className);  		
   		//判断是否采用ClassAnnotation
   		boolean flag=test.isAnnotationPresent(ClassAnnotation.class);
   		if(flag)
   		{
   			ClassAnnotation classAnnotation=(ClassAnnotation)test.getAnnotation(ClassAnnotation.class);
   			System.out.println("类描述:"+classAnnotation.value());
   		}
   		Set<Method> set=new HashSet<Method>();
   		Method[] methods=test.getMethods();
   		for(int i=0;i<methods.length;i++)
   		{
   			//判断是否采用MethodAnnotation
   			if(methods[i].isAnnotationPresent(MethodAnnotation.class))
   				set.add(methods[i]);
   		}
   		for(Method m:set)
   		{
   			MethodAnnotation methodAnnotation=m.getAnnotation(MethodAnnotation.class);
   			System.out.println("方法:"+methodAnnotation.methodName()+"  目的:"+methodAnnotation.methodDestination());
   		}
   		Set<Field> set2=new HashSet<Field>();
   		Field[] fields=test.getFields();
   		for(int i=0;i<fields.length;i++)
   		{
   			//判断是否采用FieldAnnotation
   			if(fields[i].isAnnotationPresent(FieldAnnotation.class))
   				set2.add(fields[i]);
   		}
   		for(Field f:set2)
   		{
   			FieldAnnotation fieldAnnotation=f.getAnnotation(FieldAnnotation.class);
   			System.out.println("属性的描述:"+fieldAnnotation.value());
   		}
   		
   }
    
}

单独保存成java文件,编译下就可以运行了,结果比较简单就不截图了,源码都在上面,这个是以前自己学的时候写的书上的代码,相差不大,快1年了把,时间过得好快!欢迎交流!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好,以下是Java中生成一个Socket和Servlet结合的代码例子: 首先,需要在服务器端创建一个Servlet,假设我们的Servlet类名为MyServlet: import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/MyServlet") public class MyServlet extends HttpServlet { private static final long serialVersionUID = 1L; public MyServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub try { Socket socket = new Socket("127.0.0.1", 8888); PrintWriter out = new PrintWriter(socket.getOutputStream()); out.println("Hello Server"); out.flush(); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); String msg = in.readLine(); System.out.println("Message from Server: " + msg); } catch (Exception e) { System.out.println("Exception: " + e); } doGet(request, response); } } 上述代码中,创建了一个Socket对象,连接到本地IP地址为127.0.0.1的服务器端口号为8888的Socket服务,向服务器端发送一条“Hello Server”消息,然后等待服务器端的响应消息。 接下来,在服务器端创建一个Socket服务程序,代码如下: import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class SocketServer { public static void main(String[] args) { // TODO Auto-generated method stub try { ServerSocket server = new ServerSocket(8888); while (true) { Socket client = server.accept(); System.out.println("Connect to: " + client); BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); PrintWriter out = new PrintWriter(client.getOutputStream()); String msg = in.readLine(); System.out.println("Message from Client: " + msg); out.println("Hello Client"); out.flush(); } } catch (Exception e) { System.out.println("Exception: " + e); } } } 上述代码中,创建了一个ServerSocket对象,监听本地端口号为8888的Socket服务,当有客户端连接时,创建一个Socket对象,关联到客户端的Socket服务,从客户端读取请求消息,然后向客户端发送一条“Hello Client”响应消息。 这就是Java中生成一个Socket和Servlet结合的代码例子,希望可以帮助到你,如有其它问题,请随时联系我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值