标签:style blog class c code java
Java提供了Class类,可以通过编程方式获取类别的字段和方法,包括构造方法
获取Class类实例的方法:
类名.class
实例名.getClass()
Class.forName(className)
public class RefTest { @Test public void testRef(){ //Class cls = RefTest.class; //Class.forName("com.jboa.service.RefTest"); //new RefTest().getClass(); }
在test写一个测试的实体类:
package com.jboa.service; public class User { private String username; private String password; public User() { } public User(String username, String password) { super(); this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }Spring中是如何根据类名配置创建Bean实例的呢?下面代码:
package com.jboa.service; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import org.junit.Test; public class RefTest { @Test public void testRef() throws Exception { //Class cls = RefTest.class; //Class.forName("com.jboa.service.RefTest"); //new RefTest().getClass(); Object obj = getBean("com.jboa.service.User"); this.setProperty(obj, "username", "admin"); this.setProperty(obj, "password", "passw"); User user = (User) obj; //对应ClassPathXmlApplicationContext.getBean(id) System.out.println(user.getUsername()); System.out.println(user.getPassword()); } public Object getBean(String className) throws Exception { Class cls = null; try { cls = Class.forName(className);//对应Spring ->bean -->class } catch (ClassNotFoundException e) { e.printStackTrace(); throw new Exception("类错误!"); } Constructor[] cons = null;//得到所有构造器 try { cons = cls.getConstructors(); } catch (Exception e) { e.printStackTrace(); throw new Exception("构造器错误!"); } if (cons == null || cons.length < 1) { throw new Exception("没有默认构造方法!"); } //如果上面没错,就有构造方法 Constructor defCon = cons[0];//得到默认构造器,第0个是默认构造器,无参构造方法 Object obj = defCon.newInstance();//实例化,得到一个对象 //Spring - bean -id return obj; } public void setProperty(Object bean, String propertyName, Object propertyValue) throws Exception { Class cls = bean.getClass(); Method[] methods = cls.getMethods();//得到所有方法 //cls.getFields();//所有公开字段属性 //注入属性 用户名:admin setUsername(); // obj username admin //String propertyName = "username";//对应 Spring配置文件- property ->name //String propertyValue = "admin";//对应:Spring -- property -->ref/value for (Method m : methods) { if (m.getName().equalsIgnoreCase("set" + propertyName)) { //找到方法就注入 m.invoke(bean, propertyValue); break; } } } }
运行;
参考的bean:
<bean id="employeeAction" class="com.jboa.action.EmployeeAction" scope="prototype"> <property name="employeeService" ref="employeeService"></property> <property name="dictionaryService" ref="dictionaryService"></property> </bean>
使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法,布布扣,bubuko.com
使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法
标签:style blog class c code java
原文地址:http://blog.csdn.net/jerome_s/article/details/26500575