溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在Android環(huán)境下支持Spring框架下得AOP,DI,Aspect等

發(fā)布時(shí)間:2020-07-09 18:55:09 來源:網(wǎng)絡(luò) 閱讀:2789 作者:ryanch741 欄目:移動(dòng)開發(fā)

大家都知道,Spring在JavaEE開發(fā)中扮演著非常重要的地位。

  • 可以利用它的依賴注入(DI)很好的實(shí)現(xiàn)各個(gè)模塊直接的解耦。
  • 可以利用它的AOP很好的實(shí)現(xiàn)面向方面的編程,實(shí)現(xiàn)與事務(wù)無關(guān)的業(yè)務(wù)邏輯(事務(wù)代碼寫在切面中)
  • 可以使用不同的注解(@Controller;@Service;@Repository)很好的實(shí)現(xiàn)分成架構(gòu), 表明各個(gè)模塊的作用
  • 可以用MVC實(shí)現(xiàn)的Controller很好的處理請求和響應(yīng)客戶端請求
  • 可以輕松的使用集成其他第三方組件實(shí)現(xiàn)的功能等。

那這么好的功能,如何能在Android上使用呢,服務(wù)于Android開發(fā)者呢?

現(xiàn)在給大家推薦一個(gè)第三方庫, 可以很方便的集成到您的應(yīng)用里面,獲取以上這些功能
如何在Android環(huán)境下支持Spring框架下得AOP,DI,Aspect等

項(xiàng)目地址:https://github.com/hianzuo/android-spring

Introduction

Android-spring is a android library project support IOC , DI , AOP and HTTP/Handler , it use annotation to config 。 It contains a simple project.

Add the dependency

 dependencies {
 ? compile 'com.hianzuo.android:LibSpring:1.0.4'
 }

Init spring from Application

public class SimpleApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        //if in dev mode ,please line blow code
        SpringInitializer.devMode();
        //spring init, you can add more package to scan spring component.
        SpringInitializer.init(this,
                "com.hianzuo.spring.simple.test.",
                "other package to scan spring component");
    }
}

DI Support in Activity

public class MainActivity extends AppCompatActivity {

    @Resource
    private TestService testService;

    @Resource
    private PrintService printService;

    @Resource(beanName = "testBean")
    private BeanTest testBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        testService.handle();
        setContentView(R.layout.activity_main);
        TextView tv = findViewById(R.id.tv);
        tv.setText(printService.print() + "\n\n" + testBean.getText());
    }
}

Configuration Bean

@Component
@Configuration
public class TestConfiguration {

    @Bean("testBean")
    public BeanTest bean1() {
        return new BeanTest("bean name in annotation");
    }

    @Bean
    public BeanTest methodIsBeanName() {
        return new BeanTest("method is bean name");
    }
}

AOP

@Aspect
public class TestServiceAspect {

    @Pointcut("^.*?handle\\(\\).*+$")
    public void handle() {
        System.out.println("AAA TestServiceAspect handle");
    }

    @Before("handle")
    public void before(JointPoint point) {
        System.out.println("AAA TestServiceAspect before");
    }

    @Around(value = "handle")
    public Object around(JointPoint point) {
        System.out.println("AAA TestServiceAspect around start");
        Object result = point.invokeResult();
        System.out.println("AAA TestServiceAspect around end");
        return result;
    }

    @After(value = "handle")
    public void after(JointPoint point) {
        System.out.println("AAA TestServiceAspect after");
    }

Service Annotation Support

@Service
public class TestServiceImpl implements TestService {
    @Resource
    private PrintService printService;

    @Resource(beanName = "testBean")
    private BeanTest testBean;

    @Resource(beanName = "methodIsBeanName")
    private BeanTest testBean1;

    @Override
    public void handle() {
        printService.print();
        System.out.println("AAA BeanTest :" + testBean.getText());
        System.out.println("AAA BeanTest1 :" + testBean1.getText());
        System.out.println("AAA TestService.handle.");
    }

    @Override
    public void execute() {
        System.out.println("AAA TestService.execute.");
    }
}

Cache Support

@Component
public class DemoProviderImpl extends AbstractCacheAble<Integer, Demo> {
    @Override
    protected Integer getKey(Demo demo) {
        return demo.getId();
    }

    @Override
    protected List<Demo> loadData() {
        //load Demo data from remote server or database 
        ArrayList<Demo> list = new ArrayList<>();
        list.add(new Demo(1, "aaa"));
        list.add(new Demo(2, "bbb"));
        return list;
    }
}

Http Handler Support

@Handler("/api/login")
public class HttpLoginHandler extends BaseHandler {

    @Override
    protected Object getMethodParamObjectByType(Class<?> type) {
        if(type == LoginData.class){
            String username = (String) getMethodParamObject("username");
            String password = (String) getMethodParamObject("password");
            return new LoginData(username,password);
        }
        return super.getMethodParamObjectByType(type);
    }

    @Override
    protected Object getMethodParamObject(String value) {
        // get value from request.
        // demo request.getParameter(value);
        return null;
    }

    @CheckMethod
    protected String check(@MethodParam("username") String username) {
        if (StringUtil.isEmpty(username) || username.trim().length() < 4) {
            throws new RuntimeException("用戶名不能為空");
        }
        return null;
    }

    @Resource
    private LoginService loginService;

    //you can use @MethodParam Annotation to get parameter
    /*@HandleMethod
    public void handle(@MethodParam("username") String username, @MethodParam("password") String password) {
        loginService.login(username, password);
    }*/

    //you can get DataModel in Method Param , register in (Object getMethodParamObjectByType(Class<?> type))
    @HandleMethod
    public void handle(LoginData data) {
        loginService.login(data.getUsername(), data.getPassword());
    }
}

Repository Annotation Support

@Repository like @Service Annotation for the Component.

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI