如今大部分的Spring项目都会用到注解。使用注解来替换xml,一行简单的注解就能够解决不少事情。可是你真的懂其中的原理吗。html
本文翻译于 https://docs.spring.io/spring-framework/docs and https://docs.spring.io/spring-framework/docsjava
在面试的时候 多少会问道 你了解过Spring注解吗。web
定义:指示一个类声明一个或者多个@Bean 声明的方法而且由Spring容器统一管理,以便在运行时为这些bean生成bean的定义和服务请求的类。面试
@Configuration public class AppConfig { @Bean public MyBean myBean(){ return new MyBean(); } }
上述AppConfig 加入@Configuration 注解,代表这就是一个配置类。有一个myBean()的方法,返回一个MyBean()的实例,并用@Bean 进行注释,代表这个方法是须要被Spring进行管理的bean。@Bean 若是不指定名称的话,默认使用myBean
名称,也就是小写的名称。spring
经过注解启动:apache
经过启动一个AnnotationConfigApplicationContext 来引导这个@Configuration 注解的类,好比:编程
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AppConfig.class); ctx.refresh();
在web项目中,也可使用AnnotationContextWebApplicationContext
或者其余变体来启动。api
使用SpringBoot项目的例子以下: app
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.spring.configuration</groupId> <artifactId>spring-configuration</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-configuration</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.6.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
在config 包下新建一个MyConfiguration环境配置,和上面的示例代码类似,完整的代码以下:maven
@Configuration public class MyConfiguration { @Bean public MyBean myBean(){ System.out.println("myBean Initialized"); return new MyBean(); } }
说明MyConfiguration 是一个配置类,可以在此类下面声明管理多个Bean,咱们声明了一个
MyBean
的bean,但愿它被容器加载和管理。
public class MyBean { public MyBean(){ System.out.println("generate MyBean Instance"); } public void init(){ System.out.println("MyBean Resources Initialized"); } }
public class SpringConfigurationApplication { public static void main(String[] args) { // AnnotationConfigApplicationContext context = = new AnnotationConfigApplicationContext(MyConfiguration.class) // 由于咱们加载的@Configuration 是基于注解形式的,因此须要建立AnnotationConfigApplicationContext AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); // 注册MyConfiguration 类并刷新bean 容器。 context.register(MyConfiguration.class); context.refresh(); } }
输出:
myBean Initialized generate MyBean Instance
从输出的结果能够看到,默认名称为myBean 的bean随着容器的加载而加载,由于myBean方法返回一个myBean的构造方法,因此myBean被初始化了。
经过XML 的方式来启动例子以下
经过XML 的方式来启动
能够经过使用XML方式定义的<context:annotation-config />
开启基于注解的启动,而后再定义一个MyConfiguration的bean,在/resources
目录下新建 application-context.xml
代码以下
<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd" > <!-- 至关于基于注解的启动类 AnnotationConfigApplicationContext--> <context:annotation-config /> <bean class="com.spring.configuration.config.MyConfiguration"/> </beans>
applicationContext.xml
,在SpringConfigurationApplication 须要进行引入,修改后的SpringConfigurationApplication以下:public class SpringConfigurationApplication { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); } }
输出:
myBean Initialized generate MyBean Instance
@Configuration 使用@Component 进行原注解,所以@Configuration 类也能够被组件扫描到(特别是使用XML context:component-scan
元素)。
在这里认识几个注解: @Controller,** @Service, @Repository, @Component**
@Controller
: 代表一个注解的类是一个"Controller",也就是控制器,能够把它理解为MVC 模式的Controller 这个角色。这个注解是一个特殊的@Component,容许实现类经过类路径的扫描扫描到。它一般与@RequestMapping 注解一块儿使用。
@Service
: 代表这个带注解的类是一个"Service",也就是服务层,能够把它理解为MVC 模式中的Service层这个角色,这个注解也是一个特殊的@Component,容许实现类经过类路径的扫描扫描到
@Repository
: 代表这个注解的类是一个"Repository",团队实现了JavaEE 模式中像是做为"Data Access Object" 可能做为DAO来使用,当与 PersistenceExceptionTranslationPostProcessor 结合使用时,这样注释的类有资格得到Spring转换的目的。这个注解也是@Component 的一个特殊实现,容许实现类可以被自动扫描到
@Component
: 代表这个注释的类是一个组件,当使用基于注释的配置和类路径扫描时,这些类被视为自动检测的候选者。
也就是说,上面四个注解标记的类都可以经过@ComponentScan 扫描到,上面四个注解最大的区别就是使用的场景和语义不同.
好比你定义一个Service类想要被Spring进行管理,你应该把它定义为@Service 而不是@Controller由于咱们从语义上讲,@Service更像是一个服务的类,而不是一个控制器的类,@Component一般被称做组件,它能够标注任何你没有严格予以说明的类,好比说是一个配置类,它不属于MVC模式的任何一层,这个时候你更习惯于把它定义为 @Component。@Controller,@Service,@Repository 的注解上都有@Component,因此这三个注解均可以用@Component进行替换。
来看一下代码进行理解:
@Component public class UserBean {} @Configuration public class UserConfiguration {} @Controller public class UserController {} @Repository public class UserDao {} @Service public class UserService {}
MyConfiguration
上加上@ComponentScan 注解,扫描上面5个类所在的包位置。代码以下:@Configuration @ComponentScan(basePackages = "com.spring.configuration.pojo") public class MyConfiguration { @Bean public MyBean myBean(){ System.out.println("myBean Initialized"); return new MyBean(); } }
public class SpringConfigurationApplication { public static void main(String[] args) { // AnnotationConfigApplicationContext context = = new AnnotationConfigApplicationContext(MyConfiguration.class) // ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(MyConfiguration.class); context.refresh(); // 获取启动过程当中的bean 定义的名称 for(String str : context.getBeanDefinitionNames()){ System.out.println("str = " + str); } context.close(); } }
输出:
myBean Initialized
generate MyBean Instance
str = org.springframework.context.annotation.internalConfigurationAnnotationProcessor
str = org.springframework.context.annotation.internalAutowiredAnnotationProcessor
str = org.springframework.context.annotation.internalRequiredAnnotationProcessor
str = org.springframework.context.annotation.internalCommonAnnotationProcessor
str = org.springframework.context.event.internalEventListenerProcessor
str = org.springframework.context.event.internalEventListenerFactory
str = myConfiguration
str = userBean
str = userConfiguration
str = userController
str = userDao
str = userService
str = myBean
由输出能够清楚的看到,上述定义的五个类成功被@ComponentScan 扫描到,并在程序启动的时候进行加载。
@Configuration 一般和Environment 一块儿使用,经过@Environment 解析的属性驻留在一个或多个"属性源"对象中,@Configuration类可使用@PropertySource,像Environment 对象提供属性源
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.spring.configuration</groupId> <artifactId>spring-configuration</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-configuration</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring.version>5.0.6.RELEASE</spring.version> <spring.test.version>4.3.13.RELEASE</spring.test.version> <junit.version>4.12</junit.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.test.version}</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = EnvironmentConfig.class) @Configuration @PropertySource("classpath:beanName.properties") public class EnvironmentConfig { @Autowired Environment env; @Test public void testReadProperty(){ // 获取bean.name.controller 的属性 System.out.println(env.getProperty("bean.name.controller")); // 判断是否包含bean.name.component System.out.println(env.containsProperty("bean.name.component")); // 返回与给定键关联的属性值 System.out.println(env.getRequiredProperty("bean.name.service")); } }
/resources
目录下新建beanName.properties 文件,以下:bean.name.configuration=beanNameConfiguration bean.name.controller=beanNameController bean.name.service=beanNameService bean.name.component=beanNameComponent bean.name.repository=beanNameRepository
启动并进行Junit测试,输出以下:
beanNameController
true
beanNameService
@Inject
: 这是jsr330 的规范,经过AutowiredAnnotationBeanPostProcessor 类实现的依赖注入。位于javax.inject包内,是Java自带的注解。
以下是@Inject的使用,不加@Named注解,须要配置与变量名一致便可。
@Inject @Named("mongo") private Mongo mongo;
@Autowired
: @Autowired是Spring提供的注解,经过AutowiredAnnotationBeanPostProcessor类实现的依赖注入,与@inject两者具备可互换性。位于org.springframework.beans.factory.annotation 包内,是Spring 中的注解
@Autowired默认是按照byType进行注入的,可是当byType方式找到了多个符合的bean,又是怎么处理的?Autowired默认先按byType,若是发现找到多个bean,则又按照byName方式比对,若是还有多个,则报出异常。
public class TestServiceImpl { // 下面两种@Autowired只要使用一种便可 @Autowired private UserDao userDao; // 用于字段上 @Autowired public void setUserDao(UserDao userDao) { // 用于属性的方法上 this.userDao = userDao; } }
@Autowired
注解是按照类型(byType)装配依赖对象,默认状况下它要求依赖对象必须存在,若是容许null值,能够设置它的required属性为false。若是咱们想使用按照名称(byName)来装配,能够结合@Qualifier注解一块儿使用。以下:
public class TestServiceImpl { @Autowired @Qualifier("userDao") private UserDao userDao; }
@Resource
: @Resource 是jsr250规范的实现,@Resource经过CommonAnnotationBeanPostProcessor 类实现注入。@Resource 通常会指定一个name属性,以下:
@Resource默认按照ByName自动注入,由J2EE提供,须要导入包javax.annotation.Resource。@Resource有两个重要的属性:name和type,而Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。因此,若是使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。若是既不制定name也不制定type属性,这时将经过反射机制使用byName自动注入策略。
public class TestServiceImpl { // 下面两种@Resource只要使用一种便可 @Resource(name="userDao") private UserDao userDao; // 用于字段上 @Resource(name="userDao") public void setUserDao(UserDao userDao) { // 用于属性的setter方法上 this.userDao = userDao; } }
注:
@Autowired
和@Inject
基本是同样的,由于二者都是使用AutowiredAnnotationBeanPostProcessor来处理依赖注入。可是@Resource是个例外,它使用的是CommonAnnotationBeanPostProcessor来处理依赖注入。固然,二者都是BeanPostProcessor。
@Autowired
和@Inject
默认autowired by type,能够经过@Qualifier显式指定autowired by qualifier name。
@Resource
默认autowired by field name,若是autowired by field name失败,会退化为autowired by type,能够经过@Qualifier显式指定autowired by qualifier name,若是autowired by qualifier name失败,会退化为autowired by field name。可是这时候若是autowired by field name失败,就不会再退化为autowired by type。
@Configuration 能够和@Value 和@PropertySource 一块儿使用读取外部配置文件,具体用法以下:
ReadValueFromPropertySource
类,代码以下@PropertySource("classpath:beanName.properties") @Configuration public class ReadValueFromPropertySource { @Value("bean.name.component") String beanName; @Bean("myTestBean") public MyBean myBean(){ return new MyBean(beanName); } }
经过@PropertySource引入的配置文件,使@Value 可以获取到属性值,在给myBean()方法指定了一个名称叫作myTestBean。
public class MyBean { String name; public MyBean(String name) { this.name = name; } public MyBean(){ System.out.println("generate MyBean Instance"); } public void init(){ System.out.println("MyBean Resources Initialized"); } @Override public String toString() { return "MyBean{" + "name='" + name + '\'' + '}'; } }
经过@PropertySource引入的配置文件,使@Value 可以获取到属性值,在给myBean()方法指定了一个名称叫作myTestBean
public class MyBean { String name; public MyBean(String name) { this.name = name; } public MyBean(){ System.out.println("generate MyBean Instance"); } public void init(){ System.out.println("MyBean Resources Initialized"); } @Override public String toString() { return "MyBean{" + "name='" + name + '\'' + '}'; } }
public class SpringConfigurationApplication { public static void main(String[] args) { // 为了展现配置文件的完整性,以前的代码没有删除。 // AnnotationConfigApplicationContext context = = new AnnotationConfigApplicationContext(MyConfiguration.class) // ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); // context.register(MyConfiguration.class); // context.refresh(); // // // 获取启动过程当中的bean 定义的名称 // for(String str : context.getBeanDefinitionNames()){ // System.out.println("str = " + str); // } // context.close(); ApplicationContext context = new AnnotationConfigApplicationContext(ReadValueFromPropertySource.class); MyBean myBean = (MyBean) context.getBean("myTestBean"); System.out.println("myBean = " + myBean); } }
使用Applicatio@InConntext 就可以获取myTestBean 这个bean,再生成myBean的实例。
输出:myBean = MyBean{name='bean.name.component'}
@Import
的定义(来自于JavaDoc):代表一个或者多个配置类须要导入,提供与Spring XML中相等的功能,容许导入@Configuration 、@ImportSelector、@ImportBeanDefinitionRegistar的实现,以及常规组件相似于AnnotationConfigApplicationContext。可能用于类级别或者是原注解。若是XML或者其余非@Configuration标记的Bean资源须要被导入的话,使用@ImportResource。下面是一个示例代码:
@Configuration public class CustomerBo { public void printMsg(String msg){ System.out.println("CustomerBo : " + msg); } @Bean public CustomerBo testCustomerBo(){ return new CustomerBo(); } } @Configuration public class SchedulerBo { public void printMsg(String msg){ System.out.println("SchedulerBo : " + msg); } @Bean public SchedulerBo testSchedulerBo(){ return new SchedulerBo(); } }
@Configuration @Import(value = {CustomerBo.class,SchedulerBo.class}) public class AppConfig {}
public class ImportWithConfiguration { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); CustomerBo customerBo = (CustomerBo) context.getBean("testCustomerBo"); customerBo.printMsg("System out println('get from customerBo')"); SchedulerBo schedulerBo = (SchedulerBo) context.getBean("testSchedulerBo"); schedulerBo.printMsg("System out println('get from schedulerBo')"); } }
输出 : CustomerBo : System out println('get from customerBo') SchedulerBo : System out println('get from schedulerBo')
@Profile
: 表示当一个或多个@Value 指定的配置文件处于可用状态时,组件符合注册条件,能够进行注册。** 三种设置方式:**
** 做用域**
注意:
@ImportResource
: 这个注解提供了与@Import
功能类似做用,一般与@Configuration 一块儿使用,经过AnnotationConfigApplicationContext 进行启动,下面以一个示例来看一下具体用法:
public class TestService { public TestService(){ System.out.println("test @importResource success"); } }
/resources
目录下新建 importResources.xml ,为了导入TestService<?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd" > <bean id = "testService" class="com.spring.configuration.config.TestService" /> </beans>
@Configuration @ImportResource("classpath:importResources.xml") public class ImportResourceWithConfiguration { @Autowired private TestService service; public void getImportResource(){ new TestService(); } public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ImportResourceWithConfiguration.class); context.getBean("testService"); } }
输出:test @importResource success
// 来自JavaDoc @Configuration public class AppConfig{ @Inject DataSource dataSource; @Bean public MyBean myBean(){ return new MyBean(dataSource); } @Configuration static class DataConfig(){ @Bean DataSource dataSource(){ return new EmbeddedDatabaseBuilder().build() } } }
在上述代码中,只须要在应用程序的上下文中注册 AppConfig 。因为是嵌套的@Configuration 类,DatabaseConfig 将自动注册。当AppConfig 、DatabaseConfig 之间的关系已经隐含清楚时,这就避免了使用@Import 注解的须要。
@Lazy
: 代表一个bean 是否延迟加载,能够做用在方法上,表示这个方法被延迟加载;能够做用在@Component (或者由@Component 做为原注解) 注释的类上,代表这个类中全部的bean 都被延迟加载。若是没有@Lazy注释,或者@Lazy 被设置为false,那么该bean 就会急切渴望被加载;除了上面两种做用域,@Lazy 还能够做用在@Autowired和@Inject注释的属性上,在这种状况下,它将为该字段建立一个惰性代理,做为使用ObjectFactory或Provider的默认方法。下面来演示一下:
@Lazy
注解,新增一个IfLazyInit()方法,检验是否被初始化。@Lazy @Configuration @ComponentScan(basePackages = "com.spring.configuration.pojo") public class MyConfiguration { @Bean public MyBean myBean(){ System.out.println("myBean Initialized"); return new MyBean(); } @Bean public MyBean IfLazyInit(){ System.out.println("initialized"); return new MyBean(); } }
public class SpringConfigurationApplication { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfiguration.class); // ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); // context.register(MyConfiguration.class); // context.refresh(); // // // 获取启动过程当中的bean 定义的名称 for(String str : context.getBeanDefinitionNames()){ System.out.println("str = " + str); } // context.close(); // ApplicationContext context = // new AnnotationConfigApplicationContext(ReadValueFromPropertySource.class); // MyBean myBean = (MyBean) context.getBean("myTestBean"); // System.out.println("myBean = " + myBean); } }
输出你会发现没有关于bean的定义信息,可是当吧@Lazy 注释拿掉,你会发现输出了关于bean的初始化信息:
myBean Initialized
generate MyBean Instance
initialized
generate MyBean Instance
Junit4 测试类,用于注解在类上表示经过Junit4 进行测试,能够省略编写启动类代码,是ApplicationContext 等启动类的替换。通常用@RunWith
和 @Configuration
进行单元测试,这是软件开发过程当中很是必要并且具备专业性的一部分,上面EnvironmentConfig
类证明了这一点:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = EnvironmentConfig.class) @Configuration @PropertySource("classpath:beanName.properties") public class EnvironmentConfig { // @Autowired // Environment env; @Inject Environment env; @Test public void testReadProperty(){ // 获取bean.name.controller 的属性 System.out.println(env.getProperty("bean.name.controller")); // 判断是否包含bean.name.component System.out.println(env.containsProperty("bean.name.component")); // 返回与给定键关联的属性值 System.out.println(env.getRequiredProperty("bean.name.service")); } }
@Enable 启动Spring内置功能
详情查阅
@EnableAsync
,@EnableScheduling
,@EnableTransactionManagement
,@EnableAspectJAutoProxy
,@EnableWebMvc
官方文档
** @Configuration 使用约束**