spring annotation-based configuration IOC

For creating an object: equivalent <the bean ID = "" class = "">
  @Component

    Role:
      to let spring to manage resources. Equivalent to a configuration in xml bean.
    Property:
      value: specifying the bean id. If you do not specify a value attribute, the default bean id of the current class is the class name, the first letter lowercase.

  @Controller @Service @Repository

    Annotations are notes for the three-derived @Component of their role and attributes are exactly the same, just with a more explicit semantic.
      @Controller: General notes for the presentation layer.
      @Service: general business for the annotation layer.
      @Repository: generally used for annotation persistence layer.
    Details: If there is one and only one annotation attribute to assign, and the name is value, value can not write at the time of assignment.

Data for injection: equivalent <property name = "" ref = ""> <property name = "" value = "">

  @Autowired

    Action:
      automatic injection according to the type, when using injection property annotation, SET methods can be omitted, it can only inject other bean types. When there are multiple types of matches,
      use to inject the object variable names referred to as the bean id, find the container in the spring, you can also find a successful implantation, can not be found on the error.
  @Qualifier

    Action:
      in accordance with the type automatically based on the injection, according to the Bean refilling id. It can not be used independently when a field injection, must be used with @Autowire;
      However, when a method of injection parameters (injection data when using the constructor, the constructor parameter to use), can be used independently.
    Property:
      value: specifying the bean id.

  @Resource

    Role:
      direct injection according to Bean's id. It can only be injected into other bean types.
    Attribute:
      name: specifying the bean id.

  @Value

    Action:
      injecting basic data types and data type String
    attribute:
      value: specifies values

  Collection type of injection can only be achieved through xml

For changing the scope of: the equivalent of <the bean ID = "" class = "" scope = "" >

  @Scope

    Role:
      Specifies the scope of the bean.
    Attribute:
      value: value of the specified range.
      Value: singleton prototype request session globalsession

And the lifecycle: equivalent <the bean ID = "" class = "" the init-Method = "" the destroy-Method = "" />

  @PostConstruct

    Action:
      specifies the initialization method.

  @PreDestroy

    Role:
      used to specify the method of destruction.

Example:

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    public UserServiceImpl() {
        System.out.println ( "Service to create objects of" );
    }

    @Override
    public void saveUser() {
        System.out.println ( "Service to save the user" );
        userDao.saveUser();
    }
}
@Repository("userDao")
public class UserDaoImpl implements UserDao {

    public UserDaoImpl() {
        System.out.println ( "DAO objects created" );
    }

    @Override
    public void saveUser() {
        System.out.println ( "DAO save users" );
    }
}

  Note: When using annotation injection, set methods do not write

Create a spring xml configuration file and open support for annotations:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd " > 
  <-! When told that spring to create a container to be scanned bag -> 
  < context: Scan-Component Base-Package = "com.fgy" > </ context: Scan-Component > 
</ Beans >

  Note: annotation-based integration, we need to import the constraints of a multi-context namespace import constraints.

Guess you like

Origin www.cnblogs.com/roadlandscape/p/12301103.html