目錄
一、說明
二、舉例
首頁 Java java教程 java中關於註解功能的詳細介紹

java中關於註解功能的詳細介紹

Jun 30, 2017 am 11:18 AM
spring 基於 註解

一、說明

與@Component註解功能相同,但意義不同的註解還有三個:

1)@Repository:註解在Dao實作類別上
 2) @Service:註解在Service實作類別上
 3)@Controller:註解在SpringMVC的處理器上

 Bean作用域:
 @Scope("prototype"):用於指定物件創建模式,可以是單例模式或原型模式,預設是singleton

 基本型別屬性注入:
 @Value

 @Autowired:byType方式的註解式註入,也就是依照型別註解
 @Qualifier("mySchool"):byName方式的註解式註入,在使用@Qualifier時必須與@Autowired聯合使用

 域屬性註解:
 @Resource:不加name屬性則為byType方式的註解式註入,但前提是注入的物件只能有一個
 @Resource(name="mySchool"):byName方式的註解式註入

 Bean的生命始末:
 @PostConstruct:目前Bean初始化剛完畢
 @PreDestroy:目前Bean即將被銷毀

@Configuration:表示目前類別充當Spring容器,也就是所有的Bean將由這個類別來建立

註:

  在舉例之前宣告幾個問題:

  1、註解需要依賴spring-aop-4.3.9.RELEASE.jar包,所以需要導入依賴包。

#  2、使用註解方式註入,設定檔需要添加約束頭檔:

<?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/spring-beans.xsd
         http://www.springframework.org/schema/context/spring-context.xsd">
登入後複製

  也可以自己從Spring的說明文件中找到此頭檔:

  3、如果使用到了SpringJUnit4測試,則還需要導入spring-test-4.3.9.RELEASE.jar套件

二、舉例

1、先建立一個School類別:

package com.ietree.spring.basic.annotation.demo1;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;

@Component("mySchool")public class School {

    @Value(value = "清华大学")private String name;public School() {super();
    }public School(String name) {super();this.name = name;
    }public void setName(String name) {this.name = name;
    }

    @Overridepublic String toString() {return "School [name=" + name + "]";
    }
}
登入後複製

建立Student類別:

package com.ietree.spring.basic.annotation.demo1;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import javax.annotation.Resource;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;/**
 * 说明:
 * 与@Component注解功能相同,但意义不同的注解还有三个:
 * 1)@Repository:注解在Dao实现类上
 * 2)@Service:注解在Service实现类上
 * 3)@Controller:注解在SpringMVC的处理器上
 * 
 * Bean作用域:
 * @Scope("prototype"):用于指定对象创建模式,可以是单例模式或者原型模式,默认是singleton
 * 
 * 基本类型属性注入:
 * @Value
 * 
 * @Autowired:byType方式的注解式注入,即根据类型注解
 * @Qualifier("mySchool"):byName方式的注解式注入,在使用@Qualifier时必须与@Autowired联合使用
 * 
 * 域属性注解:
 * @Resource:不加name属性则为byType方式的注解式注入,但前提是注入的对象只能有一个
 * @Resource(name="mySchool"):byName方式的注解式注入
 * 
 * Bean的生命始末:
 * @PostConstruct:当前Bean初始化刚完毕
 * @PreDestroy:当前Bean即将被销毁 *///@Scope("prototype")@Component("myStudent")public class Student {

    @Value(value = "小明")private String name;
    
    @Value(value = "25")private int age;    //    @Autowired//    @Qualifier("mySchool")//    @Resource(name="mySchool")    @Resourceprivate School school;// 对象属性,也叫做域属性public Student() {super();
    }    public Student(String name, int age) {super();this.name = name;this.age = age;
    }public void setName(String name) {
        System.out.println("执行setName()");this.name = name;
    }public void setAge(int age) {
        System.out.println("执行setAge()");this.age = age;
    }public void setSchool(School school) {this.school = school;
    }

    @Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
    }
    
    @PostConstructpublic void initAfter(){
        System.out.println("当前Bean初始化刚完毕");
    }
    
    @PreDestroypublic void preDestroy(){
        System.out.println("当前Bean即将被销毁");
    }
}
登入後複製

建立MyJavaConfig類別:

package com.ietree.spring.basic.annotation.demo1;import org.springframework.beans.factory.annotation.Autowire;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/**
 * @Configuration:表示当前类充当Spring容器,即所有的Bean将由这个类来创建 */@Configurationpublic class MyJavaConfig {
    
    @Bean(name="mySchool")public School mySchoolCreator(){return new School("清华大学");
    }    // autowire=Autowire.BY_TYPE:指从当前类这个容器中查找与域属性的类型具有is-a关系的Bean// autowire=Autowire.BY_NAME:指从当前类这个容器中查找与域属性同名的Bean@Bean(name="myStudent", autowire=Autowire.BY_TYPE)public Student myStudentCreator(){return new Student("小明", 25);
    }
}
登入後複製

#「建立設定檔:

<?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/spring-beans.xsd
         http://www.springframework.org/schema/context/spring-context.xsd">
登入後複製

建立測試類別:

package com.ietree.spring.basic.annotation.demo1;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:com/ietree/spring/basic/annotation/demo1/applicationContext.xml")public class MyTest {
    
    @Autowiredprivate Student student;
    
    @Testpublic void test01() {

        String resource = "com/ietree/spring/basic/annotation/demo1/applicationContext.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(resource);

        School school = (School) ctx.getBean("mySchool");
        System.out.println(school);

        Student student = (Student) ctx.getBean("myStudent");
        System.out.println(student);
        
        ((ClassPathXmlApplicationContext)ctx).close();
    }    public void test02(){
        System.out.println(student);
    }
    
}
登入後複製
#

以上是java中關於註解功能的詳細介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

編程新範式,當Spring Boot遇上OpenAI 編程新範式,當Spring Boot遇上OpenAI Feb 01, 2024 pm 09:18 PM

2023年,AI技術已成為熱門話題,對各行業產生了巨大影響,程式設計領域尤其如此。人們越來越認識到AI技術的重要性,Spring社群也不例外。隨著GenAI(GeneralArtificialIntelligence)技術的不斷進步,簡化具備AI功能的應用程式的創建變得至關重要和迫切。在這個背景下,"SpringAI"應運而生,旨在簡化開發AI功能應用程式的過程,使其變得簡單直觀,避免不必要的複雜性。透過"SpringAI",開發者可以更輕鬆地建立具備AI功能的應用程序,將其變得更加易於使用和操作

利用Spring Boot以及Spring AI建構生成式人工智慧應用 利用Spring Boot以及Spring AI建構生成式人工智慧應用 Apr 28, 2024 am 11:46 AM

Spring+AI作為行業領導者,透過其強大、靈活的API和先進的功能,為各種行業提供了領先性的解決方案。在本專題中,我們將深入探討Spring+AI在各領域的應用範例,每個案例都將展示Spring+AI如何滿足特定需求,實現目標,並將這些LESSONSLEARNED擴展到更廣泛的應用。希望這個專題能對你有所啟發,更深入地理解和利用Spring+AI的無限可能。 Spring框架在軟體開發領域已經有超過20年的歷史,自SpringBoot1.0版本發布以來已有10年。現在,無人會質疑,Spring

spring編程式事務有哪些實作方式 spring編程式事務有哪些實作方式 Jan 08, 2024 am 10:23 AM

spring編程式事務的實作方式:1、使用TransactionTemplate;2、使用TransactionCallback和TransactionCallbackWithoutResult;3、使用Transactional註解;4、使用TransactionTemplate和@Transactional結合使用;5、自訂事務管理器。

JUnit框架中註解如何用於測試方法? JUnit框架中註解如何用於測試方法? May 06, 2024 pm 05:33 PM

JUnit框架中的註解用於聲明和配置測試方法,主要註解包括:@Test(聲明測試方法)、@Before(測試方法執行前運行的方法)、@After(測試方法執行後運行的方法)、@ BeforeClass(所有測試方法執行前運行的方法)、@AfterClass(所有測試方法執行後運行的方法),這些註解有助於組織和簡化測試程式碼,並透過提供明確的意圖和配置來提高測試程式碼的可讀性和可維護性。

PHP 程式碼文檔化之王:PHPDoc 的進階指南 PHP 程式碼文檔化之王:PHPDoc 的進階指南 Mar 02, 2024 am 08:43 AM

引言:PHPDoc是一種用於php程式碼的註解標準,可產生易於理解且資訊豐富的文件。透過使用特定的註釋標籤,PHPDoc允許開發人員提供有關函數、類別、方法和其他程式碼元素的重要詳細資訊。這篇進階指南將深入探討PHPDoc,展示其功能並提供有效的文檔化策略。語法與標籤:PHPDoc註解以雙斜線(//)或多行註解(/**/)開頭。以下是一些常見的註解標籤:@param:定義函數或方法的參數。 @return:指定函數或方法的回傳值。 @throws:說明函數或方法可能引發的異常。 @var:定義類別的屬性或實例

Spring如何設定事務隔離級別 Spring如何設定事務隔離級別 Jan 26, 2024 pm 05:38 PM

Spring設定事務隔離等級的方法:1、使用@Transactional註解;2、在Spring設定檔中設定;3、使用PlatformTransactionManager;4、在Java配置類別中設定。詳細介紹:1、使用@Transactional註解,在需要進行事務管理的類別或方法上加入@Transactional註解,並在屬性中設定隔離等級;2、在Spring設定檔等等。

Jackson庫中註解如何控制JSON序列化和反序列化? Jackson庫中註解如何控制JSON序列化和反序列化? May 06, 2024 pm 10:09 PM

Jackson庫中的註解可控制JSON序列化和反序列化:序列化:@JsonIgnore:忽略屬性@JsonProperty:指定名稱@JsonGetter:使用獲取方法@JsonSetter:使用設定方法反序列化:@JsonIgnoreProperties:忽略屬性@ JsonProperty:指定名稱@JsonCreator:使用建構子@JsonDeserialize:自訂邏輯

詳解MyBatis註解與動態SQL的操作步驟 詳解MyBatis註解與動態SQL的操作步驟 Feb 18, 2024 pm 03:29 PM

MyBatis註解動態SQL的使用方法詳解IntroductiontotheusageofMyBatisannotationdynamicSQLMyBatis是一個持久層框架,為我們提供了便利的持久化操作。在實際開發中,通常需要根據業務需求來動態產生SQL語句,以實現靈活的資料操作。 MyBatis註解動態SQL正是為了滿足這項需求而設計的,本

See all articles