zl程序教程

您现在的位置是:首页 >  后端

当前栏目

spring(四)之基于注解(Annotation-based)的配置

Spring配置 基于 注解 based annotation
2023-09-27 14:26:37 时间
 ?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" 

 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 

 xmlns:jee="http://www.springframework.org/schema/jee" 

 xmlns:tx="http://www.springframework.org/schema/tx"

 xmlns:aop="http://www.springframework.org/schema/aop" 

 xmlns:mvc="http://www.springframework.org/schema/mvc"

 xmlns:util="http://www.springframework.org/schema/util"

 xmlns:jpa="http://www.springframework.org/schema/data/jpa"

 xsi:schemaLocation="

 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd

 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd

 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd

 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd

 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd" 

 !-- 在这里写配置文件的内容 -- 

 /beans 

@Autowired public void prepare(MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) { this.movieCatalog = movieCatalog; this.customerPreferenceDao = customerPreferenceDao; // ... }
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) { this.customerPreferenceDao = customerPreferenceDao; // ...
public void setMovieCatalogs(Set MovieCatalog movieCatalogs) { this.movieCatalogs = movieCatalogs; // ... }
Maps 也可以这样注解,只要这个 Map 的 key 类型为 String。这个 Map 的 values 应该是已知的类型,并且 keys 应该包含符合 bean 的命名
public class MovieRecommender {

 private Map String, MovieCatalog movieCatalogs;

 @Autowired

 public void setMovieCatalogs(Map String, MovieCatalog movieCatalogs) {

 this.movieCatalogs = movieCatalogs;

 // ...

}

在缺省情况下,当出现0个候选的 beans时自动连接将失败;缺省行为把连接方法,构造器,字段假设为 required 的依赖。


虽然当 一个类只有一个连接构造器时它将被标记为 required, 但是还是可以标记多个构造器的。在这种情况下,每一个构造器都有可能被认为是连接构造器, Spring 将会把依赖关系能够满足的构造器认为是greediest 的构造器。


因为通过类型的自动连接可能会有多个候选,因此经常需要在选择过程中加以控制。一种方法去完成这个控制就是使用@Qualifier注解。


@Autowired public void prepare(@Qualifier("mainCatalog") MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) { this.movieCatalog = movieCatalog; this.customerPreferenceDao = customerPreferenceDao; // ... }
@Target({ElementType.FIELD, ElementType.PARAMETER})

@Retention(RetentionPolicy.RUNTIME)

@Qualifier

public @interface Genre {

 String value();

}

@Autowired public void setComedyCatalog(@Genre("Comedy") MovieCatalog comedyCatalog) { this.comedyCatalog = comedyCatalog; // ... }

添加标签作为标签的子元素,然后指定’type’还有’value’以匹配您的自定义限定器注解。类型必须匹配注解的全名,或者是一个不危险的、方便一点的名字,您也可以使用“短” 类名。


 ?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-2.5.xsd

 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" 

 context:annotation-config/ 

 bean 

 qualifier type="Genre" value="Action"/ 

 !-- inject any dependencies required by this bean -- 

 /bean 

 bean 

 qualifier type="example.Genre" value="Comedy"/ 

 !-- inject any dependencies required by this bean -- 

 /bean 

 bean id="movieRecommender" / 

 /beans 


Spring 也提供了使用 JSR-250 bean 属性支持的注射方式。
对于Spring 托管的对象 Spring 可以以这种方式支持映射

@Resource有一个‘name’属性,缺省时,Spring 将这个值解释为要注射的 bean 的名字。换句话说,如果遵循by-name的语法


如果没有显式地给出名字,缺省的名字将继承于字段名或者 setter 方法名:如果是字段名,它将简化或者等价于字段名;如果是 setter 方法名,它将等价于 bean 属性名。


从javaee5规范开始,servlet增加了两个影响servlet生命周期的注解(annotation):@PostConstruct 与 @PreDestroy。这两个注解用来修饰一个非静态的void()方法:而且这个方法不能抛出异常声明。

当一个方法带有这些注解之一时,将被在其生命周期与 Spring 生命周期接口的方法或者显式声明回调方法同一刻上调用。


被@POSTconstruct修饰的方法会在服务器加载servlet的时候运行,并且被服务器调用一次,类似于Servlet的init()方法。被@POSTconstruct修饰的方法会在构造函数之后,init方法之前调用运行。


被@precontruct修饰的方法会在服务器卸载servlet的时候运行,并且只会被服务器调用一次,类似于servlet的destroy()方法。被@precontruct修饰的的方法会在destroy()方法之后运行,在servlet被彻底卸载之前。


Spring AOP】@Aspect结合案例详解(一): @Pointcut使用@annotation + 五种通知Advice注解(已附源码) 在微服务流行的当下,在使用SpringCloud/Springboot框架开发中,AOP使用的非常广泛,尤其是@Aspect注解方式当属最流行的,不止功能强大,性能也很优秀,还很舒心!所以本系列就结合案例详细介绍@Aspect方式的切面的各种用法,力求覆盖日常开发中的各种场景。本文带来的案例是:打印Log,主要介绍@Pointcut切点表达式的@annotation方式,以及 五种通知Advice注解:@Before、@After、@AfterRunning、@AfterThrowing、@Around。
推荐收藏系列:Spring boot 2.x注解Annotation大全(持续更新....) 为了方便我们在日常开发注解的使用,本文将开发所需要的注解统一并进行归类起来,并结合用例进行解析,这样收藏起来以便日后使用。最主要本文将持续更新日常使用的注解,也可以评论中告知其他注解。
spring基于注解的配置 **不管是 XML 还是注解,它们都是表达 Bean 定义的载体,其实质都是为 Spring 容器提供 Bean 定义的信息,在表现形式上都是将 XML 定义的内容通过类注解进****行描述。**Spring 从2.0开始就引入了基于注解的配置方式,在2.5时得到了完善,在4.0时进一步增强。
Spring(七)之基于注解配置 基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入。而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身。 在 XML 注入之前进行注解注入,因此后者的配置将通过两种方式的属性连线被前者重写。
1.加在set方法上, 默认按类型by type,如果在 spring 容器中能够找到多个相同的类型,就从这多个类型找查找有没有一个key的值跟我们的 ...
阿里特邀专家徐雷Java Spring Boot开发实战系列课程(第18讲):制作Java Docker镜像与推送到DockerHub和阿里云Docker仓库 立即下载