网站建设的主要产品,青岛建筑网,用discuz做商城网站,绿色资源网官网spring 注释介绍#xff1a; org.springframework.beans.factory.annotation和org.springframework.context.annotation包中存在的Spring注释通常称为Spring Core注释。 我们可以将它们分为两大类#xff1a;DI相关的注释和上下文配置注释#xff1a; 在本教程中#xff0… spring 注释 介绍 org.springframework.beans.factory.annotation和org.springframework.context.annotation包中存在的Spring注释通常称为Spring Core注释。 我们可以将它们分为两大类DI相关的注释和上下文配置注释 在本教程中我们将探索所有这些Spring Core批注。 与DI相关的注释 1. 我们使用Autowired标记将由Spring容器注入的依赖项。 它可以与构造函数setter或基于字段的注入一起使用。 构造函数注入 public class Employee {private Department dept;Autowiredpublic Employee(Department dept) {this.dept dept;}
} 对于基于构造函数的注入所有构造函数参数都是必需的。 从Spring 4.3开始除非我们有两个或多个否则我们可以跳过使用Autowired注释的显式注释构造函数的注释。 场注入 public class Employee {Autowiredprivate Department dept;
} 二传手注射 public class Employee {private Department dept;Autowiredpublic void setDept(Department dept) {this.dept dept;}
} Autowired批注还接受一个名为required的可选布尔参数。 默认情况下其值设置为true。 我们可以将其显式设置为false 当自动装配失败时Spring不会抛出异常。 2. 当我们有多个相同类型的bean时我们将Qualifier和Autowired注释一起使用以避免歧义。 假设我们有两个类 Component
public class Employee implements Person {}Component
public class Student implements Person {} 由于它们都实现了Person接口因此Spring无法使用Autowired知道要注入哪个Person bean。 要解决此问题我们可以使用Qualifier批注 public class Main {AutowiredQualifier(employee)private Person person;
} 就像Autowired一样我们可以将其与setter构造函数或基于字段的注入一起使用。 3. 现在我们知道当我们有多个相同类型的bean时可以将Qualifier和Autowired一起使用。 但是大多数时候我们特别需要这些bean的实例之一而很少需要其他实例。 我们可以 使用Primary批注 标记 最常用的bean。 有了它所有不合格的注入都将解析为我们的主bean。 Primary
Component
public class Employee implements Person {}Component
public class Student implements Person {}Component
public class EmployeeHandler {Autowiredprivate Person employee;
}Component
public class StudentHandler {AutowiredQualifier(student)private Person student;
} 由于我们已经在StudentHandler中指定了限定词因此将注入Student bean。 对于EmployeeHandler 我们跳过了限定符因此将注入Employee 。 就像Employee是我们类型Person的主要bean一样。 4. Bean是Configuration类中使用的方法级注释。 它标记了用于实例化Spring bean的工厂方法 Configuration
public class AppConfig {...Beanpublic Employee employee() {return new Employee();}
} 当需要返回类型的新实例时Spring将调用这些方法。 实例化的bean与工厂方法的名称相同。 如果要给它一个不同的名称可以使用此批注的name参数。 Bean(myEmp)
public Employee employee() {return new Employee();
}5. 默认情况下Spring在应用程序启动时实例化所有单例bean。 如果要防止这种急切的初始化可以使用Lazy批注。 当我们使用Lazy批注时将首先根据用户请求实例化bean。 我们可以将此注释放在 Bean带注释的方法可延迟特定的bean实例化 一个用Configuration注释的类以延迟创建该类中定义的所有bean 标记为Component的类 然后将延迟加载 以及在构造函数字段或setter上的Autowired注释。 同样在这里容器将不会加载注入的依赖项直到我们收到第一个用户请求 Configuration
public class AppConfig {BeanLazypublic Employee employee() {return new Employee();}Beanpublic Student student() {return new Student();}
} 它还接受一个可选的布尔参数值 默认值设置为true 。 如果将其设置为false 它将热切实例化该bean。 当我们有一个配置来延迟加载除少数几个以外的所有bean时这可以派上用场。 6. Required是在bean的setter方法上使用的方法级注释。 它只是标记我们要使用XML填充的依赖项 Required
void setName(String name) {this.name name;
}bean classcom.programmergirl.spring.Employeeproperty namename valueJoey /
/bean 如果不这样做它将抛出BeanInitializationException 。 7. 我们可以使用Value将外部源中定义的属性值注入到我们的bean中。 例如我们可以在application.yaml或application.properties文件中定义一些属性 james.employee.id 2563 然后将该值注入到我们的bean中 Value(${james.employee.id})
private String jamesEmpId; 我们也可以在SpEL中使用Value。 8. DependsOn注释可以强制Spring容器在使用DependsOn注释进行注释的bean之前初始化一个或多个bean。 通常此行为是自动的。 仅当我们具有隐式依赖项时才需要它例如加载JDBC驱动程序。 我们可以在任何直接或间接使用Component注释的类上或在使用Bean注释的工厂方法上使用DependsOn注释。 Configuration
public class AppConfig {BeanDependsOn(value {employee})public Dependent dependent() {return new Dependent();}}9. 我们使用Scope批注定义Component类或Bean定义的范围。 它可以是单例原型请求会话globalSession或某些自定义范围。 Component
Scope(prototype)
public class Employee {}10. 带有Lookup注释的方法告诉Spring在调用该方法时返回该方法的返回类型的实例。 它对以下有用 将原型bean注入单例实例 程序注入依赖 要了解如何将原型bean注入到singleton bean中请随时参考本文。 上下文配置注释 我们可以使用以下注释配置应用程序上下文 1. 个人资料 如果我们希望Spring仅在特定配置文件处于活动状态时使用Component类或Bean方法则可以使用Profile批注对其进行标记。 我们可以使用此批注的value参数提及配置文件的名称 Component
Profile(dev)
public class Employee {}2. 使用此批注我们可以指定一个或多个要导入的Configuration类。 Configuration
public class EmployeeConfig {Beanpublic Employee employee() {return new Employee();}
}Configuration
Import(EmployeeConfig.class)
public class AppConfig {Beanpublic Student student() {return new Student();}
} 这样我们就可以在初始化应用程序上下文时显式指定AppConfig类。 它将自动导入在EmployeeConfig中定义的bean 。 3. 我们可以使用此批注将bean从applicationContext.xml文件加载到ApplicationContext中 Configuration
ImportResource({classpath*:applicationContext.xml})
public class AppConfig {
}4. 此批注提供了一种方便的方法来定义用于应用程序设置的属性文件 Configuration
PropertySource(classpath:appConfig.properties)
public class DatabaseConfig implements InitializingBean {AutowiredEnvironment env;...void setDbConfig() {DataSourceConfig config new DataSourceConfig();config.setDriver(env.getProperty(jdbc.driver));config.setUrl(env.getProperty(jdbc.url));config.setUsername(env.getProperty(jdbc.username));config.setPassword(env.getProperty(jdbc.password));}}5. 我们可以使用此批注指定多个PropertySource配置 Configuration
PropertySources({ PropertySource(classpath:/student.properties),PropertySource(classpath:/employee.properties),PropertySource(classpath:/database.properties)
})
class AppConfig {} 从Java 8开始我们只需使用重复注释功能即可实现相同目的即直接指定多个PropertySource注释。 结论 在本文中我们介绍了最常见的Spring核心注释。 我们可以将它们用于Bean接线或配置应用程序上下文。 翻译自: https://www.javacodegeeks.com/2019/05/spring-core-annotations.htmlspring 注释