class literal & instance.getClass() & Class.forName(String className)

常用的几种取得Class类实例的方式:
[color=red]1 class literal (class字面量, 如String.class/int.class/void.class)
2 instanceOfClass.getClass();
3 Class.forName(String className)
4 classLoaderInstance.loadClass(String name, boolean resolve)
[/color]
3 4 为显式的动态加载,关于动态加载,我要[size=medium][b]记得看附件中的Understanding Class.forName.pdf![/b][/size]关于ClassLoader的更多知识参阅 [url]http://wuaner.iteye.com/admin/blogs/1011036[/url],这里不再详述。


[b]class literal:
Java Language Specification -> 15.8.2 Class Literals :
[url]http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.8.2[/url][/b][quote]A class literal is an expression consisting of the name of a class, interface, array, or primitive type, or the pseudo-type void, followed by a '.' and the token class.
The type of C.class, where C is the name of a class, interface, or array type (§4.3), is Class<C>.
The type of p.class, where p is the name of a primitive type (§4.2), is Class<B>, where B is the type of an expression of type p after boxing conversion (§5.1.7).
The type of void.class (§8.4.5) is Class<Void>. [/quote]


[b]class字面量[/b]只能作用在type名上(这里的type包括class, interface, array, primitive type,void)!Object的方法getClass()作用在类实例上!它们返回的都是Class类的实例!
对一个Class类的实例clazz,可以一直调用getClass方法!而.class因为不能作用在类实例上,所以不能一直调下去,因为第一次调SomeClass.class时,返回的就已经是个Class类的实例了!


JDK中关于Class类:[quote]public final class Class<T>
extends Object
implements Serializable, GenericDeclaration, Type, AnnotatedElement

Instances of the class Class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.

Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.

The following example uses a Class object to print the class name of an object:

void printClassName(Object obj) {
System.out.println("The class of " + obj +
" is " + obj.getClass().getName());
}


It is also possible to get the Class object for a named type (or for void) using a class literal (JLS Section 15.8.2). For example:

System.out.println("The name of class Foo is: "+Foo.class.getName());
[/quote]
关于Class.forName():[quote]public static Class<?> forName(String name,
boolean initialize,
ClassLoader loader)
throws ClassNotFoundException

Returns the Class object associated with the class or interface with the given string name, using the given class loader. Given the fully qualified name for a class or interface (in the same format returned by getName) this method attempts to locate, load, and link the class or interface. The specified class loader is used to load the class or interface. If the parameter loader is null, the class is loaded through the bootstrap class loader. The class is initialized only if the initialize parameter is true and if it has not been initialized earlier.

If name denotes a primitive type or void, an attempt will be made to locate a user-defined class in the unnamed package whose name is name. Therefore, this method cannot be used to obtain any of the Class objects representing primitive types or void.

If name denotes an array class, the component type of the array class is loaded but not initialized.

For example, in an instance method the expression:

Class.forName("Foo")


is equivalent to:

Class.forName("Foo", true, this.getClass().getClassLoader())


Note that this method throws errors related to loading, linking or initializing as specified in Sections 12.2, 12.3 and 12.4 of The Java Language Specification. Note that this method does not check whether the requested class is accessible to its caller.

If the loader is null, and a security manager is present, and the caller's class loader is not null, then this method calls the security manager's checkPermission method with a RuntimePermission("getClassLoader") permission to ensure it's ok to access the bootstrap class loader.

Parameters:
name - fully qualified name of the desired class
initialize - whether the class must be initialized
loader - class loader from which the class must be loaded
Returns:
class object representing the desired class [/quote]
使用clazz(a instance of Class).newInstance()时要注意:[quote]if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason,将会抛出InstantiationException[/quote]


关于Object的getClass() 方法:[quote]public final Class<?> getClass()

Returns the runtime class of this Object. The returned Class object is the object that is locked by static synchronized methods of the represented class.

The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called. For example, no cast is required in this code fragment:

Number n = 0;
Class<? extends Number> c = n.getClass();

Returns:
The Class object that represents the runtime class of this object.[/quote]


What is a class literal in Java ?
[url]http://stackoverflow.com/questions/2160788/what-is-a-class-literal-in-java[/url]
Understanding Class.forName()(请见附件pdf文档):
[url]http://www.theserverside.com/news/1365412/Understanding-ClassforName-Java[/url]


例子:
public class Parent {

public static void main(String[] args) throws Exception {


Class c1 = void.class;
Class c2 = int.class;
Class c3 = Integer.class;
System.out.println(c2 == c3); //false
//Integer i = (Integer)c3.newInstance(); //会报InstantiationException,原因是Integer没有无参构造方法

Class clazz1 = String.class;
String string = (String)clazz1.newInstance();
string = "ooo";
System.out.println(string);
Class clazz2 = Class.class;
///Class clazz3 = Class.forName("package.MyClass"); //必须处理ClassNotFoundException

String str = "abc";
Class clazz4 = str.getClass();

Class clazz5 = clazz1.getClass().getClass().getClass();
//Class clazz6 = String.class.class; //编译错误

Class clazz7 = System.out.getClass().getClass();
//Class clazz8 = System.out.class; //编译错误

System.out.println(String.class == "abc".getClass()); //true
System.out.println(Class.class == clazz1.getClass()); //true
System.out.println(Class.class == clazz4.getClass()); //true
System.out.println(String.class == "abc".getClass().getClass()); //false
System.out.println(Class.class == clazz7.getClass().getClass()); //true

Parent p = new Parent();
System.out.println(p.getClass() == Parent.class); //true
System.out.println(p.getClass().getClass() == Parent.class); //false

Parent p2 = new Child();
System.out.println(p2.getClass() == Parent.class); //false
System.out.println(p2.getClass() == Child.class); //true
System.out.println(p2.getClass().getClass() == Child.class); //false

//System.out.println(String.class == Parent.class); //编译错误:Incompatible operand types Class<String> and Class<Parent>
System.out.println("abc".getClass() == p.getClass()); //false
}

}

class Child extends Parent {

}



[url]http://juixe.com/techknow/index.php/2006/05/08/javaclass/[/url][quote]In Java, given an object, an instance of a class, you can get the class name by coding the following:

Class clazz = obj.getClass();
String clazzName = clazz.getName();
Sometimes you want you want to create a Class object for a given class. In this case you can do so by writing code similar to the following example:

Class clazz = MyClass.class;
Many plugin frameworks, including the JDBC Driver Manager will create a Class object without having the knowledge of what class name at compile time. There might be a case where you know a class implements a given interface but you don’t know the class name of the implementation until at runtime when you read it from a properties file. In situations like this you can do the following:

String clazzName = "com.juixe.techknow.MyClass";
...
Class clazz = Class.forName(clazzName);
Sometimes you will need a Class object for a primitive type. You might need a Class object for an int or boolean when dealing with reflection. In this case you do so using the dot class notation on a primitive type. Here is a more elaborate example where we create a Class object for an int primitive:

int newValue = ...
Class clazz = obj.getClass();
Method meth = clazz.getMethod("setValue", new Class[]{int.class});
meth.invoke(obj, new Object[]{new Integer(newValue)});
You can also get the class for an array. The only place where I have ever need the class of an array is when working with reflection. Here is how you can get the class for an array:

Class clazz = String[].class;[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
内容介绍 项目结构: Controller层:使用Spring MVC来处理用户请求,负责将请求分发到相应的业务逻辑层,并将数据传递给视图层进行展示。Controller层通常包含控制器类,这些类通过注解如@Controller、@RequestMapping等标记,负责处理HTTP请求并返回响应。 Service层:Spring的核心部分,用于处理业务逻辑。Service层通过接口和实现类的方式,将业务逻辑与具体的实现细节分离。常见的注解有@Service和@Transactional,后者用于管理事务。 DAO层:使用MyBatis来实现数据持久化,DAO层与数据库直接交互,执行CRUD操作。MyBatis通过XML映射文件或注解的方式,将SQL语句与Java对象绑定,实现高效的数据访问。 Spring整合: Spring核心配置:包括Spring的IOC容器配置,管理Service和DAO层的Bean。配置文件通常包括applicationContext.xml或采用Java配置类。 事务管理:通过Spring的声明式事务管理,简化了事务的处理,确保数据一致性和完整性。 Spring MVC整合: 视图解析器:配置Spring MVC的视图解析器,将逻辑视图名解析为具体的JSP或其他类型的视图。 拦截器:通过配置Spring MVC的拦截器,处理请求的预处理和后处理,常用于权限验证、日志记录等功能。 MyBatis整合: 数据源配置:配置数据库连接池(如Druid或C3P0),确保应用可以高效地访问数据库。 SQL映射文件:使用MyBatis的XML文件或注解配置,将SQL语句与Java对象映射,支持复杂的查询、插入、更新和删除操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值