7.01
@AutoWired default priority to find and consistent type of bean, if there are many types of container bean, the default will inject the same bean object id and name @Autowired annotated bean. You can use @Qualifier ( "XXXX (bean of the id)") to specify that in the end inject bean. If you find the same type of Bean in the container, it will throw an exception, then you can use @AutoWired annotation attributes required = false, which would give the bean == null.
Use @Primary annotations Bean, when @autowired, the default is first injected into @Primary annotated bean
Also supports the use @Resource (JSR250 specification) and @Inject (JSR330)
@Resouces @Autowired role and the role of the same, can be used for automatic assembly, the default priority configuration id and attribute names, like the bean, can also be used
@Resources(name="XXX")
to specify a specific Bean@Autowired not only to the method of annotation, the annotation may further process (may be a builder) on.
@Component public class Car { private Person person; /*当注解到方法时,ioc容器初始化bean会把方法的入参从容器中自动获取,然后执行这个方法(构造器也是)*/ @Autowired public void setPerson(Person person) { this.person = person; } @Override public String toString() { return "Car{" + "person=" + person + '}'; } }
The method of annotation @Bean default parameter acquired from the container
Poured into a container assembly of the components can be obtained by implementing the underlying Spring XXXAware interface.
@Component public class Car implements EmbeddedValueResolverAware { private Person person; private int age; /*当注解到方法时,会把方法的入参从容器中自动获取,然后执行这个方法*/ @Autowired public void setPerson(Person person) { this.person = person; } @Override public String toString() { return "Car{" + "person=" + person + ", age=" + age + '}'; } @Override public void setEmbeddedValueResolver(StringValueResolver stringValueResolver) { /*可以解析Spel*/StringValueResolver String s = stringValueResolver.resolveStringValue("你的系统是 ${os.name}"); System.out.println(s); } }
@Profile specify what those components enabled environment (for the data source !!!!)
- When registration assembly, in the method (the method may be, indicating that all the following components of the whole class configuration) plus the above
@Profile("XXX(如test、dev等)")
, the container when activated, will determine what component is to be loaded (the environment variable according to an environment variable (-Dspring.profiles.active = test) you can set jvm, also by way of the code, but the code is no way to be a reference in the form to create the container). If there is no profile comments, no matter what the environment will be registered, provided that the configuration profile has not been annotated class
- When registration assembly, in the method (the method may be, indicating that all the following components of the whole class configuration) plus the above