Spring (1) Bean comments

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/m0_38060977/article/details/102756495

@Autowired:

Automatic injection

  1. Find the corresponding default priority according to the type of component to the container: applicationContext.getBean (BookDao.class); assignment can be found
  2. If a plurality of components of the same type is found, then the property name as the id to find a container assembly
  3. @Qualifier ( "bookDao"): Use @Qualifier id specified component assembly required, instead of using the attribute name
  4. The default automatic assembly be sure to assign attribute good, no error will be;
    you can use @Autowired (required = false);
  5. Support @Primary

In the automatic assembly methods and constructors
1. Default added ioc container assembly, the container calls the constructor with no arguments will start to create an object, then the assignment operations such as initialization
2. [Method marked position,]: @Bean + Method parameter; parameter acquisition from the container; the default does not write @Autowired effect is the same; can be automatic assembly

    @Bean//@Autowired可省略
	public Color color(Car car){
		Color color = new Color();
		color.setCar(car);
		return color;
	}

3. [marked on the constructor]: If the component is only a constructor parameter, this parameter has @Autowired constructor may be omitted, or the position of the component parameters may be automatically acquired from the vessel

	//如果只有一个构造器,@Autowired可以省略
	public Boss(Car car){
		this.car = car;
		System.out.println("Boss...有参构造器");
	}

@Resource:

  • And automatic assembly can function as @Autowired; default is assembled according to the component name;
  • No support @Primary function; no support @Autowired (reqiured = false)
  • When that matches the name can not be found when assembled in accordance with the type of bean. However, note that if the name attribute if specified, it will only be assembled by name

@Resource assembly sequence:
① If both name and type, from the Spring context to find a unique match of bean assembled, can not find an exception is thrown.
② If the name is specified, from the context, find the name (id) match bean assembled, can not find an exception is thrown.
③ If the type is specified, from the context find a unique bean similar matches for assembly, can not find or find more, will throw an exception.
④ If neither specified name, and no specified type, according to the automatic assembly byName manner; if there is no match, then the match is a backoff primitive type, if a match is automatically assembled.
@Resource effect equivalent to @Autowired, but @Autowired automatic injection according to byType

@Component

@Component are all common forms of Spring-managed components, @ Component annotation can be placed on the head of the class, @ Component is not recommended.

@Controller

The corresponding presentation layer Bean
after @Controller use annotations to identify UserAction, you said we should UserAction to the Spring container management, there will be a name for the "userAction" of action in the Spring container, the name is based on the class name UserAction taken. Note: If you do not specify the value @Controller @Controller [], the default name for the bean class name in lowercase first letter of the class, if the specified value [@Controller (value = "UserAction")] or [@Controller ( "UserAction ")], then use the value as the name of the bean.
UserAction used herein also @Scope annotation, @ Scope ( "prototype") Action shows a prototype of the stated range, the container may be utilized scope = "prototype" to ensure that every request has a separate process to Action to avoid struts Action in the thread-safety issues. spring default scop

@Primary

When multiple candidates when Bean automatic assembly, is annotated as @Primary Bean will serve as the first choice for those who would otherwise throw an exception

@Bean

Is a comment on a method level, mainly used in @Configuration annotated class, can also be used in @Component annotated class. Add the bean id is the method name
to accept lifecycle callbacks

public class Foo {
    public void init() {
        // initialization logic
    }
}

public class Bar {
    public void cleanup() {
        // destruction logic
    }
}

@Configuration
public class AppConfig {

    @Bean(initMethod = "init")
    public Foo foo() {
        return new Foo();
    }

    @Bean(destroyMethod = "cleanup")
    public Bar bar() {
        return new Bar();
    }

}

bean alias

    @Bean(name = { "dataSource", "subsystemA-dataSource", "subsystemB-dataSource" })
    public DataSource dataSource() {
        // instantiate, configure and return DataSource bean...
    }

@configuration

Configuration class called repeatedly @bean modified method, the object returns to the same
particular see: https://www.cnblogs.com/duanxz/p/7493276.html

Recommended Reading

  1. I was the interviewer to abuse ignorant, it turned out to be because I do not understand in Spring @Configuration

reference

Guess you like

Origin blog.csdn.net/m0_38060977/article/details/102756495