public class Teacher implements ApplicationContextAware {
private ApplicationContext context;
private List<Course> courses = new ArrayList<>();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
@PostConstruct
public void addCourse() {
if (context.containsBean("math")) {
Course math = context.getBean("math", Course.class);
courses.add(math);
}
if (context.containsBean("physics")) {
Course physics = context.getBean("physics", Course.class);
courses.add(physics);
}
}
// standard constructors, getters and setters
}
public class MyClass implements ApplicationContextAware {
static final long serialVersionUID = 02L;
ApplicationContext applicationContext = null;
public void doSomething(){
if (applicationContext != null && applicationContext.containsBean("accessKeys")){
MyBean beanA = (MyBean) applicationContext.getBean("mybean");
//Do something with this AccessBean
}
return null;
}
@Override
public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
System.out.println("setting context");
this.applicationContext = applicationContext;
}
}
public class gzservlet extends HttpServlet {
static final long serialVersionUID = 02L;
ApplicationContext applicationContext = null;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (applicationContext == null){
System.out.println("setting context in get");
applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
}
if (applicationContext != null && applicationContext.containsBean("accessKeys")){
AccessBean thisAccessBean = (AccessBean) applicationContext.getBean("accessKeys");
req.setAttribute("keys", thisAccessBean.toString());
System.out.println("setting keys");
}
req.getRequestDispatcher("/index2.jsp").include(req,resp);
}
}
@Configuration
@ComponentScan(value = "guru.springframework.blog.componentscan.example.demopackageA",
useDefaultFilters = false)
public class BlogPostsApplicationDisablingDefaultFilters {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.
run(BlogPostsApplicationDisablingDefaultFilters.class,args);
System.out.println("Contains A " + context.containsBean("demoBeanA"));
}
}
public void testAspectJTypeFilter() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"org/springframework/context/annotation/aspectjTypeFilterTests.xml");
assertTrue(context.containsBean("fooServiceImpl"));
assertTrue(context.containsBean("stubFooDao"));
assertFalse(context.containsBean("scopedProxyTestBean"));
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
if (StringUtils.isEmpty(userCacheBean)) {
if (applicationContext.containsBean(userServiceBean)) {
userDetailsService = (UserDetailsService) applicationContext
.getBean(userServiceBean);
} else {
throw new RuntimeException("Can not found spring bean["
+ userServiceBean + "] implements "
+ UserDetailsService.class.getName() + " interface!");
}
} else {
if (applicationContext.containsBean(userCacheBean)) {
userCache = (UserCache) applicationContext
.getBean(userServiceBean);
} else {
throw new RuntimeException("Can not found spring bean["
+ userCacheBean + "] implements "
+ UserCache.class.getName() + " interface!");
}
}
}
public <T> T getExtension(Class<T> type, String name) {
for (ApplicationContext context : contexts) {
if (context.containsBean(name)) {
Object bean = context.getBean(name);
if (type.isInstance(bean)) {
return (T) bean;
}
}
}
return null;
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
boolean hasBean=applicationContext.containsBean(KnowledgePackageService.BEAN_ID);
if(hasBean){
knowledgePackageService=(KnowledgePackageService)applicationContext.getBean(KnowledgePackageService.BEAN_ID);
}
}
public VideoCodecFactory getVideoCodecFactory(){
final IContext context=scope.getContext();
ApplicationContext appCtx=context.getApplicationContext();
if (!appCtx.containsBean(VIDEO_CODEC_FACTORY)) {
return null;
}
return (VideoCodecFactory)appCtx.getBean(VIDEO_CODEC_FACTORY);
}
protected static Object getScopeService(IScope scope,String name,Class<?> defaultClass){
if (scope == null) {
return null;
}
final IContext context=scope.getContext();
ApplicationContext appCtx=context.getApplicationContext();
Object result;
if (!appCtx.containsBean(name)) {
if (defaultClass == null) {
return null;
}
try {
result=defaultClass.newInstance();
}
catch ( Exception e) {
log.error("{}",e);
return null;
}
}
else {
result=appCtx.getBean(name);
}
return result;
}