1. 程式人生 > 其它 >Dubbo踩坑記錄 ImmutableCollection不支援序列化導致的介面呼叫失敗

Dubbo踩坑記錄 ImmutableCollection不支援序列化導致的介面呼叫失敗

技術標籤:springspringjavamybatisspring bootxml

我們使用註解前要先進行匯入這個包 就是這裡面的一個aop包。

在這裡插入圖片描述
寫配置檔案,我們使用這種註解標頭檔案,開啟註解,同時我們有添加了一個標籤,元件掃描讓其這個包下的所有元件都生效

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
<!--    元件掃描-->
    <context:component-scan base-package="aw"/>
    
    <context:annotation-config/>


</beans>

@Component元件用於pojo層
@Repository元件用於dao層
@Service元件用於service層
@Controller元件用於controle(servlet)層

這些註解的意思是都將他們註冊到spring中。
我們以Component為例
@Component等價與 <bean id=“user” class=“aw.pojo.User”
@Value等價與<property name=“name” value=“阿威”/

package aw.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class User {
    @Value("阿威")
    private String name;

    public void show(){
        System.out.println(name+"1111");
    }
}

同時我們還可以使用註解新增他的作用域。

package aw.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;

@Component
@Scope("singleton")
public class User {
    @Value("阿威")
    private String name;

    public void show(){
        System.out.println(name+"1111");
    }
}

xml是屬於萬能使用,複雜的使用。
註解只適用於簡單的,當自己的類。

java開發

首先是我們建立一個類,這裡我們的註解我們已經瞭解過了。

package aw.pojo.java;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("singleton")
public class User {
    @Value("阿威")
    private String name;

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

現在我們不是寫applicationContext.xml配置檔案,而是寫一個配置類,我們首先是寫一個
@Configuration配置註解,他的作用相當於.xml,同時也將這個類註冊到spring中。
@Bean註解就相當於<bean 標籤,id是方法名,class型別是返回值型別。

package aw.pojo.java;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JavaConfig {

    @Bean
    public User getUser(){
        return new User();
    }

}

因為我們是通過java註解寫所以要通過註解配置檔案(AnnotationConfig)讀取這個類,然後在通過id獲取方法型別。

public class Test2 {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext javaConfig = new AnnotationConfigApplicationContext(JavaConfig.class);
        User getUser = (User) javaConfig.getBean("getUser");
        System.out.println(getUser);
    }
}

我們還可以在配置類中新增一些其他的註解
@ComponentScan(“aw.pojo.java”) 元件掃描 ( <context:component-scan base-package=“aw”/>)
@Import(JavaConfig2.class) 匯入另一個配置檔案( <import resource=""/)

@Configuration
@ComponentScan("aw.pojo.java")
@Import(JavaConfig2.class)
public class JavaConfig {

    @Bean
    public User getUser(){
        return new User();
    }

}