Home » Java » Spring Bean configuration and tagging (Annotation)...

Spring Bean configuration and tagging (Annotation) configuration

Provide a variety of ways

Spring framework for Spring Bean configuration, there is a common XML through the configuration file, by tagging (Annotation) configuration and configuration of Java. Based on the container which is based on Java application container configuration Java is often seen in the independent, common for Web applications is the XML file configuration and annotation (Annotation the configuration of.

)

focuses here on Web application tagging (Annotation), configuring.


1. Spring Bean callout (Annotation) configuration


in the Spring framework of the default XML configuration file, can be achieved through the annotation configuration register. When this Spring framework started first check the annotation of Spring Bean (Annotation).


common Spring Bean tagging (Annotation):

@Required, the setter method acting on attributes

@Autowired acts on constructors, attributes, and various methods (which can be replaced by JSR-330's @Inject)

@Primary gives the preferred
when multiple Bean are automatically bound
@Qualifier ("..."), when multiple Bean are automatically bound, select the
of a specific name
@Component ("beanName"), a generic component managed by Spring,

@Controller ("beanName"), a special case of @Component, the Java class
for controller functions
@Service ("beanName"), a special case of @Component, Java class
for general purpose functions
@Repository ("beanName"), a special case of @Component, for library Bean (such as DAO)

JSR-250's @Resource (name=,...) selects Bean by a specific name, giving priority to using the annotation, acting on the property's setter method,

The >JSR-250 of
The >
JSR-250 of


note: when the Spring framework starts, it first performs the configuration by tagging, and then executes the configuration through the XML file and covers the callout configuration, that is, the XML file configuration priority.



2. scan classpath to find the Spring Bean
that is suitable for injection
in the Spring framework of the default XML configuration file, through scanning to achieve the specified labeling package class Java (Annotation).

Note: configuration , implied the configuration of the



3., JSR-330, Dependency, Injection, for, Java standard annotation (Spring 3 start support)

The implementation class is located in the javax.inject package

@javax.inject.Inject, similar to Spring's @Autowired

@javax.inject.Named, similar to Spring's @Component

@Singleton, similar to Spring's @Scope ("Singleton")

@javax.inject.Qualifier, similar to Spring's @Qualifier

javax.inject.Provider, similar to the ObjectFactory class of Spring (note that this is a Java class, not a callout)

Latest