package com.ddb.spring.auto; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Component; import javax.annotation.Resource; @Component("boyAnnotation")//相当于写了一个id public class BoyAnnotation { private static Logger logger = Logger.getLogger(BoyAnnotation.class); private UsaGirlFriend usaGirlFriend; private static ApplicationContext ctx; public UsaGirlFriend getUsaGirlFriend(){ return usaGirlFriend; } @Autowired//常用的,根据类型找对应的对象 //@Resource(name = "usaGirlFriend")//注入 public void setUsaGirlFriend(UsaGirlFriend usaGirlFriend) { this.usaGirlFriend = usaGirlFriend; } public static void main(String[] args) { ctx = new ClassPathXmlApplicationContext("applicationContext-auto.xml"); BoyAnnotation boy = (BoyAnnotation) ctx.getBean("boyAnnotation"); logger.info(boy.usaGirlFriend.speak()); } }

wuziayng1232 LV11
2023年5月8日
神龙摆尾无拘束 LV2
2023年3月17日
莫名奇妙的帅 LV4
2023年3月9日
Canvcer LV1
2023年3月1日
马克思
2022年12月5日
暂无贡献等级
冰枫xjz8 LV31
2021年6月12日
鸦葱 LV4
2021年6月11日
1468229607 LV6
2021年5月28日
xb1406112453 LV5
2021年4月15日
cqm0609 LV13
2021年4月3日