8.1 Experiment 32 of Annotation configuration OF Dao, Service and Controller components Create a Dao, a Service, and a Controller via annotations. Spring configuration bean annotations include @controller to configure web layer components @service to configure Service layer components @repository to configure Dao layer components @Component Components other than the Web layer, service layer, and DAO layer. @scope configures the Scope

Bean object

/** * @component is equivalent to <br/> * <bean id="book" class="com.pojo. book" /> */ @component public class book {} /** * @repository = <br/> * <bean id="bookDao" class=" com.dao.bookdao "/> */ @repository public class bookDao {} /** * <br/> * <bean id="bookService" class="com.service. bookService" /> */ @service public class bookService {} / * * * @ Controller is equivalent to < br / > * < bean id = "bookServlet" class = "com. Controller. BookServlet" / > * / @ Controller public class BookServlet {}Copy the code

Context :include-filter Context :include-filter Context :include-filter
Set the content to be included. Note: You usually need to use this in conjunction with the use-default-filters property to achieve the “only some components” effect.
Sets the excluded content by setting the use-default-filters property to false

Annotation com.xxxAnnotation Filters all classes that annotate XxxAnnotation. This rule filters all subclasses of the BaseXxx class assignable com.BaseXxx based on whether the target component has annotations of the specified type. This rule filters based on whether the target component is a subclass of the specified type.

Applicationcontext.xml is configured as follows

<! -- use-default-filters="false" sets the default inclusion rule to be disabled --> <context:component-scan base-package="com" use-default-filters="false"> <! <context:include-filter type="annotation" --> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> <! --> <context:exclude-filter type="assignable" expression="com.atguigu.service.BookService"/> </context:component-scan>Copy the code

The above configuration will include all @service annotated classes. Eliminate com. Service. BookService class

Exclusion examples of packet scanning:

<! -- context:component-scan Base package set the base package you want to scan. Base package="com" And all of its subpackages are scanned --> <context:component-scan base-package="com"> <! -- context:exclude-filter exclude certain classes type="annotation" specifies the annotation to exclude expression Which annotation to exclude --> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/> <! <context:exclude-filter type="assignable" expression=" com.dao.bookDAO "/> </context:component-scan>Copy the code

Example of package scan inclusion:

<! -- context:component-scan Base package set the base package you want to scan. Base package="com" Use-default-filters ="false" Disable the default packet-scanning inclusion rule. The custom inclusion rule must be used in conjunction with use-default-filters="false" --> <context:component-scan base-package="com" use-default-filters="false"> <! - < context: include - filter type = "annotation" expression = "org. Springframework. Stereotype. Service" / > according to the comments in the form of configuration includes scanning rules - > <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/> <context:include-filter type="assignable" expression="com.atguigu.dao.BookDao"/> </context:component-scan>Copy the code

Experiment 35: Use @AutoWired annotation to achieve automatic assembly according to the type ★

The @AutoWired annotation automatically looks up the corresponding class in the Spring container based on the annotated object type. If it finds it, it automates. With the @autowired annotation, no get/set methods are required

Public class BookService {/** * @autoWired configure automatic injection * 1. */ @Autowired private BookDao bookDao; @Override public String toString() { return "BookService [bookDao=" + bookDao + "]"; }}Copy the code

If there is more than one resource type bean, the default is to look up the bean according to the member variable name of the @autoWired annotation tag as the ID

/** * @repository = <br/> * <bean id="bookDao" class="com.dao.BookDao" scope="singleton"/> */ @repository public class BookDao {} /** * @repository is equivalent to <br/> * <bean id=" BookDao" class="com.dao.BookDao" scope="singleton"/> */ @repository Public class BookDaoExt extends BookDao{} /** * @service = <br/> * <bean id="bookService" Class =" com.service.bookService "/> */ @service (" BookService") // @scope ("singleton") // Public class BookService {/** * @autoWired configure automatic injection * 1. <br/> <br/> <br/> <br/> <br/> <br/> * */ @Autowired private BookDao bookDao; @Override public String toString() { return "BookService [bookDao=" + bookDao + "]"; }}Copy the code

The @qualifier annotation can be used to explicitly specify the id of the target bean if the bean is not found based on the member variable name

@service ("bookService") // <bean id="bs" class=" com.service.bookService "/> // @scope ("singleton") // @scope ("prototype") public class BookService {/** * @autoWired; <br/> <br/> <br/> <br/> <br/> <br/> * */ @Qualifier("bookDao") @AutoWired Private bookDao bookDao2; @Override public String toString() { return "BookService [bookDao=" + bookDao2 + "]"; }}Copy the code

39: The @Autowired annotation’s Required attribute specifies that a property is not allowed to be set

/** * @autowired configure automatic injection * 1. <br/> <br/> <br/> <br/> <br/> <br/> * * @qualifier ("bookDao") can be set to look up and inject according to the specified ID * @autoWired (required=true) means that a corresponding bean object injection value must be found. If not found, an error is reported. * If required=false means bean object injection cannot be found, Qualifier("bookDaoExt2") @AutoWired (Required =false) private BookDao bookDao2;Copy the code

8.7. Use of @autowired and @Qualifier on methods. Experiment 38: Use the @qualifier annotation in the method parameter position

After the bean object is created, the @autowired method is automatically called <br/> * and the @autowired method is automatically looked up by type in the Spring IOC container and injected with <br/> * if looked up by type, multiple are found. Just take the parameter name as id and look it up and inject it. */ @autoWired public void ABC (@qualifier ("bookDao") bookDao bookext2) {system.out.println ("... bookDao ==>> " + bookDaoExt2); }Copy the code

8.8. Generic Injection (Understanding)

Experiment 40: Testing generic dependency injection ★



9. Spring’s proprietary testing (learn about it)

@ContextConfiguration

@RunWith

/** * How to use Spring to integrate Junit tests (Spring adds the container to the test context) ** // tell the test, Where is the Spring container configuration file @ ContextConfiguration (locations = "classpath: applicationContext. XML") @ RunWith (SpringJUnit4ClassRunner. Class) / / said the use of integration of the Spring IOC container test class to test the public class SpringTest2 {@autowired BookService bookService; @Autowired UserService userService; @Test public void test1() throws Exception { bookService.saveEntity(new Book()); System.out.println("========================="); userService.saveEntity(new User()); }}Copy the code