Spring commonly used notes and action

@Component (value)
configuration class, when this annotation, SpringIOC this class will be automatically scanned into a bean instance
is not the time of writing, the default is the class name and the first letter lowercase

@ComponentScan
default representative scans the current packet
contains two parameters:
basePackages: a plurality of packages may be scanned, is an array type, may result in generating a plurality Bean instance
basePackageClasses: a plurality of scanning methods can, is an array type, may resulting in generating multiple instances Bean

@Autowired
automatic assembly, searches for the specified Bean depending on the type, required: If you say in the bean represents not find the specified directory does not require injection of annotations needed, modify the default is true to false, indicating if that is not found can not be allowed injection.

@Primary
means that when an interface or abstract class by injection, multiple implementation classes, then use the annotation can tell IOC, priority injection Parmary modified

@Qualifier
If you say that when a query method specified by name, using Qulifier (method name)

@Bean
when using this annotation, SpringIOC this method will be automatically scanned into a bean instance
when not writing, the default is the method name and the first letter lowercase
can use the bean set name (bean name), is a reference to whether autowire Bean method of defaults Autowire.NO, initMothed initialization method, destroyMethod methods of destruction

@Scope
may be provided with a plurality of the objects of the annotation bean

@ImportResource ({ "classpath: xxx.xml"
}) capable of reading a plurality of xml files, can be introduced so that a plurality of defined xml the bean
@Import ({xx.class, xxx.class})
capable of reading a plurality of class file

@Profile
using the annotation can configure the plurality of data sources to a spring connection pool object
when disposed in the bean,
may be used <beans profile = "data source. 1"> </ Beans>
<beans profile = "Data Source 2"> </ Beans>
1. in class configuration, when used, it is necessary to activate the specified class
@ActiveProfiles ( "data source 1")
2 arranged in the virtual
configuration in web.xml
<the init-param>
<param -name> spring.profiles.active </ param-name>
<param-name> data source. 1 </ param-name>
</ the init-param>
<the init-param>
<param-name> spring.profiles.active < / param-name>
<param-name data source 2 </ param-name>>
</ the init-param>
there are two

Loading a configuration file:
Use annotation: @PropertySource (value = "classpath: xxx")
using the xml configuration file <context: property-placeholder location = "classpath: db.properties" />


@Contional ({xx.class}) (Learn)
can be conditioned, the bean assembly;

@Value ( "# {xxx}"
) can be directly assigned to the attribute
can be directly assigned to the bean element

Guess you like

Origin www.cnblogs.com/anonymityning/p/11803119.html