用注解方式,配置web.xml中的内容
源代码:
package day52.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = “Demo01Servlet”,
urlPatterns = {"/demo01","/mydemo01"},
loadOnStartup = 1,
initParams = {@WebInitParam(name=“username”,value=“root”),
@WebInitParam(name=“password”,value=“root123”)})
public class Demo01Servlet extends HttpServlet {
@Override
public void init() throws ServletException {
System.out.println(“Demo01Servlet init”);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Demo01Servlet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
利用反射突破泛型封装
源代码:
package Demo;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class Demo09 {
public static void main(String[] args) throws Exception {
//创建List集合对象
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
System.out.println(list);
//泛型只在编译期有效!
//反射越过泛型检查
//反射可以在程序运行时,动态地调用List中的add方法去添加元素
Class<? extends List> clazz = list.getClass();
Method add = clazz.getDeclaredMethod("add",Object.class);
add.setAccessible(true);
Object result = add.invoke(list,"hello , generic type !");
System.out.println(list);
}
}
运行效果图:
反射操作成员变量:
源代码:
package Demo;
import bean.User;
import java.lang.reflect.Field;
public class Demo07 {
public static void main(String[] args) throws Exception {
//获取User类的Class对象
Class<User> clazz = User.class;
User user = clazz.newInstance();
//操作public修饰的成员变量
Field idField = clazz.getField("id");
//设置该成员变量值
//obj:需要设置的对象
//value:需要设置的值
//给user对象的id 属性值设置为250
idField.set(user,250);
System.out.println(user);
//获取该成员变量值
Object idValue = idField.get(user);
//操作非public修饰的成员变量
Field usernameField = clazz.getDeclaredField("username");
usernameField.setAccessible(true);
usernameField.set(user,"坤坤");
System.out.println(user);
Object usernameValue = usernameField.get(user);
System.out.println(usernameValue);
}
}
运行效果图:
反射操作成员方法:
源代码:
package Demo;
import bean.User;
import java.lang.reflect.Method;
/**
- 反射操作成员方法
*/
public class Demo08 {
public static void main(String[] args) throws Exception {
//获取User类的Class对象
Class<User> clazz = User.class;
User user = clazz.newInstance();
//获取public修饰的成员方法
Method method01 = clazz.getMethod("setPassword", String.class);
//使用方法对象
//obj:哪个对象在执行该方法
//args:方法执行时所需要的参数值
method01.invoke(user,"123456");
System.out.println(user);
//操作非public修饰的成员的方法
Method method02 = clazz.getDeclaredMethod("show");
method02.setAccessible(true);
Object result = method02.invoke(user);
System.out.println(result);
}
}
执行效果图:
反射操作构造方法:
源代码:
package Demo;
import bean.User;
import java.lang.reflect.Constructor;
/**
- 反射操作构造方法
*/
public class Demo06 {
public static void main(String[] args) throws Exception {
//获取User类对应的Class对象
Class<?> clazz = Class.forName("bean.User");
//获取无参构造方法对象
Constructor<?> c1 = clazz.getConstructor();
//使用无参创建User类对象
Object obj1 = c1.newInstance();
System.out.println(obj1);
System.out.println("------------------");
//获取User类对应的有参构造方法对象
Constructor<?> c2 =clazz.getConstructor(Integer.class,String.class,String.class);
//使用有参创建User对象
Object obj2 = c2.newInstance(1,"张三","root");
System.out.println(obj2);
System.out.println("------------------");
Constructor<?> c3 = clazz.getDeclaredConstructor(String.class,String.class);
//暴力反射,让私有构造器对象可以被外部访问
c3.setAccessible(true);
Object obj3 = c3.newInstance("李四","20200202");
System.out.println(obj3);
}
}
运行效果图:
反射结合配置文件:
源代码:
package Demo;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
-
反射结合配置文件
*/
public class Demo11 {
public static void main(String[] args) throws Exception {//需求:编写bean.properties,配置对象的唯一标识及对象的全类名,根据这段配置创建一个对象 Properties properties = new Properties(); //将bean.properties中的数据存储到inputStream中 InputStream inputStream = Demo11.class.getClassLoader().getResourceAsStream("bean.properties"); //将bean.properties中的数据绑定到了Properties中! properties.load(inputStream); //获取全类名 String className = properties.getProperty("bean01"); //根据上述全类名创建了一个对象 Object obj = Class.forName(className).newInstance(); System.out.println(obj);
}
}
运行效果图:
反射案例
给指定对象的指定属性设置指定值
源代码:
package Demo;
import bean.User;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
-
反射案例
-
给指定对象的指定属性设置指定值
*/
public class Demo10 {
public static void main(String[] args) throws Exception {
User user = new User();
setValue(user,“id”,123);
System.out.println(user);}
public static void setValue(Object obj,String fieldName,Object value) throws Exception {
Class<?> clazz = obj.getClass();
//username setUsername
//根据属性名称获取对应的set方法名称
//String methodName = “set”+“U”+“sername”
String methodName = “set”+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);
Field field = clazz.getDeclaredField(fieldName);
//获取字段的数据类型
Class<?> fieldType = field.getType();
//获取到set方法对象
Method method = clazz.getMethod(methodName,fieldType);
//执行set方法
method.invoke(obj,value);}
}
运行效果图: