Spring learn the next day

    The next day a total learned the following:

  • The spring ioc common annotation;
  • Case notes using xml mode and single-mode table CRUD operations (but still require xml configuration file, not a pure sample configuration);
  • Annotation-based transformation Ioc case, pure notes of ways, the use of some of the new annotation spring;
  • junit spring and integration;

A, ioc Common notes:

 1.1 on creating objects, just as the xml <bean> tag, @ Component (used in other classes), @ Controller (generally used in the class presentation layer), @ Service (generally used in the class of business layer), @ the Repository (generally used in the persistence class)

 1.2 on injection data, @ AutoWired (by type automatic injection, may act on the variables and methods can be used without setter method);

           @Qualifier (by name and then injected on the basis of the injection according to the type can not be used alone as it is injected to the class members, need to rely on @Autowired, but that used in the previous method given parameter during injection, @ Qualifier ( " xx "));

                   @Resource (direct injection according to the bean id, can be used independently)

   More than three notes can only be injected into the bean types of data, and the basic types of data and String types can not be used to achieve the above comment. Further, the type of injection set can only be achieved by xml.                                                                                                                                 

         @Value (for injection type String and basic types of data, attributes: value, a value which can be used to specify the data in the spring SpEl (el expression in the spring), the wording: expression $ {})

1.3 for changing scope, @ Scope (scope specified for the bean, attributes: value, the specified range, with a value of: singleton (default), the prototype)

1.4 on life cycle: @PostConstruct (used to specify the method of initialization), @ PreDestory (used to specify the methods of destruction)

   When using annotations configuration, only informed bean.xml spring file when creating the container package to be scanned, with the <context: component-scan> base-package attribute to the tag.

Second, on the DBUtils

  DBUtils is a small frame dao layer, encapsulates the JDBC, map the result set to JavaBean. DBUtils not pass DataSource to create an object is to ensure that multiple statements share the same transaction (process have multiple interactions with the database in a business inside).

  •   JDBC: native way to access the database, access database trouble;
  •        DBUtils: a JDBC simple package, the package can automatically query result set, need to write SQL code;
  •        Mybatis: further encapsulated jdbc, Sql statements written in a configuration file, the object-oriented operation, a secondary cache;
  •        Hibernate: jdbc package of the most thorough, pure object-oriented, can not write the SQL;

Three, Spring new comment

  First, create a config package, create a file called SpringConfiguration class, where the package name and class name can easily play.

  @Configuration, the current class designated as a primary configuration class;

  @ComponentScan, Spring annotation specifies by when creating the container package to be scanned, attributes: value, the role of base-packages it is the same as the configuration of the <context: component-scan>.

  @Bean, for the current return value is stored as the spring Ioc container bean object, attributes: name, for specifying the bean id, the default value is the name of the current method.

  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. Action and find a way AutoWired notes are the same.

       At this point in the spring of acquiring core container object, no longer used classPathXmlApplication ( "xxx.xml"), but rather use AnnotationConfigApplicationContext (configuration class .class), because at this time no longer use bean.xml file configured.

  @Import, other configurations for introducing to the master configuration classes classes, attributes: value, other configurations for specifying the bytecode classes. When we use @Import notes, there @Import annotated class is the parent class configuration, while the import of all sub-class configuration.

  @PropertySource, specifies the location of the file properties, attributes: value, used to specify the name and path of the file. Keywords: classpath, indicates the class path, if the write packet package path, as @PropertySource ( "classpath: jdbcConfig.properties").

Four, Spring integration configuration Junit

  1. Import a jar junit integrated spring; (spring-test)

  2. A method to replace the original main @Runwith annotated using a spring to provide annotation Junit provided, based on the written test class; (@ Runwith (springJunit4classRunner.class))

  3. Tell the spring of container, ioc spring of creation is based on xml or annotation-based configuration, location and description; (@ContextConfiguration, attribute: Specify the location of the xml file, plus classPath keyword indicates the class path: ①locations ; ②classes: developing position where class notes, annotations profile );

  4. bean objects automatic injector; (@ AutoWired)

  

 1 @RunWith(SpringJUnit4ClassRunner.class)
 2 @ContextConfiguration(locations = "classpath:bean.xml")
 3 public class AccountServiceTest {
 4 
 5     @Autowired
 6     @Qualifier(value = "accountService")
 7     private IAccountService accountService;
 8 
 9     @Test
10     public void transferTest(){
11         String sourceName = "aaa";
12         String targetName = "ccc";
13         Float money = 500.23f;
14         accountService.transfer(sourceName,targetName,money);
15     }
16 }

 

  When using spring5.x version, it requires at least the junit jar version 4.12 and above;

 

Guess you like

Origin www.cnblogs.com/zxgCoding/p/11479836.html