目录
一、说明
二、举例
首页 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脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++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编程式事务有哪些实现方式 spring编程式事务有哪些实现方式 Jan 08, 2024 am 10:23 AM

spring编程式事务的实现方式:1、使用TransactionTemplate;2、使用TransactionCallback和TransactionCallbackWithoutResult;3、使用Transactional注解;4、使用TransactionTemplate和@Transactional结合使用;5、自定义事务管理器。

利用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

PHP 代码文档化之王:PHPDoc 的进阶指南 PHP 代码文档化之王:PHPDoc 的进阶指南 Mar 02, 2024 am 08:43 AM

引言:PHPDoc是一种用于php代码的注释标准,可生成易于理解且信息丰富的文档。通过使用特定的注释标签,PHPDoc允许开发人员提供有关函数、类、方法和其他代码元素的重要详细信息。这篇进阶指南将深入探讨PHPDoc,展示其功能并提供有效的文档化策略。语法和标签:PHPDoc注释以双斜杠(//)或多行注释(/**/)开头。以下是一些常见的注释标签:@param:定义函数或方法的参数。@return:指定函数或方法的返回值。@throws:说明函数或方法可能引发的异常。@var:定义类的属性或实例

JUnit框架中注解如何用于测试方法? JUnit框架中注解如何用于测试方法? May 06, 2024 pm 05:33 PM

JUnit框架中的注解用于声明和配置测试方法,主要注解包括:@Test(声明测试方法)、@Before(测试方法执行前运行的方法)、@After(测试方法执行后运行的方法)、@BeforeClass(所有测试方法执行前运行的方法)、@AfterClass(所有测试方法执行后运行的方法),这些注解有助于组织和简化测试代码,并通过提供明确的意图和配置来提高测试代码的可读性和可维护性。

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正是为了满足这一需求而设计的,本

Spring如何设置事务隔离级别 Spring如何设置事务隔离级别 Jan 26, 2024 pm 05:38 PM

Spring设置事务隔离级别的方法:1、使用@Transactional注解;2、在Spring配置文件中设置;3、使用PlatformTransactionManager;4、在Java配置类中设置。详细介绍:1、使用@Transactional注解,在需要进行事务管理的类或方法上添加@Transactional注解,并在属性中设置隔离级别;​2、在Spring配置文件等等。

See all articles