@Autowired出错

1.补下基础知识

@Configuration可理解为用spring的时候xml里面的标签

@Bean可理解为用spring的时候xml里面的标签 

使用@Autowired要用上面这两个进行注入

2.有些还需要配置相关的数据

spring boot的话,可以在application.properties上配置数据

@ConfigurationProperties(prefix = "memcache") 

写一个类的属性和application.properties里面的memcache.xx匹配

#缓存机制配置
memcache.servers=127.0.0.1:11211
memcache.weights=5
memcache.initConn=20
memcache.minConn=10
memcache.maxConn=50
memcache.maintSleep=3000
memcache.nagle=false
memcache.socketTO=3000
@Component
@ConfigurationProperties(prefix = "memcache")
public class SockIOPoolConfig {
    private String[] servers;

    private Integer[] weights;

    private int initConn;

    public String[] getServers() {
        return servers;
    }

    public void setServers(String[] servers) {
        this.servers = servers;
    }

    public Integer[] getWeights() {
        return weights;
    }

    public void setWeights(Integer[] weights) {
        this.weights = weights;
    }

    public int getInitConn() {
        return initConn;
    }

    public void setInitConn(int initConn) {
        this.initConn = initConn;
    }

    public int getMinConn() {
        return minConn;
    }

    public void setMinConn(int minConn) {
        this.minConn = minConn;
    }

    public int getMaxConn() {
        return maxConn;
    }

    public void setMaxConn(int maxConn) {
        this.maxConn = maxConn;
    }

    public long getMaintSleep() {
        return maintSleep;
    }

    public void setMaintSleep(long maintSleep) {
        this.maintSleep = maintSleep;
    }

    public boolean isNagle() {
        return nagle;
    }

    public void setNagle(boolean nagle) {
        this.nagle = nagle;
    }

    public int getSocketTO() {
        return socketTO;
    }

    public void setSocketTO(int socketTO) {
        this.socketTO = socketTO;
    }

    private int minConn;

    private int maxConn;

    private long maintSleep;

    private boolean nagle;

    private int socketTO;
}
然后再用@Autowired SockIOPoolConfig获取属性值,

再用@Configuration @Bean进行注入

最后就可以用@Autowired使用这个类

@Autowired
	MemCachedClient memCachedClient;



你可能感兴趣的:(java)