1. 程式人生 > 其它 >spring與new物件互動

spring與new物件互動

技術標籤:Spring

1 如何從spring容器獲得bean例項。

繼承ApplicationContextAware,將這個類用spring管理起來。applicationContext.getBean();

@Service
public class BeanUtils implements ApplicationContextAware {
 
    private static Logger logger = LoggerFactory.getLogger(InitNewService.class);
 
    private static ApplicationContext applicationContext;
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }

2 new java 物件放入容器內部。


public class test{

    @Autowired
    private AutowireCapableBeanFactory beanFactory;
    @Autowired
DefaultListableBeanFactory defaultListableBeanFactory; public void inject(){ UserDao userDao=new UserDao(); //將new出的物件放入Spring容器中 defaultListableBeanFactory.registerSingleton("userDao",userDao); //自動注入依賴 beanFactory.autowireBean(userDao); userDao.
print(); //判斷new的物件和從容器中拿出的物件是否一致 System.out.println(defaultListableBeanFactory.getBean("userDao").equals(userDao)); } }

3 new java的物件(該類沒有被spring管理)的成員自動注入。直接繼承下面這個類就可以。

@Service
public class InitNewService implements ApplicationContextAware {
 
    private static Logger logger = LoggerFactory.getLogger(InitNewService.class);
 
    private static ApplicationContext applicationContext;
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
 
    /**
     * 將繼承此類的service中的欄位(註解了@Autowired 或者@Resource)等注入進來
     */
    public InitNewService(){
 
        if (this.applicationContext == null){
            return;
        }
 
        if (this.getClass().isAnnotationPresent(org.springframework.stereotype.Service.class)
                || this.getClass().isAnnotationPresent(org.springframework.stereotype.Controller.class)
                || this.getClass().isAnnotationPresent(org.springframework.stereotype.Component.class) ){
            return;
        }
 
        Class clazz = this.getClass();
        do {
            Field[] fields = clazz.getDeclaredFields();
            for (Field f : fields) {
                if (f.isAnnotationPresent(org.springframework.beans.factory.annotation.Autowired.class)
                        || f.isAnnotationPresent(javax.annotation.Resource.class)){
 
                    try {
                        String simpleName = f.getType().getSimpleName();
                        String beanName = StrUtils.toLowerCaseFirstOne(simpleName);
 
                        Object bean = applicationContext.getBean(beanName);
                        if (bean == null){
                            return;
                        }
 
                        boolean accessible = f.isAccessible();
                        f.setAccessible(true);
                        f.set(this,bean);
                        f.setAccessible(accessible);
                    }catch (Exception e){
                        logger.error(clazz.getName() + "當new物件注入類" + f.getName() + "的時候,發生了錯誤",e);
                        e.printStackTrace();
                    }
 
                }
            }
            clazz = clazz.getSuperclass();
        } while (clazz != Object.class);
    }
 
 
}