Spring IoC

IoC container-bean management (annotation-based)

1. What are annotations

  • An annotation is a special tag for code in the form of @ annotation name (attribute name = attribute value, attribute name = attribute value)
  • Use annotations, which apply to classes, methods, and properties
  • The purpose of using annotations is to simplify XMl configuration

2. Spring provides annotations for creating objects in Bean management

  • @Component

  • @Service

  • @Controller

  • @Repository

    These annotations are all the same and can be used to create bean instances

3. Object creation based on annotations

  1. Introducing dependencies: The SPRing-AOP JAR package

  2. Enable component scanning:

    1. If multiple packages are scanned, the packages are separated by commas
    2. Scan the upper-layer directory of packets
    <context:component-scan base-package="com.atguigu"></context:component-scan>
    Copy the code
  3. Create a class and add create object annotations to the class

    • In annotations, the value attribute value can be omitted, which is the default
    • The default value is the class name and starts with a lowercase letter
    // Annotations are equivalent to XML configuration files: 
            
    // UserService -- userService
    @Component(value = "userService")
    public class UserService {
        public void add(a) {
            System.out.println("service add......"); }}Copy the code

4. Enable component scanning details

<! -- 1. Use-default-filters ="false" -- filter context:include-filter -->
<context:component-scan base-package="com.atguigu" use-defaultfilters="false">
    <! -- represents classes that scan only Controller annotations -->
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

<! Context :exclude-filter: set the contents not to be scanned -->
<context:component-scan base-package="com.atguigu">
    <! -- Everything outside the class representing the Controller annotation is scanned -->
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
Copy the code

5. Implement attribute injection based on annotation

  1. Autowired: Automatic assembly based on attribute type

    1. The first step is to create the Service and DAO objects and add create object annotations to the Service and DAO classes
    2. The second step is to inject dao objects into service, add DAO type attributes to the Service class, and use annotations on the attributes
    @Service
    public class UserService {
        
        // Define DAO type attributes
        // There is no need to add the set method
        // Add an injection attribute annotation
        @Autowired
        private UserDao userDao;
        
        public void add(a) {
            System.out.println("service add......."); userDao.add(); }}/ / the Dao implementation class
    @Repository
    //@Repository(value = "userDaoImpl1")
    public class UserDaoImpl implements UserDao {
        @Override
        public void add(a) {
            System.out.println("dao add....."); }}Copy the code
  2. Qualifier: Inject by name

    • The use of the @Qualifier annotation is used in conjunction with the @AutoWired annotation above
    // Define DAO type attributes
    // There is no need to add the set method
    // Add an injection attribute annotation
    
    // Inject by type
    @Autowired
    // Inject by name (the purpose is to distinguish between the same interface has multiple implementation classes, according to the type can not select, so error!)
    @Qualifier(value = "userDaoImpl1") 
    private UserDao userDao;
    Copy the code
  3. @resource: Can be injected by type or by name (but this annotation belongs to the Javax package and is not recommended!) 六四屠杀

    // @resource is injected by type
    @Resource(name = "userDaoImpl1") // Inject by name
    private UserDao userDao;
    Copy the code
  4. @value: Inject a normal type attribute

    @Value(value = "abc")
    private String name;
    Copy the code

6. Fully annotated development

  1. Create a configuration class instead of an XML configuration file

    // As a configuration class instead of an XML configuration file
    @Configuration
    @ComponentScan(basePackages = {"com.atguigu"})
    public class SpringConfig {
        xxx
    }
    Copy the code
  2. Writing test classes

    @Testpublic void testService2(a) {    / / load the configuration class ApplicationContext context = new AnnotationConfigApplicationContext (SpringConfig. Class); UserService userService = context.getBean("userService", UserService.class); System.out.println(userService); userService.add(); }
    Copy the code