使用工具获取serive的类,解决在工具类之中引入service为null情况

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Spring 工具包 获取bean类
 */
@Component
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (SpringUtil.applicationContext == null) {
            SpringUtil.applicationContext = applicationContext;
        }
    }

    //获取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    //通过name获取 Bean.
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }

    //通过class获取Bean.
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    //通过name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name, Class<T> clazz) {
        return getApplicationContext().getBean(name, clazz);
    }


}

 

 

 

使用方法:

UserService userService = SpringUtil.getApplicationContext().getBean(UserService.class);
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用bind启动service,需要使用以下步骤: 1. 在AndroidManifest.xml文件中注册你的service: ``` <service android:name=".MyService" android:enabled="true" android:exported="false" /> ``` 2. 在你的service中实现onBind()方法: ``` public class MyService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { // TODO: Return your binder instance here return null; } } ``` 3. 实现你的Binder: ``` public class MyBinder extends Binder { public MyService getService() { return MyService.this; } } ``` 4. 在你的service中创建一个Binder实例: ``` private final IBinder mBinder = new MyBinder(); ``` 5. 在onBind()方法中返回Binder实例: ``` @Nullable @Override public IBinder onBind(Intent intent) { return mBinder; } ``` 6. 在你的Activity中启动service并绑定它: ``` Intent intent = new Intent(this, MyService.class); bindService(intent, mConnection, Context.BIND_AUTO_CREATE); ``` 7. 创建ServiceConnection实例: ``` private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { MyBinder binder = (MyBinder) iBinder; MyService service = binder.getService(); // TODO: Use the service instance here } @Override public void onServiceDisconnected(ComponentName componentName) { // TODO: Handle service disconnected event here } }; ``` 8. 在你的Activity销毁时解绑service: ``` @Override protected void onDestroy() { super.onDestroy(); unbindService(mConnection); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值