java获取本类泛型_java获取泛型类型

Java获取泛型类型方法:

通常获取泛型的类型名称,然后组装,再根据这个名称从spring容器拿到一个Bean;比如

@Service("baseService")

public class BaseService implements BaseMapper{

private String mapper;

@Override

public int insert(T t) {

BaseMapper baseMapper= (BaseMapper) SysConfigHelper.getAppContext().getBean(t.getClass().getSimpleName().toLowerCase()+"Mapper");

return baseMapper.insert(t);

}

} 像上面的T一样,可以拿到T的类型值为 User

即 t.getClass().getSimpleName().toLowerCase()+"Mapper" 的结果为【userMapper】

但是delete方法的时候,这种方式却是不行的

@Override

public int deleteById(int id) {

T t = null;

BaseMapper baseMapper= (BaseMapper) SysConfigHelper.getAppContext().getBean(t.getClass().getSimpleName().toLowerCase()+"Mapper");

return baseMapper.deleteById(id);

}

这样写会抛异常,因为t是为nul

我们可以这些写

@Override

public int update(T t) {

Type type = getClass().getGenericSuperclass();

Type trueType = ((ParameterizedType) type).getActualTypeArguments()[0];

Class entityClass=(Class) trueType;

System.out.println(type);

System.out.println(trueType);

System.out.println(entityClass.getSimpleName());

BaseMapper baseMapper= (BaseMapper) SysConfigHelper.getAppContext().getBean(entityClass.getSimpleName().toLowerCase()+"Mapper");

return baseMapper.update(t);

}

控制台打印的结果为:

com.zte.framework.jdbc.BaseService

class com.zte.user.domain.User

User

这样就拿到了泛型T的类型了;值为【User】

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 1. 通过getBean()方法获取bean 2. 通过@Autowired和@Qualifier注解获取bean 3. 通过@Resource注解获取bean 4. 通过@Inject注解获取bean 5. 通过Java Config类中的@Bean注解获取bean 6. 通过Java Config类中的@Configuration注解获取bean 7. 通过Java Config类中的@Import注解获取bean 8. 通过Java Config类中的@ImportResource注解获取bean 9. 通过XML配置文件中的<bean>标签获取bean ### 回答2: Spring获取Bean的9种方式: 1. 通过ApplicationContext获取:可以直接通过ApplicationContext类的getBean()方法来获取Bean实例。例如:ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); MyBean bean = (MyBean) context.getBean("myBean"); 2. 通过BeanFactory获取BeanFactory是Spring的最基本的工厂接口,可以通过BeanFactory来获取Bean实例。例如:BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); MyBean bean = (MyBean) factory.getBean("myBean"); 3. 通过@Autowired注解获取:使用@Autowired注解可以自动装配Bean实例。例如:@Autowired MyBean bean; 4. 通过@Resource注解获取:使用@Resource注解可以指定要装配的Bean实例。例如:@Resource(name = "myBean") MyBean bean; 5. 通过Java配置类获取:使用@Configuration注解创建一个Java配置类,然后在配置类中使用@Bean注解将Bean实例化并返回。例如:@Configuration public class AppConfig { @Bean public MyBean myBean() { return new MyBean(); } } ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); MyBean bean = (MyBean) context.getBean("myBean"); 6. 通过@Qualifier注解获取:使用@Qualifier注解可以指定Bean的名称,用于解决多个同类Bean的装配问题。例如:@Autowired @Qualifier("myBean") MyBean bean; 7. 通过构造函数获取:可以在类的构造函数中注入依赖的Bean实例。例如:public class MyClass { private MyBean myBean; public MyClass(MyBean myBean) { this.myBean = myBean; } } 8. 通过静态工厂方法获取:可以使用静态工厂方法来创建Bean实例。例如:public class MyFactory { public static MyBean createBean() { return new MyBean(); } } MyBean bean = MyFactory.createBean(); 9. 通过实例工厂方法获取:可以在实例工厂类中创建Bean实例。例如:public class MyFactory { public MyBean createBean() { return new MyBean(); } } MyFactory factory = new MyFactory(); MyBean bean = factory.createBean(); ### 回答3: Spring获取Bean的方式有以下9种: 1. 通过注解:使用@Autowired或者@Inject注解,在类的属性或者构造方法上标注,Spring会自动将对应的Bean注入进来。 2. 通过配置文件:在Spring的配置文件中,使用<bean>标签定义Bean对象,并指定其对应的类名。 3. 通过ApplicationContext:通过ApplicationContext接口的getBean方法,传入Bean的名称获取Bean对象。 4. 通过BeanFactory:通过BeanFactory接口的getBean方法,传入Bean的名称获取Bean对象。BeanFactory是ApplicationContext的父接口。 5. 通过实现FactoryBean接口:自定义一个FactoryBean实现类,在getObject方法中返回具体的Bean对象。 6. 通过Bean的别名:在Spring的配置文件中,通过<alias>标签为Bean设置一个别名,然后通过别名获取Bean对象。 7. 通过泛型类:在初始化Bean的时候,使用泛型指定Bean的类,然后调用getBean方法获取Bean对象。 8. 通过Bean的作用域:在Spring的配置文件中,可以为Bean设置不同的作用域,如单例(Singleton)、原(Prototype)等,在需要使用Bean的地方获取相应的作用域的Bean对象。 9. 通过名称和类:可以根据Bean的名称和类,通过getBean方法获取Bean对象。 以上是Spring获取Bean的9种常见方式,可以根据具体的场景选择合适的方式来获取Bean对象。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值