通过注解的方式获取bean
<!-- 扫描所有Servcie类 -->
<context:component-scan base-package="com.cnten.**.service" />
通过类名获取实例化的bean
@Service
public class PortalService {
}
PortalService portalService = (PortalService) applicationContext.getBean("PortalService");
spring应用的上下文环境
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class ApplicationContextAwareUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static <T> T getBean(String name)
{
return applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> clazz)
{
return applicationContext.getBeansOfType(clazz);
}
}
通过实现了InitializingBean接口获取bea
获取bean的工具类
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
public class SpringContextHolder
implements ApplicationContextAware, InitializingBean
{
private static ConfigurableApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext)
{
applicationContext = (ConfigurableApplicationContext)applicationContext;
}
public static ApplicationContext getApplicationContext()
{
checkApplicationContext();
return applicationContext;
}
public static <T> T getBean(String name)
{
checkApplicationContext();
return applicationContext.getBean(name);
}
public static <T> T getBean(Class<T> clazz)
{
checkApplicationContext();
return applicationContext.getBeansOfType(clazz);
}
private static void checkApplicationContext()
{
if (applicationContext == null) {
throw new IllegalStateException("applicaitonContext未注入,请在applicationContext.xml中定义SpringContextHolder");
}
}
public void afterPropertiesSet()
throws Exception
{}
}
<bean id="cacheManager" class="net.sf.ehcache.CacheManager" lazy-init="false"/>
<bean id="ehCacheManagerImpl" class="com.cache.EhCacheManagerImpl" lazy-init="false"/>
<!-- bean 管理器 -->
<bean id="springContextHolder" class="com.context.SpringContextHolder" lazy-init="false"/>
public class EhCacheManagerImpl
{
public static final CacheManager cacheManager = (CacheManager)SpringContextHolder.getBean("cacheManager");
}
实例化bean
@Autowired
private EhCacheManagerImpl ehCaheMamger;
实现了InitializingBean接口,会自动调用afterPropertiesSet方法。
@Component("initializingService")
public class InitializingServiceImpl implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("call InitializingBean");
}
}
获取bean上下文工具类实现
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; // Spring应用上下文环境
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
/**
* 获取对象
*
* @param name
* @return Object 实例
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
}
<bean id="springContextUtil" class="com.aop.SpringContextUtil" />
public class InitializingBeanDemo {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("spring.xml"); // 加载ApplicationContext(模拟启动web服务)
InitializingService service = (InitializingService) SpringContextUtil.getBean("initializingService");
}
}
返回
call InitializingBean