程式碼獲取Spring註解的bean
阿新 • • 發佈:2019-02-03
方法一 不用配置xml,直接java程式碼實現
public class GetApplicationContext { private static class ApplicationContextHolder { // 單例變數 private static ApplicationContext AC = new FileSystemXmlApplicationContext( "classpath:applicationContext.xml"); } // 私有化的構造方法,保證外部的類不能通過構造器來例項化。 private GetApplicationContext() { } // 獲取單例物件例項 public static ApplicationContext getInstance() { if (ApplicationContextHolder.AC == null) { ApplicationContextHolder.AC = new FileSystemXmlApplicationContext( "classpath:applicationContext.xml"); } return ApplicationContextHolder.AC; } }
獲取所有spring自動裝配的bean;
<span style="font-size:18px;">//獲取spring裝配的bean個數 GetApplicationContext.getInstance().getBeanDefinitionNames().length; //逐個打印出spring自動裝配的bean。根據我的測試,類名第一個字母小寫即bean的名字 for(int i=0;i<33;i++){ System.out.println( GetApplicationContext.getInstance().getBeanDefinitionNames()[i]); }</span>
然後通過下面的程式碼獲取到spring註解裝配的bean供自己使用
<span style="font-size:18px;">StorageReturnService ossService = (StorageReturnService) GetApplicationContext.getInstance().getBean("storageReturnServiceImpl");</span>
方法二 實現ApplicationContextAware
一定要在spring.xml中加上:
<bean id="Spring ContextUtil " class="com.am.oa.commons.service.SpringContextUtil " singleton="true" />
當對SpringContextUtil 例項時就自動設定applicationContext,以便後來可直接用applicationContext
<span style="font-size:18px;">public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; //Spring應用上下文環境
/**
* 實現ApplicationContextAware介面的回撥方法,設定上下文環境
* @param applicationContext
* @throws BeansException
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
/**
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 獲取物件
* @param name
* @return Object 一個以所給名字註冊的bean的例項
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
/**
* 獲取型別為requiredType的物件
* 如果bean不能被型別轉換,相應的異常將會被丟擲(BeanNotOfRequiredTypeException)
* @param name bean註冊名
* @param requiredType 返回物件型別
* @return Object 返回requiredType型別物件
* @throws BeansException
*/
public static Object getBean(String name, Class requiredType) throws BeansException {
return applicationContext.getBean(name, requiredType);
}
/**
* 如果BeanFactory包含一個與所給名稱匹配的bean定義,則返回true
* @param name
* @return boolean
*/
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
/**
* 判斷以給定名字註冊的bean定義是一個singleton還是一個prototype。
* 如果與給定名字相應的bean定義沒有被找到,將會丟擲一個異常(NoSuchBeanDefinitionException)
* @param name
* @return boolean
* @throws NoSuchBeanDefinitionException
*/
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return applicationContext.isSingleton(name);
}
/**
* @param name
* @return Class 註冊物件的型別
* @throws NoSuchBeanDefinitionException
*/
public static Class getType(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getType(name);
}
/**
* 如果給定的bean名字在bean定義中有別名,則返回這些別名
* @param name
* @return
* @throws NoSuchBeanDefinitionException
*/
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getAliases(name);
}
}</span>
action呼叫:
<span style="font-size:18px;">
public class UserAction extends BaseAction implements Action,ModelDriven{
private Users user = new Users();
//不用再載入springContext.xml檔案,因為在web.xml中配置了,在程式中啟動是就有了.
UserService userService = (UserService) SpringContextUtil.getBean("userService");
public String execute() throws Exception {
return SUCCESS;
}
public Object getModel() {
return user;
}
public String getAllUser(){
HttpServletRequest request = ServletActionContext.getRequest();
List ls=userService.LoadAllObject( Users.class );
request.setAttribute("user",ls);
this.setUrl("/yonghu.jsp");
return SUCCESS;
}
}</span>