溫馨提示×

溫馨提示×

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

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

Java中如何使用Spring注解

發(fā)布時(shí)間:2021-07-27 13:47:27 來源:億速云 閱讀:152 作者:Leah 欄目:開發(fā)技術(shù)

Java中如何使用Spring注解,針對這個(gè)問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡單易行的方法。

  • 在Spring4之后,要使用注解開發(fā),必須要保證aop的包導(dǎo)入了

Java中如何使用Spring注解

  • 使用注解需要導(dǎo)入context約束,增加注解的支持!

<?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
       https://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">
    <!--指定要掃描的包,這個(gè)包下的注解就會生效-->
    <context:component-scan base-package="com.example.springannotation" />
    <context:annotation-config></context:annotation-config>
    <!-- <bean id="cat" class="com.example.springannotation.dao.Cat"/>
    <bean id="people" class="com.example.springannotation.dao.People"/>-->
</beans>

注解的支持:

//@Component 等價(jià)于<bean id="pepople" class="com.example.springannotation.dao.People" />
@Component
public class People {
    @Autowired(required = false)
    @Value("1235") //相當(dāng)<property name="id " value="1235"/>
    private int id;
    @Autowired(required = false)
    private String name ="ming";
    
    @Value("qing") //相當(dāng)<property name="name " value="qing"/>
    public void setName(@Nullable String name) {
        this.name = name;
    }
}

衍生的注解
@Component有幾個(gè)衍生注解,我們在web開發(fā)中,會按照mvc三層架構(gòu)分層!

  • dao 【@Repository】

  • service【@Service】

  • controller【@Controler】

這四個(gè)注解功能都是一樣的,都是代表將某個(gè)類注冊到Spring中,裝配Bean。

@Scope("singleton") //singleton:標(biāo)識單例模式,prototype:標(biāo)識原型模式 、request:標(biāo)識請求模式、session:標(biāo)識會話模式

xml 與注解:

  • xml更加萬能,適用于任何場合!維護(hù)簡單方便。注解不是自己類使用不了,維護(hù)相對復(fù)雜!

xml與注解最佳實(shí)踐:

  • xml 用來管理bean;

  • 注解只負(fù)責(zé)完成屬性的注入;

  • 我們在使用的過程中,只需要注意一個(gè)問題:必須讓注解生效,就需要開啟注解的支持

<!--指定要掃描的包,這個(gè)包下的注解就會生效-->
    <context:component-scan base-package="com.example.springannotation" />
    <context:annotation-config></context:annotation-config>

JAVA的方式配置Spring

  • @Configuration 這個(gè)也會spring容器托管,注冊到容器中,因?yàn)樗緛砭褪且粋€(gè)Component,

  • @Configuration代表這是一個(gè)配置類,就和我們之前看的beans.xml

// 配置類 代替 beans.xml
import com.example.springannotation.dao.Cat;
import com.example.springannotation.dao.People;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@ComponentScan("com.example.springannotation")
@Import(WwConfig.class)  //引入第二個(gè)配置
public class AppConfig {
    //注朋一個(gè)bean 相當(dāng)于當(dāng)于我們之前寫的一個(gè)bean標(biāo)簽
    //這個(gè)方法的名字,就相當(dāng)于bean標(biāo)簽中的id屬性
    //這個(gè)方法的返回價(jià),就和當(dāng)了bean標(biāo)簽中的class屬性
    @Bean
    public People getPeople(){
        return new People();
    }

    @Bean
    public Cat getCat(){
        return new Cat();
    }
}

import org.springframework.context.annotation.Configuration;
@Configuration
public class WwConfig {
}
//測試類
import com.example.springannotation.config.AppConfig;
import com.example.springannotation.dao.People;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@SpringBootTest
class SpringannotationApplicationTests {
    @Test
    void contextLoads() {
        如果完全使用了配置類方式做,
        // 我們就只能通過 AnnotationConfig 上下文來獲取容器,通過配置類的class對象加載!
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        People people = (People) context.getBean("getPeople");
        System.out.println(people.toString());
    }
}

關(guān)于Java中如何使用Spring注解問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。

向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