20200112-- annotation-based development spring

Notes for the persistence layer dao
Here Insert Picture Description

Notes service for the business layer
Here Insert Picture Description
following variables should be annotated with comments up @Autowired

This configuration file still exists bean.xml

SpringConfiguration create a file, the same role in beal.xml

spring new notes, Configuration Role: Specifies the current configuration class is a class,
Here Insert Picture Description

ComponentScan effect: for the annotation specifies the spring when creating a container, you want to scan packages
action attribute value and basePackages it is the same, is used to specify the container to be scanned to create a package
that we use this annotation is equivalent to the xml configuration

    <context:component-scan base-package="com.itheima"></context:component-scan>
  public QueryRunner createQueryRunner(DataSource dataSource){
        return new QueryRunner(dataSource);
    }

For creating a queryrunner object. Preceded Spring @Bean automatically added to the vessel.
Action: the current value returned by spring ioc stored as bean container object
property name: specifying the bean id, the default value is the name of the method

 @Bean("dataSource")
    public DataSource createDataSource(){
        try{
            ComboPooledDataSource ds = new ComboPooledDataSource();
            ds.setDriverClass("com.mysql.jdbc.Driver");
            ds.setJdbcUrl("jdbc:mysql://localhost:3306/eesy?serverTimezone=GMT%2B8&amp;characterEncoding=utf-8");
            ds.setUser("root");
            ds.setPassword("password");
            return ds;
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }

Details: When we use the annotation configuration method, if the method has parameters, spring framework will have to find the bean container object is not available
ways to find and Autowired notes the role is the same

The method may also be configured to annotate class with @Scope, into a multi-mode embodiment

Details: configuration notes, when the configuration class as an argument AnnotationConfigApplicationContext object created, the comment can not write

Self-summary
of working procedures Test file
Here Insert Picture Description
test file, called findAllAccount this method
this method is IAccountService interface defines the methods
the entity class business layer, but also by the data persistence layer called
the final method in AccountDaoImpl the implementation class in
Here Insert Picture Description
this method use this object to the runner,
runner class objects are configured in the config folder
Here Insert Picture Description
need @Configuration notes that he is a configuration class
in front of the class configuration method plus @Bean will be automatically added to the Spring container.
Action: the current value returned by spring ioc stored as bean container object
property name: specifying the bean id, the default value is the name of the method

Other configurations for introducing import class
attribute value specifies other types of bytecode
when we use the import annotations, annotation has import class is the main class configuration

propertysource
position properties for the specified file name and file path attribute value specified
keywords: classpath, represents the classpath

Analysis
Application inlet main method
junit unit test, no main method can also be performed
junit incorporates a main method
which will determine whether the current test classes which annotation methods @Test
junit so that there will be annotated test method execution,
junit not care whether we use spring framework
in the implementation of the test mode, junit do not know if we're using spring framework
so it reads the configuration file or configuration class for our core container to create
, execute From the above test method when, no ioc container, even wrote autowired notes, can not be achieved injection

spring integrating junit configuration
import spring integrating junit jar package
using annotations junit provided the main method replaces the original, replaced by spring provided
@Runwith
inform the runner spring, spring and ioc creation is based on xml or annotation location and Description
@ContextConfiguration
locations: Specifies the xml file location, plus classpath keywords, expressed in the class path
classess: specify the location where the class notes

When we use the spring 5.x version requires junit jar version 4.1.1 version must be more than

Published 657 original articles · won praise 39 · views 60000 +

Guess you like

Origin blog.csdn.net/qq_36344771/article/details/103943742