Spring中提供一些Aware相关接口,像是BeanFactoryAware、 ApplicationContextAware、ResourceLoaderAware、ServletContextAware等等,实现这些 Aware接口的Bean在被初始之后,可以取得一些相对应的资源。例如实作ApplicationContextAware的Bean,在Bean被初始后,将会被注入 ApplicationContext的实例等等。 Bean取得BeanFactory、ApplicationContextAware的实例目的是什么,一般的目的就是要取得一些档案资源的存取、相 关讯息资源或是那些被注入的实例所提供的机制
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContext; import org.springframework.beans.BeansException; public class GlobalParamUtils implements ApplicationContextAware { private static ApplicationContext ac; public void setApplicationContext(ApplicationContext arg0) throws BeansException { this.ac = arg0; } public static String generateCodeByType(String type) { String result = ""; if (ac.containsBean("swmsBussinessCodeManager")) { BussinessCodeManager codeManager = (BussinessCodeManager) ac.getBean("swmsBussinessCodeManager"); try { Method method = codeManager.getClass().getMethod(type, new Class[0]); result = (String) method.invoke(codeManager, new Object[0]); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } return result; } }
//swmsBussinessCodeManager在bean中的配置
<bean class="com.vtradex.swms.server.service.GlobalParamUtils"/> <bean id="swmsBussinessCodeManager" parent="abstractTransactionProxy"> <property name="target"> <bean class="com.vtradex.swms.server.service.sequence.pojo.DefaultBussinessCodeManager" parent="baseManager"> <constructor-arg index="0"> <ref bean="sequenceGenerater"/> </constructor-arg> <constructor-arg index="1"> <ref bean="swmsSequenceDao"/> </constructor-arg> </bean> </property> </bean>
基本上,Spring虽然提供了这些Aware相关接口,然而Bean上若实现了这些界面,就算是与Spring发生了依赖,从另一个角度来看,虽然您可以直接在Bean上实现这些接口,但您也可以透过setter来完成依赖注入,例如:
public class GlobalParamUtils { private static ApplicationContext ac; public void setApplicationContext(ApplicationContext arg0) throws BeansException { this.ac = arg0; } public static String generateCodeByType(String type) { String result = ""; if (ac.containsBean("swmsBussinessCodeManager")) { BussinessCodeManager codeManager = (BussinessCodeManager)ac.getBean("swmsBussinessCodeManager"); try { Method method = codeManager.getClass().getMethod(type, new Class[0]); result = (String) method.invoke(codeManager, new Object[0]); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } return result; } }
注意这次我们并没有实作ApplicationContextAware,我们在程序中可以自行注入ApplicationContext实例:
ApplicationContext context = new ClassPathXmlApplicationContext("service.xml");
就Bean而言,降低了对Spring的依赖,可以比较容易从现有的框架中脱离
相关推荐
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextInitializer; import org.springframework.core.io.Resource; import org.springframework....
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; ...
import org.springframework.context.ApplicationContextAware; public class MyBean implements ApplicationContextAware { private static ApplicationContext context; @Override public void ...
在源码中,可以看到`org.springframework.beans`和`org.springframework.context`包下的类,如`BeanFactory`和`ApplicationContext`,它们是IoC容器的实现基础。 2. **AOP(Aspect Oriented Programming)**:...
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationEvent; public class DwrService implements ApplicationContextAware { private ...
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.spring...
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; @SpringJUnitConfig(CommonConfig.class) @TestPropertySource(locations = "classpath:application.yml") public class ...
在`org.springframework.jdbc`和`org.springframework.orm`包中,可以研究其底层实现。 5. **Spring MVC**:作为Spring的Web MVC框架,Spring MVC负责处理HTTP请求,提供模型-视图-控制器结构。`org.spring...
<bean id="eventPublisher" class="org.springframework.context.ApplicationContextAware"> ``` 发布事件时,调用`eventPublisher.publishEvent(event)`,其中`event`是你要发布的事件对象。 2. 注解方式调用...
import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class ApplicationContextHolder implements ApplicationContextAware { ...
import org.springframework.web.context.support.SpringBeanAutowiringSupport; public class SpringWiredBean extends SpringBeanAutowiringSupport { @Autowired private static BeanFactory beanFactory; /...
import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; / * Spring 上下文工具类 * * @author nwgdk */ @Component public class ...
import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class SpringUtil implements ApplicationContextAware { private static ...
import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; @Component public class SpringUtils implements ApplicationContextAware { private static ...
import org.springframework.context.ApplicationContextAware; public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext context; @Override public void ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
import org.springframework.context.ApplicationContextAware; public class ApplicationContextHolder implements ApplicationContextAware { private static ApplicationContext context; @Override public ...
<bean id="myJobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="com.example.MyJob"/> <bean id="myTrigger" class="org.spring...