当一个类实现了这个接口之后,这个类就可以方便地获得 ApplicationContext 中的所有bean。换句话说,就是这个类可以直接获取Spring配置文件中,所有有引用到的bean对象。结合工厂模式写个demo,比如说ApplicationContext 中有两个发送短信的bean,一个是叫移动的Bean,另一个是叫电信的Bean,那么在某一个场景下,你只要用某一个Bean,那么到底用哪一个取决于你自已,也就是你所传的参数。
1.类ChannelFactory实现了ApplicationContextAware接口
package com.tanlu.user.factory; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; @Component
public class ChannelFactory implements ApplicationContextAware {
private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
} /**
* 获取applicationContext
* @return
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
} public Object getBean(String param) {
return applicationContext.getBean(param);
} /**
* 通过class获取Bean.
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
} public SendMessageChannel getChannel(String code){
switch (code){
case "1":
return applicationContext.getBean(YiDongMobile.class);
case "2":
return applicationContext.getBean(DianXinMobile.class);
default:
return applicationContext.getBean(YiDongMobile.class);
}
} }
2.ApplicationContext 中的两个Bean
package com.tanlu.user.factory; import com.tanlu.user.model.ResponseData;
import org.springframework.stereotype.Component; @Component
public class DianXinMobile implements SendMessageChannel { @Override
public ResponseData sendMsg(String s1) {
ResponseData data = ResponseData.susscce();
data.setMessage("电信");
System.out.println("中国电信发送短信");
return data;
}
}
package com.tanlu.user.factory; import com.tanlu.user.model.ResponseData;
import org.springframework.stereotype.Component; /**
*移动
*/
@Component
public class YiDongMobile implements SendMessageChannel { @Override
public ResponseData sendMsg(String s1) {
ResponseData data = ResponseData.susscce();
data.setMessage("移动");
System.out.println("我是中国移动发送短信");
return data;
}
}
package com.tanlu.user.factory; import com.tanlu.user.model.ResponseData; public interface SendMessageChannel {
/**
* 发送短信
* @param s1
*/
public ResponseData sendMsg(String s1);
}
3.测试,这时候你可以随意注入实现ApplicationContextAware接口的类。
package com.tanlu.user.controller; import com.alibaba.fastjson.JSON;
import com.tanlu.user.factory.ChannelFactory;
import com.tanlu.user.model.ResponseData;
import com.tanlu.user.model.dto.SendMessageDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; @RequestMapping("/message")
@RestController
public class SendMessageController {
private static final Logger logger = LoggerFactory.getLogger(SendMessageController.class); @Autowired
private ChannelFactory channelFactory; /**
* 工厂类demo
* @param dto
* @return
* @throws Exception
*/
@RequestMapping(value = "/channel", method = RequestMethod.POST, produces = "application/json")
public ResponseData sendMsgCode(@RequestBody SendMessageDto dto) throws Exception {
String code = dto.getCode();
logger.info("code :"+code);
ResponseData data = channelFactory.getChannel(code).sendMsg(code);
System.out.println("test applicationcontextaware :"+ JSON.toJSON(data));
return data;
} }