1、定义成员变量
package com.example.demo.controller;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.Map;
/**
* @Description: TODO
* @Author: Top
* @Version: V1.0
* @Date: 2020-01-15 15:03
*/
@RestController
@RequestMapping("/api/{edition}/page2")
public class MyPageHelperController2 {
@Autowired
private Environment env;
@Autowired
private UserService userServiceImpl;
private String str1;
private static String str2;
@GetMapping("/myPage3")
@ResponseBody
public Map<Object, Object> expenseStatement(Object str) throws IOException {
str1 = "zhangjiguo";
System.out.println(str1);
str2 = "guoguo";
System.out.println(str2);
return null;
}
@GetMapping("/myPage4")
@ResponseBody
public Map<Object, Object> expenseStatement2(Object str) throws IOException {
System.out.println(str1);
System.out.println(str2);
return null;
}
@GetMapping("/myPage5")
@ResponseBody
public Map<Object, Object> expenseStatement3(Object str) throws IOException {
str1 = "zhangsan";
str2 = "jiji";
System.out.println(str1);
System.out.println(str2);
return null;
}
}
2、定义工具类,从容器中获取某个bean
package com.example.demo.common;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
/**
* 通过name获取 Bean.
*
* @param name
* @return
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
/**
* 通过class获取Bean.
*
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
/**
* 通过name及Clazz返回指定的Bean
*
* @param name
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
@Autowired
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtils.applicationContext = applicationContext;
}
/**
* 获取applicationContext
*
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
}
3、修改成员变量
@RequestMapping("ok")
public Object test2(){
ApplicationContext applicationContext = SpringContextUtils.getApplicationContext();
String[] beans = applicationContext.getBeanDefinitionNames();
for (String beanName : beans) {
// 拿到bean的Class对象
Class<?> beanType = applicationContext.getType(beanName);
if (beanType == null) {
continue;
}
// 拿到当前bean类型的所有字段
Field[] declaredFields = beanType.getDeclaredFields();
if(!beanName.contains("myPageHelperController2") ){
continue;
}
for (Field field : declaredFields) {
// 从spring容器中拿到这个具体的bean对象
Object bean = applicationContext.getBean(beanName);
// 当前字段设置新的值
try {
setFieldData(field, bean, "ffffff");
System.out.println("finished");
} catch (Exception e) {
e.printStackTrace();
}
}
}
return "ok";
}
private void setFieldData(Field field, Object bean, String data) throws Exception {
// 注意这里要设置权限为true
field.setAccessible(true);
Class<?> type = field.getType();
if (type.equals(String.class)) {
field.set(bean, data);
} else if (type.equals(Integer.class)) {
field.set(bean, Integer.valueOf(data));
} else if (type.equals(Long.class)) {
field.set(bean, Long.valueOf(data));
} else if (type.equals(Double.class)) {
field.set(bean, Double.valueOf(data));
} else if (type.equals(Short.class)) {
field.set(bean, Short.valueOf(data));
} else if (type.equals(Byte.class)) {
field.set(bean, Byte.valueOf(data));
} else if (type.equals(Boolean.class)) {
field.set(bean, Boolean.valueOf(data));
} else if (type.equals(Date.class)) {
field.set(bean, new Date(Long.valueOf(data)));
}
}
示例二:
import java.lang.reflect.Field;
public class Main {
public static void main(String[] args) throws Exception {
// 创建一个Person类的实例
Person person = new Person("John", 25);
// 获取Person类的Class对象
Class<?> clazz = person.getClass();
// 获取name字段并设置新值
Field nameField = clazz.getDeclaredField("name");
nameField.setAccessible(true);
nameField.set(person, "Tom");
// 输出修改后的结果
System.out.println(person.getName()); // 输出"Tom"
// 获取age字段并设置新值
Field ageField = clazz.getDeclaredField("age");
ageField.setAccessible(true);
ageField.setInt(person, 30);
// 输出修改后的结果
System.out.println(person.getAge()); // 输出30
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}