「java注解题目」注解Java

博主:adminadmin 2022-12-31 20:51:10 504

本篇文章给大家谈谈java注解题目,以及注解Java对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

求java注解,语句解释

Spring中常用注释前提:有加入j2ee/common-annotations.jar

@Resource

1),默认按名称,名称找不到,按类型

2),可以按指定特定名称查找

3)强烈推荐

倘若不使用该tag,完整实现该功能的示例如下:

Java代码部分:

public class UserManagerImpl implements UserManager {

private UserDao userDao;

public void setUserDao(UserDao userDao) {

this.userDao = userDao;

}

...

}

Spring配置文件部分:

bean id="userManagerImpl" class="com.kedacom.spring.annotation.service.UserManagerImpl"

property name="userDao" ref="userDao" /

/bean

bean id="userDao" class="com.kedacom.spring.annotation.persistence.UserDaoImpl"

property name="sessionFactory" ref="mySessionFactory" /

/bean

@Repository, @Service, and @Controller

1)按层查找对应上一层的impl,不用在每层定义上层的类

2)强烈推荐

倘若不使用该tag,完整实现该功能的示例如下:

Java代码部分:

public class UserManagerImpl implements UserManager {

private UserDao userDao;

public void setUserDao(UserDao userDao) {

this.userDao = userDao;

}

...

}

配置文件

!-- 把DAO注入给Session工厂 --

bean id="userDAO" class="com.alonely.dao.UserDAO"

property name="sessionFactory"

ref bean="sessionFactory" /

/property

/bean

!-- 把Service注入给DAO --

bean id="userService" class="com.alonely.service.UserService"

property name="userDAO"

ref local="userDAO" /

/property

/bean

!-- 把Action注入给Service --

bean name="/user" class="com.alonely.struts.action.UserAction"

property name="userService"

ref bean="userService" /

/property

@Transactional

1)Spring提供的事务管理注释,可赋值。

2)可声明在方法和类上

3)需要在Spring配置文件中配置声明式事务管理。

@Scope("prototype")

1)定义Bean的作用范围,作用于Web工程一定要在Action上设为prototype,否则只能单处理。

@Autowired(required = false)

@Qualifier("accountsBusinessImpl")

1)@Autowired是byType,按类型查找,是Spring自己定义的tag

2)@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作,其中required = false通知不必一定要存在,

若是为true找不到可能报错

3)一般两个一起使用,效果类似@Resource ,但是已经基本被由JSR-250规范的@Resource代替,

Spring还支持的几个由JSR-250规范定义的注释有:@PostConstruct以及@PreDestroy

以下是分别在成员变量和方法中定义的代码:

//成员变量中定义

public class UserManagerImpl implements UserManager {

private UserDao userDao;

@Autowired

public void setUserDao(UserDao userDao) {

this.userDao = userDao;

}

...

}

//方法中定义

public class UserManagerImpl implements UserManager {

private UserDao userDao;

@Autowired

public void setUserDao(UserDao userDao) {

this.userDao = userDao;

}

...

}

//唯一区别:第一种实现中,Spring会直接将UserDao类型的唯一一个bean赋值给userDao这个成员变量;

//第二种实现中,Spring会调用setUserDao方法来将UserDao类型的唯一一个bean装配到userDao这个属性。

一般使用该两个tag要引入:

bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /

但自动装配中包含了,所以不重复定义bean了。

JAVA 元注解的问题

1@Retention,参数有SOURCE/CLASS/RUNTIME,自身也是一个元注释,用于对元注释的生命保持,SOURCE只存在于源代码,CLASS还存在于.class文件,RUNTIME,运行时可以使用。默认值是CLASS,如果你在代码运行中需要判断是否存在这样的元注释,则需要用RUNTIME。

2@Target,指定元注解用于什么目标,参数比较好理解,METHOD的只能用在方法上面,例如

@Override,FIELD只能用于成员,例如@Deprecated public static final int SDK;标明该公有变量不鼓励使用。

3Documented标明是一个有文档的API,这个元注释类的声明比较有意思

@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.ANNOTATION_TYPE)

public @interface Documented {

}

它用自身说明了自己的属性,感觉是自注释的。

4,Inherited,如果用它来注释一个元注释,那么被注释的元注释会被使用了该元注释的类的子类所继承。

例如某classA,使用了@TestAnno而TestAnno声明为Inherited

那么class B extends A,则B也拥有该TestAnno,否则B不拥有该TestAnno

java面试题求代码,最好有注解。。。

public

class

ThreadCaseDemo01

{

/**

*

*

*

采用Java

*

多线程技术,设计实现一个符合生产者和消费者问题的程序。对一个对象(枪膛)进行操作,其最大容量是12颗子弹。

*

生产者线程是一个压入线程

*

它不断向枪膛中压入子弹;消费者线程是一个射出线程,它不断从枪膛中射出子弹。

*/

public

static

void

main(String[]

args)

{

Gun

i

=

new

Gun();

Producter

pro

=

new

Producter(i);

Consumer

con

=

new

Consumer(i);

new

Thread(pro).start();

new

Thread(con).start();

}

}

//////////////////////////////////////////////

public

class

Gun

{

private

String

name;

private

String

content;

private

boolean

flag

=

false;

public

synchronized

void

set(String

name,

String

content)

{

if

(flag)

{

try

{

super.wait();

}

catch

(InterruptedException

e)

{

e.printStackTrace();

}

}

this.setName(name);

this.setContent(content);

flag

=

true;//保证生产者进入等待状态

super.notify();

}

public

synchronized

void

get()

{

if

(!flag)

{

try

{

super.wait();

}

catch

(InterruptedException

e)

{

e.printStackTrace();

}

}

System.out.println(this.getName()

+

"--"

+

this.getContent());

flag

=

false;//保证消费者进入等待状态

super.notify();

}

public

String

getName()

{

return

name;

}

public

void

setName(String

name)

{

this.name

=

name;

}

public

String

getContent()

{

return

content;

}

public

void

setContent(String

content)

{

this.content

=

content;

}

}

//////////////////////////////////////

public

class

Consumer

implements

Runnable

{

private

Gun

gun=null;

public

Consumer

(Gun

gun)

{

this.gun=gun;

}

public

void

run()

{

for(int

i=0;i12;i++)

{

this.gun.get();

}

}

}

///////////////////////////////////////

public

class

Producter

implements

Runnable

{

private

Gun

gun=null;

public

Producter(Gun

gun)

{

this.gun=gun;

}

public

void

run()

{

boolean

flag=false;

for(int

i=0;i12;i++)

{

if(flag)

{

this.gun.set("楼主",

"给枪上了一发子弹");

flag=false;

}

else

{

this.gun.set("我","给了楼主一枪");

flag=true;

}

}

}

}

JAVA求加注解

//导入一个包 主要用来监控控制台的输入

import java.util.Scanner;

public class Mymath

{

//定义一个私有的double变量

private double number;

//构造函数

public Mymath(double number)

{

this.number=number;

}

//将输入的数字转换成正数

public double abs()

{

if(number=0)

return number;

else

number=-number;

return number;

}

//判断传进来的a和number,返回它们之中大的那个数

public double max(double a)

{

if(number=a)

return number;

else

return a;

}

//进制转换

public double gcd(double number,double n)

{

if(n0)

return -1;

else if(number0)

return -1;

else if(n==0)

return number;

else

return gcd (n,number%n);

}

public double pow(double k)

{

double temp=1;

if(k0)

{

for(int p=1;p=k;p++)

temp=number*temp;

}

return temp;

}

public double sqrt()

{

double a = number;

while((a*a-number)0.00001||(number-a*a)0.00001)

{

a=(number/a+a)/2.0;

}

return a;

}

private int factorial(int n)

{

int a=0;

if (n==0)

return 1;

else if (n==1)

return 1;

else

a=n*factorial(n-1);

return a;

}

public double exp()

{

if(number=66)

{

System.out.println("erorr");

return number;

}

else

{

int i=0;

double c=0.0;

for(double k=0.000000001;k(pow(i)/factorial(i));i++)

{

c=c+(pow(i)/factorial(i));

}

return c;

}

}

//主程序入口

public static void main(String []args)

{

//实例化一个Scanner类 并接收控制台输入(System.in:标准输入流)

Scanner console = new Scanner(System.in);

System.out.print( "the first number is : "); //打印语句

double x = console.nextDouble(); //将输入的double赋值给double型的x

//实例化Mymath类

Mymath m = new Mymath (x);

//调用Mymath的abs方法

m.abs();

System.out.println("abs of first number is: "+m.abs());

//调用Mymath的sqrt方法

m.sqrt();

System.out.println("Sqrt the first number = "+m.sqrt());

//调用Mymath的exp方法

m.exp();

System.out.println("exp"+"("+x+")"+"="+m.exp());

//输入第二个数 并比较其与第一个数的大小

Scanner console1 = new Scanner(System.in);

System.out.print( "the second number is : ");

double y = console1.nextDouble();

m.max(y);

System.out.println("between "+x+" and "+y+" the is max is "+m.max(y));

//输入第三个数 并将第一次输入的数转换成第三个数的进制 比如说 如果第三个数是2 那么就是将第一次输入的数转换成2进制数

Scanner console2 = new Scanner(System.in);

System.out.print( "sent a number to get gcd between first number: ");

double z = console2.nextDouble();

m.gcd(x,z);

System.out.println("The gcd is "+m.gcd(x,z));

//输入第四个数 表示阶乘 如果第四个数是2 那么就返回第一个数的2次方的值

Scanner console3 = new Scanner(System.in);

System.out.print( "sent a number pow of the first number: ");

int a = console3.nextInt();

m.pow(a);

System.out.println("The "+x+" to pow "+a+" is "+m.pow(a));

}

}

//大概就是这么些把,注释的并不全,其实都不怎么难,仔细点看看,运行一下就知道了。

//我并没有运行这个程序,所以我注释是基于这个程序并没有错误的基础上的。。。

关于java注解题目和注解Java的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。