spring注解不支持静态变量注入:今天敲代码  自动配置 配置:

Animal.java
 package study01_autoconfig.beanConfig;

 import org.springframework.stereotype.Component;

 @Component
public class Person implements Animal{
private String name; public void talk() {
name="linhua";
System.out.println("hi I'm "+name);
}
}

Person.java继承Anima

 package study01_autoconfig.beanConfig;

 import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @ComponentScan
@Configuration
public class PersonConfig { }

配置类

测试类:

package study01_autoconfig.beanConfig;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class) //不写这个会报错。。。。。
@ContextConfiguration(classes=PersonConfig.class)
public class PersonTest {
@Autowired
//spring annotation不支持静态变量注入 static Person per; //当为静态变量注入时,会报Autowired annotation is not supported on static fields
@Test
public void test1() {
per.talk();
}
//不能在main方法里面,否则会报空指针
/*public static void main(String[] args) {
per.talk();
}*/ }

然后发现  ,spring注解不支持静态变量注入

spring注解不支持静态变量注入的更多相关文章

  1. Spring如何给静态变量注入值

    Common.java是一个工具类. Spring无法直接给静态变量注入值,因为静态变量不属于对象,只属于类,也就是说在类被加载字节码的时候变量已经初始化了,也就是给该变量分配内存了,导致spring ...

  2. spring boot 静态变量注入配置文件

    spring 静态变量注入 spring 中不支持直接进行静态变量值的注入,我们看一下代码: @Component(value = "KafkaConfig") @Configur ...

  3. Spring不能直接@autowired注入Static变量/ 关于SpringBoot的@Autowired 静态变量注入

    昨天在编写JavaMail工具类的时候,静态方法调用静态变量,这是很正常的操作,当时也没多想,直接静态注入. @Component public class JavaMailUtil { @Autow ...

  4. Spring boot 工具类静态属性注入及多环境配置

    由于需要访问MongoDB,但是本地开发环境不能直接连接MongoDB,需要通过SecureCRT使用127.0.0.2本地IP代理.但是程序部署到线上生产环境后,是可以直接访问MongoDB的,因此 ...

  5. spring 给静态变量注入值

    一般在spring中,给static变量加上@Autowired注解的时候会报空指针异常错误. 解决: 1.通过xml配置文件配置 这个就不多说了. 2.通过注解 @Component public ...

  6. SpringMvc通过@Value( ) 给静态变量注入值

    spring 不允许/不支持把值注入到静态变量中,如: @Value("${ES.CLUSTER_NAME}")private static String CLUSTER_NAME ...

  7. spring注解(Component、依赖注入、生命周期、作用域)

    1.注解 注解就是一个类,使用@加上注解名称,开发中可以使用注解取代配置文件 2.@Component 取代<bean  class="">,@Component 取代 ...

  8. 使用spring注解——定义bean和自动注入

    对于java bean的定义和依赖配置,使用xml文件真心是不方便. 今天学习如何用注解,解决bean的定义和注入. 常用注解: 1.自动注入:@Resources,@Autowired 2.Bean ...

  9. 参考 - spring boot 静态变量注入值

    参考http://blog.csdn.net/zhayuyao/article/details/78553417 @Component public class A { private static ...

随机推荐

  1. 【jar包管理】Maven BOM

    BOM Alibaba Spring Boot Dependencies is a Maven BOM used to manage the versions of most used Alibaba ...

  2. Enum的简单扩展

    1 添加一个描述的Attribute public enum MessageResult { [System.ComponentModel.Description("未通过")] ...

  3. 使用Appium做手机自动化录制问题

    最近在使用appium做Android手机自动化脚本录制, 发现点击“tap”时,一直没有用,页面还是不能跳转. 咋办?发愁... 于是看到旁边有个“sendkeys”,那是不是能够直接发送参数不就行 ...

  4. 线性模型之LDA和PCA推导

    线性模型之LDA和PCA 线性判别分析LDA LDA是一种无监督学习的降维技术. 思想:投影后类内方差最小,类间方差最大,即期望同类实例投影后的协方差尽可能小,异类实例的投影后的类中心距离尽量大. 二 ...

  5. 并发编程-concurrent指南-原子操作类-AtomicReference

    1.类 AtomicReference<V> public class AtomicReference<V>extends Objectimplements Serializa ...

  6. kuangbin专题 专题一 简单搜索 Fire Game FZU - 2150

    题目链接:https://vjudge.net/problem/FZU-2150 题意:’ . '代表火无法烧着的地方,‘ # ’表示草,火可以烧着.选择任意两个‘ # ’(可以两个都选同一个 ‘ # ...

  7. 嵊州D1T4 cf1174E 占梦人

    嵊州D1T4 cf1174E 此题为改编题,原题:cf1174E 占梦人 占梦人一个晚上会做 n 个梦,编号为 1 ∼ n,她可以安排做这 n 个梦的顺序. 假如她第一个做了编号为 x 的梦,那么她的 ...

  8. 哈工大计算机网络Week1-网络应用

    目录 哈工大计算机网络Week1-网络应用 2.1网络应用的体系结构 特点 应采取什么结构 C/S结构 客户机/服务器 P2P CS vs P2P 混合结构 思考题目 2.2网络应用的基本原理 网络应 ...

  9. PostgreSQL 窗口函数 ( Window Functions ) 如何使用?

    一.为什么要有窗口函数 我们直接用例子来说明,这里有一张学生考试成绩表testScore: 现在有个需求,需要查询的时候多出一列subject_avg_score,为此科目所有人的平均成绩,好跟每个人 ...

  10. 理解 Spring 定时任务的 fixedRate 和 fixedDelay 的区别

    用过 Spring 的 @EnableScheduling 的都知道,有三种方式,即 @Scheduled 注解的 fixedRate(fixedRateString), fixedDelay(fix ...