spring的annotation

spring bean container to create objects of ways:

1, using reflection to invoke the constructor with no arguments to create an instance (assuming no-argument constructor class) (normal mode)

2, obtained by the instance of the class factory (factory class implements the interface FactoryBean <?>)

3, the object is obtained by way of example bean plant (not need to implement any interface or inherit or superclass)

4, get the instance through the static factory

 

annotation configuration of ioc

1, @ Autowired
    1) by adding the following after @Autowired need to use xml configuration file to take effect: <context: annotation-config / >

 2) @Autowired assembled byType default attributes, if matched to a plurality of types of instances, and then determined by Bean byName.

2,@Resource

  1) @Resource role and @Autowired similar, but @Resource is the default first with byName, if you can not find the right and then to inject byType

  2) by adding the following configuration in the xml file to take effect: <context: annotation-config />

byNameBean is performed by the tag name or id attribute value matching injection (after matching the name of the removed setXxx set), byTypeis according to the type of Class Bean tag matches injection. (The type of set methods which the current class parameters,
            to find the object in the container matches)

eg:

 

 Note: only for automatic assembly [Object Type] function, do not work on the basic types.

@Autowired
    private Car car;
  <!-- autowire注解 默认bytype,再byname -->
   <bean name="car" class="com.briup.ioc.annotation.Car">
        <property name="price" value="33333"></property>
        <property name="name" value="test"></property>
    </bean>
    <bean name="car123456" class="com.briup.ioc.annotation.Car">
        <property name="price" value="122222"></property>
        <property name="name" value="baoma"></property>
    </bean>
Results: 
33333.0 
the Test

 

 

 

 

3, @ controller controller (injection service)
used for labeling the control layer, layer corresponds to the action of the struts

4, @ service service (DAO injection)
used for labeling the service layer, primarily used for service processing logic

5, @ repository (dao achieve access)
for annotation data access layer, may also be used for said annotation data access components, i.e. DAO components.

. 6, @ component (pojo instantiated into the normal spring vessel, corresponding to the configuration file
<bean id = "" class = "" / >)
refers to a variety of components, that is when our class does not belong to a variety of classification when (not part of @ Controller, @ Services, etc. when), we will be able to use @Component mark this class.

@Component all forms common bean components in Spring, @Repository @Service @Controller is of @Component (very little difference between them, it is preferable to use classified)

@Component an optional parameter that specifies the name of the bean
        @Component ( "boss")

If not specified, the default is the class name in lowercase current class

 @Component annotation can be defined directly bean, without having to define in xml. However, if two definitions exist, Bean definition will cover the definition of the class xml annotation

 @Component notes written directly to the top class

 

    Note:
    1.component-Scan label automatically scan the default packet in the case where the specified path (including all sub-packets)

    2.component-Scan tag with @Component @Repository @Service @Controller annotation class automatically registered into the spring container

    3.component-scan tag marked with the @Required @Autowired @PostConstruct @PreDestroy @Resource @WebServiceRef @EJB @PersistenceContext @PersistenceUnit like annotated classes corresponding to the operation, so that annotation into effect
    
    4.component-scan tag contains annotation-config the tag

Guess you like

Origin www.cnblogs.com/wskb/p/10939430.html