每天注解学习(七)maven项目整合到springboot启动类注解

1、注解使用

maven项目类注入到spring中,加上注解:

@Component

springboot初始化类,注解:

@Configuration
public class AppConfig {

    @Bean(initMethod = "init")
    @DependsOn(value = "task")
    public OcppClient ocppClient() {
        return new OcppClient();
    }

    @Bean(initMethod = "init")
    @DependsOn(value = "taskManager")
    public Task task() {
        return new Task();
    }

    @Bean(initMethod = "init")
    public TaskManager taskManager() {
        return new TaskManager();
    }

}

实例化Bean实例,init方法:

 @Bean(initMethod = "init")

类依赖的启动顺序:

@DependsOn(value = "taskManager")

 

2、注解原理解析:

(1)@Component

把普通pojo实例化到spring容器中,相当于配置文件中的 (<bean id="" class=""/>

泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于@Controller、@Services等的时候),我们就可以使用@Component来标注这个类,下面写这个是引入component的扫描组件 :

<context:component-scan base-package=”com.mmnc”>

 其中base-package为需要扫描的包(含所有子包) 。

注解解析:https://blog.csdn.net/heroqiang/article/details/79019415

(2)@Bean

它的实现接口类为:

@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Bean {

	/**
	 * Alias for {@link #name}.
	 * <p>Intended to be used when no other attributes are needed, for example:
	 * {@code @Bean("customBeanName")}.
	 * @since 4.3.3
	 * @see #name
	 */
	@AliasFor("name")
	String[] value() default {};

	/**
	 * The name of this bean, or if several names, a primary bean name plus aliases.
	 * <p>If left unspecified, the name of the bean is the name of the annotated method.
	 * If specified, the method name is ignored.
	 * <p>The bean name and aliases may also be configured via the {@link #value}
	 * attribute if no other attributes are declared.
	 * @see #value
	 */
	@AliasFor("value")
	String[] name() default {};

	/**
	 * Are dependencies to be injected via convention-based autowiring by name or type?
	 * <p>Note that this autowire mode is just about externally driven autowiring based
	 * on bean property setter methods by convention, analogous to XML bean definitions.
	 * <p>The default mode does allow for annotation-driven autowiring. "no" refers to
	 * externally driven autowiring only, not affecting any autowiring demands that the
	 * bean class itself expresses through annotations.
	 * @see Autowire#BY_NAME
	 * @see Autowire#BY_TYPE
	 */
	Autowire autowire() default Autowire.NO;

	/**
	 * The optional name of a method to call on the bean instance during initialization.
	 * Not commonly used, given that the method may be called programmatically directly
	 * within the body of a Bean-annotated method.
	 * <p>The default value is {@code ""}, indicating no init method to be called.
	 */
	String initMethod() default "";

	/**
	 * The optional name of a method to call on the bean instance upon closing the
	 * application context, for example a {@code close()} method on a JDBC
	 * {@code DataSource} implementation, or a Hibernate {@code SessionFactory} object.
	 * The method must have no arguments but may throw any exception.
	 * <p>As a convenience to the user, the container will attempt to infer a destroy
	 * method against an object returned from the {@code @Bean} method. For example, given
	 * an {@code @Bean} method returning an Apache Commons DBCP {@code BasicDataSource},
	 * the container will notice the {@code close()} method available on that object and
	 * automatically register it as the {@code destroyMethod}. This 'destroy method
	 * inference' is currently limited to detecting only public, no-arg methods named
	 * 'close' or 'shutdown'. The method may be declared at any level of the inheritance
	 * hierarchy and will be detected regardless of the return type of the {@code @Bean}
	 * method (i.e., detection occurs reflectively against the bean instance itself at
	 * creation time).
	 * <p>To disable destroy method inference for a particular {@code @Bean}, specify an
	 * empty string as the value, e.g. {@code @Bean(destroyMethod="")}. Note that the
	 * {@link org.springframework.beans.factory.DisposableBean} and the
	 * {@link java.io.Closeable}/{@link java.lang.AutoCloseable} interfaces will
	 * nevertheless get detected and the corresponding destroy/close method invoked.
	 * <p>Note: Only invoked on beans whose lifecycle is under the full control of the
	 * factory, which is always the case for singletons but not guaranteed for any
	 * other scope.
	 * @see org.springframework.context.ConfigurableApplicationContext#close()
	 */
	String destroyMethod() default AbstractBeanDefinition.INFER_METHOD;

}

可以看到它有五个实现方法,在注入时,我用的InitMethod方法。

 

(3)@DependsOn

 源码如下,即获取所有依赖的类,放入spring容器中:

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DependsOn {

	String[] value() default {};

}

 


版权声明:本文为u014252478原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。