关于spring中environment注入为null的一点理解

最近在学习spring的源码 ,在学习中经常出现一件事,就是自动注入的某些属性不知道忘了那些配置经常会出现null的现象,现在做一些统一整理:

首先:spring中的bean从注入方式上会分成三种,一种是普通的javaBean,就是自己在配置文件中写好的放在容器中的,在一种就是系统环境bean,包括三个,Environment,SystemPropertits,SystemEnvironment,还有一种是实现了XXXAware接口的bean,

第一种不解释

第二种,这种bean不是我们自己创建的,而是spring帮我们自动创建的,spring代码如下:

        // Register default environment beans.
        if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) {
            beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment());
        }
        if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
            beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties());
        }
        if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) {
            beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment());
        }

儿上面的getEnvironment()方法的代码如下:

public ConfigurableEnvironment getEnvironment() {
   if (this.environment == null) {
      this.environment = createEnvironment();
   }
   return this.environment;
}
protected ConfigurableEnvironment createEnvironment() {
   return new StandardEnvironment();
}

默认会返回一个Environment接口的实现StandardEnvironment;用来存贮我们所需要的属性;

第三种,实现XXXAware接口的类

代码如下:

private void invokeAwareMethods(String beanName, Object bean) {
    if (bean instanceof Aware) {
        if (bean instanceof BeanNameAware) {
            ((BeanNameAware)bean).setBeanName(beanName);
        }

        if (bean instanceof BeanClassLoaderAware) {
            ((BeanClassLoaderAware)bean).setBeanClassLoader(this.getBeanClassLoader());
        }

        if (bean instanceof BeanFactoryAware) {
            ((BeanFactoryAware)bean).setBeanFactory(this);
        }
    }

}

這是系統默认的几个Aware,其他的Aware实现与之类似,具体使用方法如下:

public class Student  implements EnvironmentAware {
    Environment environment;

    private int id;
    public Student(int id) {
        this.id=id;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment=environment;
    }
}

测试代码

public class Main {

    public static void main (String[] args){
        ApplicationContext context=new ClassPathXmlApplicationContext("classpath:resources\\META-INF\\spring\\camel-context.xml");
        System.out.println(((Student)context.getBean("xiaoming")).environment==null);
        
    }
}

结果:

原理就是使用了个回调方法,也没啥说的

这三种方式,在不显式声明使用自动注入的情况下,第一种与第二种将不会注入到自己创建的javaBean之中,在开启自动注入的情况下,也就是说在配置文件中添加

component-scan

子元素,前两种的属性将会注入到所创建的javaBean之中;

总结,当所创建的javaBean之中属性出现null时,考虑如下解决方式:

1.仔细查看配置文件与配置类,是否声明了相关的类型,且

constructor-arg 
property 等配置文件的相关子元素是否配置得当

IDEA编译器为spring提供了相当强的插件支持,建议换个编译器~~~~

2.若是属性在配置文件或者配置类里面已经声明且符合唯一约束;

3.若是使用了@Autowire等相关注解,查看是否开启了自动注解,开启自动注解方法是;在相关的配置文件或者配置类里面声明

<context:component-scan/>

或者是

@ComponentScan 注解

4。此时会解决绝大部分null问题,当仍为null,考虑实现相关的Aware接口,比如ApplicationContextAware,此时他会将ApplicationContext注入到你的javaBean中,而一般情况下他不会给你自动注入的;

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当在 Job 类注入 Spring Bean 时,需要使用 Spring 提供的 JobFactory 来创建 Job 实例,这样 Job 类的 Bean 才能被正确注入。具体实现步骤如下: 1. 创建一个实现了 Spring 的 JobFactory 接口的类,用于创建 Job 实例。 ```java import org.quartz.Job; import org.quartz.Scheduler; import org.quartz.SchedulerContext; import org.quartz.spi.JobFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; @Component public class AutowiringSpringBeanJobFactory implements JobFactory { @Autowired private ApplicationContext context; /** * {@inheritDoc} */ @Override public Job newJob(TriggerFiredBundle bundle, Scheduler scheduler) throws SchedulerException { final Job job = (Job) context.getBean(bundle.getJobDetail().getJobClass()); final SchedulerContext schedulerContext = scheduler.getContext(); schedulerContext.put("applicationContext", context); return job; } } ``` 2. 在 Quartz 配置注册 JobFactory,如下所示: ```java import javax.sql.DataSource; import org.quartz.JobDetail; import org.quartz.Scheduler; import org.quartz.Trigger; import org.quartz.impl.StdSchedulerFactory; import org.quartz.impl.jdbcjobstore.JobStoreTX; import org.quartz.impl.jdbcjobstore.PostgreSQLDelegate; import org.quartz.impl.jdbcjobstore.StdJDBCDelegate; import org.quartz.impl.jdbcjobstore.StdJDBCJobStore; import org.quartz.impl.jdbcjobstore.oracle.OracleDelegate; import org.quartz.spi.JobFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; @Configuration public class QuartzConfig { @Autowired private Environment env; @Bean public JobFactory jobFactory(ApplicationContext applicationContext) { AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory(); jobFactory.setApplicationContext(applicationContext); return jobFactory; } @Bean public Scheduler scheduler(Trigger[] triggers, @Qualifier("quartzDataSource") DataSource dataSource) throws SchedulerException { StdSchedulerFactory factory = new StdSchedulerFactory(); factory.initialize(quartzProperties()); Scheduler scheduler = factory.getScheduler(); scheduler.setJobFactory(jobFactory); scheduler.setDataSource(dataSource); scheduler.setQuartzProperties(quartzProperties()); scheduler.start(); for (Trigger trigger : triggers) { scheduler.scheduleJob(trigger.getJobDetail(), trigger); } return scheduler; } @Bean public JobDetail jobDetail() { return JobBuilder.newJob().ofType(MyJob.class) .storeDurably().withIdentity("MyJob").withDescription("Invoke My Job service...").build(); } @Bean public Trigger trigger(JobDetail job) { return TriggerBuilder.newTrigger().forJob(job) .withIdentity("MyJobTrigger").withDescription("My Job trigger").withSchedule(CronScheduleBuilder.cronSchedule(env.getProperty("myjob.cron.expression"))).build(); } @Bean public Properties quartzProperties() { Properties properties = new Properties(); properties.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME, env.getProperty("scheduler.instance.name")); properties.setProperty(StdSchedulerFactory.PROP_SCHED_INSTANCE_ID, env.getProperty("scheduler.instance.id")); properties.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_CLASS, env.getProperty("scheduler.threadPool.class")); properties.setProperty(StdSchedulerFactory.PROP_THREAD_POOL_THREAD_COUNT, env.getProperty("scheduler.threadPool.threadCount")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_CLASS, env.getProperty("scheduler.jobStore.class")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_DRIVER_DELEGATE_CLASS, env.getProperty("scheduler.jobStore.driverDelegateClass")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_USE_PROPERTIES, env.getProperty("scheduler.jobStore.useProperties")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_DATASOURCE, env.getProperty("scheduler.jobStore.dataSource")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_TABLE_PREFIX, env.getProperty("scheduler.jobStore.tablePrefix")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_IS_CLUSTERED, env.getProperty("scheduler.jobStore.isClustered")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_CLUSTER_CHECKIN_INTERVAL, env.getProperty("scheduler.jobStore.clusterCheckinInterval")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_MAX_MISFIRES_TO_HANDLE_AT_A_TIME, env.getProperty("scheduler.jobStore.maxMisfiresToHandleAtATime")); properties.setProperty(StdSchedulerFactory.PROP_JOB_STORE_MISFIRE_THRESHOLD, env.getProperty("scheduler.jobStore.misfireThreshold")); return properties; } } ``` 3. 在 Job 类注入 Bean,如下所示: ```java import org.quartz.Job; import org.quartz.JobExecutionContext; import org.quartz.JobExecutionException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; @Component public class MyJob implements Job { @Autowired private MyService myService; /** * {@inheritDoc} */ @Override public void execute(JobExecutionContext context) throws JobExecutionException { ApplicationContext applicationContext = (ApplicationContext) context.getScheduler().getContext().get("applicationContext"); applicationContext.getAutowireCapableBeanFactory().autowireBean(this); myService.doSomething(); } } ``` 通过这种方式,就可以在 Quartz 的 Job 类注入 Spring Bean。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值