Spring framework learning (6) use ioc annotation to configure beans

Content from: Configuring beans using ioc annotations

 

context layer: 
context environment/container environment applicationContext.xml

1 ioc annotation function

Annotation simplifies xml file configuration such as hibernate mapping file

ioc annotations simplify the configuration of the ioc container

1 bean object definition process

@Component

This annotation is used to annotate a class

Annotate which classes need to be managed/instantiated using ioc

The marked class will become the object instantiated by the ioc

When the ioc container is resolved, it will scan all the classes marked with the annotation in the project, and instantiate them using the ioc method.

 

 

@Controller is used to annotate business logic objects xxServlet xxAction xxController

@Service is used to annotate the service type object xxService xxServiceImpl

@Repository is used to annotate persistent objects xxDao xxDaoImpl The usage and effect of these three annotations are exactly the same as those of @Component. For the standardization of program development, try to use which annotation to annotate what type of object.

2 Injection process between beans

@Autowired autowired. This annotation is used to annotate an attribute in the target object. According to the attribute name and attribute type of the annotated attribute, the matching bean is searched in the ioc container to obtain the required bean object.

 

Specific example: 
now add context layer support in the ioc container: 
including adding xmlns:context, xsi:schema, annotation scan address

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <!-- 
        ioc annotation function ~ context layer
            Import the namespace and schame file corresponding to the context layer
        If there are too many packet structures, scan com directly
     -->
    <context:component-scan base-package="com.etoak.action,com.etoak.dao"></context:component-scan>

</beans>

LoginAction.java: 
Note that the parameter la in @Component can be written or not written, the id value of the bean object is specified when it is written, and the default is the lowercase loginAction at the beginning of the class name.

@Component("la")   // <bean id="la" class="xx.LoginAction"/>
public class LoginAction {

    @Autowired
    private UserDaoImpl ud;
    /**
     * First according to the attribute name 'ud' of the marked attribute
     * Find a bean with id="ud" in the ioc container for injection
     * Then according to the attribute type 'UserDaoImpl' of the marked attribute
     * Find a class="UserDaoImpl" bean in the ioc container for injection
     * @return
     */


    public String execute() {
        System.out.println( "Process the login.action request submitted by the client" );
        ud.login ();
        return "success";
    }
}

UserDaoImpl.java

@Component     // <bean id="userDaoImpl" class="xx.UserDaoImpl"/>
public class UserDaoImpl {

    public boolean login(){
        System.out.println( "Connect to the database to determine whether the login is successful" );
         return  true ;
    }
}

Test class:

public class Test {

    public static void main(String[] args) {

        ApplicationContext ac = new 
            ClassPathXmlApplicationContext("applicationContext.xml");

        /**
         * 1 When instantiating bean objects using annotations
         * Because there is no specific ID value set for it
         */
        LoginAction la = ac.getBean(LoginAction.class);

        /**
         * 2 Although the ID value is not manually set for it
         * but the annotation will automatically give it an ID value
         * Lowercase class names  
         */
        UserDaoImpl ud = 
                (UserDaoImpl)ac.getBean("userDaoImpl");

        /**
         * 3 Manually set an ID value for it
         */
        LoginAction la2 = (LoginAction)ac.getBean("la");
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325169350&siteId=291194637