直接上代码
方式1:
//可以换成@Configuration,与@Inject配合使用
@Componentpublic
class XXUtils {
//可以换成@Inject
@Resource
private XXXProperties xxxPropertiesAutowired;
private static XXXProperties xxxProperties;
@PostConstruct
public void init() {
this.xxxPropertiesAutowired = xxxProperties;
}
}
@Component
public class UrlUtil {private static RRJConfig rrjConfig;
@Autowired
public void setRRJConfig(RRJConfig rRJConfig) {
UrlUtil.rrjConfig = rRJConfig;
}
本文介绍了两种在Spring框架中实现依赖注入的方法。第一种是通过@Component注解定义一个实用类,并利用@Resource或@Autowired进行属性注入,结合@PostConstruct初始化静态成员变量。第二种方法则是通过将@Autowired的实例设置为静态类成员来完成依赖注入。
398

被折叠的 条评论
为什么被折叠?



