Spring-Anotion-(Spring configures beans based on annotations)

1. Basic introduction and introductory cases of Spring’s annotation-based bean configuration

1. Basic introduction:

Configure beans based on annotations, mainly components in project development, such as Action, Service, and Dao.

2. Commonly used component annotation forms are:

@Component indicates that the current annotation identifies a component.

@Controller indicates that the current annotation identifies a controller, usually used in Action classes.

@Service indicates that the current annotation identifies a class that handles business logic, usually used in the Service class.

@Repository indicates that the current annotation identifies a persistence layer class, usually used for Dao classes.

3. Example:

@Component //The ioc container scans this annotation and constructs the Action into a bean.

public void Action() {

//………………………..

}

Note: 1> Once you write annotations, you don’t need to write .. repeatedly in bean.xml.

2>Annotations can be seen as automatically configuring beans instead of manually configuring them in the ioc bean configuration file. This is another system for ioc to create beans. Compared with the previous manual configuration of beans in spring, these two systems can manage beans independently or complement each other.

3>The four annotations @Component, @Controller, @Service, and @Repository have the same meaning for the IOC container. The IOC container will scan the class with a certain annotation and build a bean. These four annotations are just for the convenience of programmers to distinguish which one of Action, Service, and Dao a certain class is.

4. Spring-aop.RELEASE.jar needs to be introduced

5. Usage cases

1>Create a class and write comments

import org.springframework.stereotype.Component;

@Component
public class MyComponent {
}

2. Configure the package automatically scanned by spring in the ioc container bean.xml. Do not write the package name incorrectly!

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util.xsd
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">


<!-- Open ioc annotation-based configuration -->
<!-- context: component-scan: indicates enabling the ioc annotation-based function -->
<!-- base-package:com.itbull.ioc.component,-->
<!-- Indicates that we scan the annotated classes of the com.itbul1.ioc.comgonent package.-->

<!-- 1. If we want to be able to scan all sub-packages of the com.itbull.ioc.component package, -->
<!-- The standard writing method is com.itbul1.ioc.component.*-->

    <context:component-scan base-package="com.spring.component"/>


</beans>

3. In the test method, getBean(). Use MyComponent.class, and of course you can also give it an alias, which will be written later.

 @Test
    public void test04() {
        MyComponent myComponent = (MyComponent) applicationContext.getBean(MyComponent.class);
        System.out.println(myComponent.getClass());
    }

4. Got the bean

6. Summary of automatic package scanning

Details:
1. The “automatically scanned package” must be specified in the Spring configuration file so that the IOC container can detect which classes in the current project are annotated. Note that the context namespace is imported.

<-Configure the package for automatic scanning->You can use the wildcard * method to specify, such as com.itbull.ioc.* to indicate more clear

2. Spring’s IOC container cannot detect whether a class annotated with @Controller is a real controller. The name of the annotation is used by programmers to identify what component is currently identified. The same is true for other @Service@Repository [that is to say, spring’s IOC container will generate objects as long as it checks the annotation, but the meaning of this annotation will not be recognized by spring. The annotation is for the convenience of programmers]

3.resource-pattern=”User*.class” means to scan only those that meet the conditions.

resource-pattern=”User*.class”/> // Indicates that only the package will be scanned Classes whose names begin with User

2. Configure beans based on annotations – obtain by id

illustrate:

1. Default: After marking the annotation, the first letter of the class name in lowercase is used as the value of the id.

2. You can use the value attribute of the annotation to specify the id value, and the value can be omitted. (equivalent to bean id)

@Controller(value=”userAction1″)

@Controller(“userAction1”)

 @Test
    public void test04() {
        MyComponent myComponent = (MyComponent) applicationContext.getBean("myent");
        System.out.println(myComponent.getClass());
    }
import org.springframework.stereotype.Component;

@Component(value = "myent")
public class MyComponent {
}

3. exclude-filter tag

Note: The exclude-filter tag is inserted into to indicate that certain annotated classes are excluded when generating beans.

Example:



1. Configure the exclusion filter in beans.xml to exclude @Controler classes from scanning.

<!--How to exclude which annotation classes you do not want to scan, such as excluding the @Controller class under com.spring.component-->
<!-- 1.context:exclude-filter: Indicates which categories are excluded from scanning -->
<!-- 2.type="annotation": Indicates exclusion based on annotation -->
<!-- 3.expression="org.springframework.stereotype.Controller"The full class name of the annotation to be excluded-->
    <context:component-scan base-package="com.spring.component">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

2. Write comments, one with @Controler and one with @Repository

package com.spring.component;


import org.springframework.stereotype.Controller;

@Controller(value = "myent")
public class MyComponent {
}
package com.spring.component;

import org.springframework.stereotype.Repository;

@Repository(value = "baseDao")
public class BaseDao {
}
@Test
    public void test04() {
        BaseDao baseDao = (BaseDao) applicationContext.getBean("baseDao");
        System.out.println(baseDao.getClass());

        MyComponent myComponent = (MyComponent) applicationContext.getBean("myent");
        System.out.println(myComponent.getClass());
    }

4. You can see that @Repository has been built with beans, and @Controler has been excluded.

detail:



The type parameter has five types, two of which are commonly used. assignable means filtering out the subclasses of BaseXxx.

4. include-filter tag

The include tag indicates that only the expression of the specified annotation will be scanned.

 <!--annotation-config="false": The default scanning mechanism context is no longer used:
    include-filter: indicates that only the class expression of the specified annotation is scanned
    ="org.springframework.stereotype.Component": The full class name of the annotation -->
    <context:component-scan base-package="com.spring.component" use-default-filters="false">
        <context:include-filter type="annotation"
                                expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>
package com.spring.component;


import org.springframework.stereotype.Controller;

@Controller(value = "myent")
public class MyComponent {
}
package com.spring.component;

import org.springframework.stereotype.Repository;

@Repository(value = "baseDao")
public class BaseDao {
}
 @Test
    public void test04() {
        BaseDao baseDao = (BaseDao) applicationContext.getBean("baseDao");
        System.out.println(baseDao.getClass());

        MyComponent myComponent = (MyComponent) applicationContext.getBean("myent");
        System.out.println(myComponent.getClass());
    }

It can be seen that the BaseDao annotated with @Respsitory was scanned and the bean was built, but @Controller was not scanned.

5. Annotation-based automatic assembly

1. If there are dependencies between beans, how to use annotations to implement automatic assembly (that is, how does the IOC container know the relationship between beans)?

Use @Autowired

Note the position of @Autowired

package com.spring.component;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyService {

    private MyDao myDao;

    public MyService() {
    }

    public MyService(MyDao myDao) {
        this.myDao = myDao;
    }

    public MyDao getMyDao() {
        return myDao;
    }

    public void setMyDao(MyDao myDao) {
        this.myDao = myDao;
    }

    public void saveDao() {
        System.out.println("saved an object");
    }
}

6. Spring’s bean configuration is based on generic dependencies

How to use annotations to handle dependencies when there are generics and dependencies

1. Create the following categories

2,

package com.spring.myBean;

public class Book {
}
package com.spring.repository;

public abstract class BaseDao<T> {
    public abstract void save();
}
import com.spring.myBean.Book;
import org.springframework.stereotype.Repository;

@Repository
public class BookDao extends BaseDao<Book>{
    @Override
    public void save() {
        System.out.println("BookDao save");
    }
}
package com.spring.service;

import com.spring.repository.BaseDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


public class BaseServic<T> {
@Autowired
    private BaseDao<T> baseDao;
    public void save () {
        baseDao.save();
    }

}
package com.spring.service;

import com.spring.myBean.Book;
import org.springframework.stereotype.Service;

@Service
public class BookService extends BaseServic<Book>{

}
@Test
    public void test05() {
        BookService bookService = (BookService) applicationContext.getBean(BookService.class);
        bookService.save();

    }

3. There are five classes in total. The save method of BookDao is directly called from the BookService object, which includes inheritance and generic relationships.

4. Abbreviation of code execution process:

First, BookService calls the save method, and then the jvm first finds the BookService object, finds that it inherits BaseService, and then finds that BaseService automatically assembles BaseDao. It then finds BaseDao with the generic parameter Book, but finds that BaseDao is an abstract class, so again I went to find BookDao that implemented this abstract class, and finally found the save() method. I found that I needed to construct a BookDao object, and found that BookDao had already been constructed, so I called the save() method directly.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 139421 people are learning the system