搜索
首页Javajava教程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
Java Spring怎么实现定时任务Java Spring怎么实现定时任务May 24, 2023 pm 01:28 PM

java实现定时任务Jdk自带的库中,有两种方式可以实现定时任务,一种是Timer,另一种是ScheduledThreadPoolExecutor。Timer+TimerTask创建一个Timer就创建了一个线程,可以用来调度TimerTask任务Timer有四个构造方法,可以指定Timer线程的名字以及是否设置为为守护线程。默认名字Timer-编号,默认不是守护线程。主要有三个比较重要的方法:cancel():终止任务调度,取消当前调度的所有任务,正在运行的任务不受影响purge():从任务队

Java axios与spring前后端分离传参规范是什么Java axios与spring前后端分离传参规范是什么May 03, 2023 pm 09:55 PM

一、@RequestParam注解对应的axios传参方法以下面的这段Springjava代码为例,接口使用POST协议,需要接受的参数分别是tsCode、indexCols、table。针对这个Spring的HTTP接口,axios该如何传参?有几种方法?我们来一一介绍。@PostMapping("/line")publicList

Spring Boot与Spring Cloud的区别与联系Spring Boot与Spring Cloud的区别与联系Jun 22, 2023 pm 06:25 PM

SpringBoot和SpringCloud都是SpringFramework的扩展,它们可以帮助开发人员更快地构建和部署微服务应用程序,但它们各自有不同的用途和功能。SpringBoot是一个快速构建Java应用的框架,使得开发人员可以更快地创建和部署基于Spring的应用程序。它提供了一个简单、易于理解的方式来构建独立的、可执行的Spring应用

Spring 最常用的 7 大类注解,史上最强整理!Spring 最常用的 7 大类注解,史上最强整理!Jul 26, 2023 pm 04:38 PM

随着技术的更新迭代,Java5.0开始支持注解。而作为java中的领军框架spring,自从更新了2.5版本之后也开始慢慢舍弃xml配置,更多使用注解来控制spring框架。

Java Spring框架创建项目与Bean的存储与读取实例分析Java Spring框架创建项目与Bean的存储与读取实例分析May 12, 2023 am 08:40 AM

1.Spring项目的创建1.1创建Maven项目第一步,创建Maven项目,Spring也是基于Maven的。1.2添加spring依赖第二步,在Maven项目中添加Spring的支持(spring-context,spring-beans)在pom.xml文件添加依赖项。org.springframeworkspring-context5.2.3.RELEASEorg.springframeworkspring-beans5.2.3.RELEASE刷新等待加载完成。1.3创建启动类第三步,创

从零开始学Spring Cloud从零开始学Spring CloudJun 22, 2023 am 08:11 AM

作为一名Java开发者,学习和使用Spring框架已经是一项必不可少的技能。而随着云计算和微服务的盛行,学习和使用SpringCloud成为了另一个必须要掌握的技能。SpringCloud是一个基于SpringBoot的用于快速构建分布式系统的开发工具集。它为开发者提供了一系列的组件,包括服务注册与发现、配置中心、负载均衡和断路器等,使得开发者在构建微

Java Spring Bean生命周期管理的示例分析Java Spring Bean生命周期管理的示例分析Apr 18, 2023 am 09:13 AM

SpringBean的生命周期管理一、SpringBean的生命周期通过以下方式来指定Bean的初始化和销毁方法,当Bean为单例时,Bean归Spring容器管理,Spring容器关闭,就会调用Bean的销毁方法当Bean为多例时,Bean不归Spring容器管理,Spring容器关闭,不会调用Bean的销毁方法二、通过@Bean的参数(initMethod,destroyMethod)指定Bean的初始化和销毁方法1、项目结构2、PersonpublicclassPerson{publicP

spring设计模式有哪些spring设计模式有哪些Dec 29, 2023 pm 03:42 PM

spring设计模式有:1、依赖注入和控制反转;2、工厂模式;3、模板模式;4、观察者模式;5、装饰者模式;6、单例模式;7、策略模式和适配器模式等。详细介绍:1、依赖注入和控制反转: 这两个设计模式是Spring框架的核心。通过依赖注入,Spring负责管理和注入组件之间的依赖关系,降低了组件之间的耦合度。控制反转则是指将对象的创建和依赖关系的管理交给Spring容器等等。

See all articles

热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尊渡假赌尊渡假赌尊渡假赌

热工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具