以前的注解文章仅仅只是叙述文章中心的注解, 仍旧基于XML配置而非注解扫描. (@Senyag)html
Java:
1.8
javaMaven:
3
springSpringFramework版本以及各组件成员:
5.1.1.RELEASE
app
- spring-context
- spring-core
- spring-beans
如下是官方文档说明:框架
Starting with Spring 3.0, many features provided by the Spring JavaConfig project became part of the core Spring Framework. Thus, you can define beans external to your application classes by using Java rather than XML files. To use these new features, see the @Configuration, @Bean, @Import, and @DependsOn annotations.ide
从Spring 3.0开始,Spring JavaConfig项目提供的许多功能都成为核心Spring Framework(Core)的一部分。所以,您能够使用Java而不是XML文件在应用程序类外部定义bean。要使用这些新功能,请参阅
@Configuration
,@Bean
,@Import
和@DependsOn
注解。this
@Configuration
- 表示这个类能够使用 Spring IoC 容器做为 bean 定义的来源。.net
@Bean
- 表示方法生成由Spring容器管理的bean。code
@Import
- 表示要导入的一个或多个@Configuration类。component
@DependsOn
- 代表有依赖条件的一个类时, 那些被它依赖的Bean类将先被初始化.
(换句话说哪一个Bean被注解了这个, 那么它所依赖的Bean都会先初始化)
"Spring容器载入bean顺序是不肯定的,Spring框架没有约定特定顺序逻辑规范。但spring保证若是A依赖B(如beanA中有@Autowired B的变量),那么B将先于A被加载。但若是beanA不直接依赖B,咱们如何让B仍先加载呢?"
---- 引用自: https://blog.csdn.net/neweastsun/article/details/78775371
这里先了解
@Configuration
和@Bean
.@DependsOn
和@Import
后面再谈...
@Configuration
&@Bean
的简单使用)本示例参考: https://www.cnblogs.com/microcat/p/7074720.html
做用: 简单演示功能 -- 仅仅获取一个Bean并调用其中方法, 并不存在依赖关系.
将了解:
@Bean
用于注册一个SpringBean.@Configuration
代表这是个与Spring相关的类, 能够做为Bean定义的来源.
HelloWorld.java
非JavaBean规范
package yag; public class HelloWorld { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
HelloConfig.java
package yag; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class HelloConfig { // 将HelloWrold类注册为一个Bean @Bean public HelloWorld helloWorld() { return new HelloWorld(); } }
至关于如下XML配置:
<beans> <bean class="yag.HelloWorld"/> </beans>
spring-beans.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:annotation-config/> <context:component-scan base-package="yag"/> </beans>
Runner.java
package yag; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Runner { public static void main(String[] args){ ApplicationContext context = new AnnotationConfigApplicationContext(HelloConfig.class); // 获取这个bean HelloWorld helloWorld = context.getBean(HelloWorld.class); // 调用方法 helloWorld.setMessage("Hello World"); System.out.println(helloWorld.getMessage()); } }
Hello World Process finished with exit code 0
@Autowired
进行Setter方法注入)在以前的文章中已经介绍了一些经过注解进行注入的示例了. 但那些文章目的仅仅是了解那些注解如何使用, 仍旧是基于XML配置环境的. 如下将了解在注解配置中的使用方式.
背景:
HelloWorld
是一个bean. 至于Bean使用者就是HelloWorldUser
, 它将调用HelloWorld
中的sayHello()
方法(这须要一个HelloWorld
实例).
HelloWorld.java
非JavaBean规范
package yag; import org.springframework.context.annotation.Configuration; @Configuration public class HelloWorld { public void sayHello(){ System.out.println("Hello World"); } }
HelloWorldUser.java
package yag; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; @Configuration public class HelloWorldUser { private HelloWorld helloWorld; @Autowired // 在setter方法上, 以实现byName装配(将匹配id="helloWorld"的Bean) public void setHelloWorld(HelloWorld helloWorld) { this.helloWorld = helloWorld; } public void useHelloWorld(){ helloWorld.sayHello(); } }
HelloConfig.java
package yag; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class HelloConfig { @Bean(name = "helloWorld") // id="helloWorld" public HelloWorld helloWorld(){ return new HelloWorld(); } @Bean public HelloWorldUser helloWorldUser(){ return new HelloWorldUser(); } }
Runner.java
package yag; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Runner { public static void main(String[] args){ ApplicationContext context = new AnnotationConfigApplicationContext(HelloConfig.class); HelloWorldUser helloWorldUser = context.getBean(HelloWorldUser.class); helloWorldUser.useHelloWorld(); } }
spring-beans.xml
为了缩短篇幅, 就罗列这些, 具体的dtd能够从上面的示例找.
<context:annotation-config/> <context:component-scan base-package="yag"/>
Hello World Process finished with exit code 0
@Bean
的随记值得一提的是, @Bean
提供了不少参数.