IOC operation Bean management (based on annotations)

 IOC inversion of control, handing over the object creation process to Spring for management.

Table of contents

what is annotation

Bean management annotation method (create object)

Annotation-based object creation

Step 1: Introduce dependencies 

Step 2: Turn on component scanning

Create a class and add object creation annotations on the class (the following three are all available)

Bean management annotation method (component scanning details configuration)

Annotation-based property injection (injection property @)

@AutoWired : Autowire according to attribute type;

@Qualifer : Injection based on property name

@Resource : can be injected according to type, can be injected according to name

@Value : Inject normal type properties

 Fully Annotated Development


 Bean management

 There are two ways of bean management operations

         The first method: based on the xml configuration file

         The second method: implementation based on annotations

 Based on the xml configuration file method, you can see the following article

The underlying principle of IOC and Bean management XML method, xml injection collection attributes

Bean management (factory beans)

ICO operation Bean management (bean scope and lifecycle)

Bean management XML method (autowiring and external property files)

Not much to say, officially enter the annotation method of Bean management

what is annotation

  • Annotation is a special code mark, format: @ annotation name (attribute name = attribute value, attribute name = attribute value)
  • Using annotations, annotations act on classes, methods, and attributes
  • Use annotations to simplify xml configuration

Bean management annotation method (create object)

Spring provides annotations for creating objects in Bean management

@Component   @Service  @Controller  @Repository

The four annotations have the same function and can be used to create bean instances. Most of the time, they are written in different layers, mainly to better understand the business logic of different layers.

Annotation-based object creation

Step 1: Introduce dependencies 

Step 2: Turn on component scanning

The popular way is to tell spring5 that a class needs to be scanned.

If scanning multiple packages, use commas to separate multiple packages;
 <context:component-scan base-package="com.atguigu.spring5.dao,com.atguigu.spring5.service"></context:component-scan>
If you are scanning all packages in the same directory, directly write the upper directory in the path
 <context:component-scan base-package="com.atguigu"></context:component-scan>

Create a class and add object creation annotations on the class (the following three are all available)

@Component   @Service  @Controller  @Repository

The value attribute value in the annotation can be omitted. If it is omitted, its default value is the class name, but the first letter will be lowercase.

@Component(value = "userService") is equivalent to the path bean id="userService" class="..." when creating a service

@Component(value = "userService")
public class UserService {
    public void add(){
        System.out.println("service add...");
    }
}

test

  @Test
    public void testService (){
      ApplicationContext context =
              new ClassPathXmlApplicationContext("bean1.xml");
      UserService userService = context.getBean("userService",UserService.class);
      System.out.println(userService);
      userService.add();
    }

Bean management annotation method (component scanning details configuration)

use-default-filters="false" : Indicates that the default Filter is not used now, and you need to configure it yourself
context:include-filter: Indicates what content to scan
     <context:component-scan base-package="com.atguigu" use-default-filters="false">
         <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
     </context:component-scan>
context:exclude-filter: set which content is not to be scanned; 
base-package="com.atguigu" without use-default-filters="false" means to scan all, but context:exclude-filter: you can set which content is not to be scanned. The following shows the cloth scanning Component
    <context:component-scan base-package="com.atguigu">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Component"/>
    </context:component-scan>

Annotation-based property injection (injection property @)

@AutoWired : Autowire according to attribute type;

Step 1: Create service and dao objects, and add creation object annotations to service and dao classes;

Step 2: Inject the dao object in the service; add the dao type attribute in the service class, and use annotations on the attribute

@Service
public class UserService {

    //定义dao类型属性
    //不需要添加set方法
    //添加注入属性注解
    @Autowired
    private UserDao userDao;

    public void add(){
        System.out.println("service add...");
        userDao.add();
    }
}

@Qualifer : Injection based on property name

The use of this @Qualifer annotation is used together with @AutoWired above

@Service
public class UserService {

    //定义dao类型属性
    //不需要添加set方法
    //添加注入属性注解
    @Autowired //根据类型属性注入
    @Qualifier(value = "userDaoImpI")//根据名称注入
    private UserDao userDao;

    public void add(){
        System.out.println("service add...");
        userDao.add();
    }
}

@Resource : can be injected according to type, can be injected according to name

Inject by type

    @Resource
    private UserDao userDao;

    public void add(){
        System.out.println("service add...");
        userDao.add();
    }

Inject by name

    @Resource(name = "userDaoImpI")
    private UserDao userDao;

    public void add(){
        System.out.println("service add...");
        userDao.add();
    }

@Value : Inject normal type properties

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

 Fully Annotated Development

Create a configuration class to replace the xml configuration file

@Configuration  //作为配置类,替代xml 配置文件
@ComponentScan(basePackages = {"com.atguigu"})
public class SpringConfig {
}
@ComponentScan(basePackages = {"com.atguigu"}) of the above code is equivalent to <context:component-scan base-package="com.atguigu"></context:component-scan> of the xml file

write test class

  @Test
  public void testService2 (){
    //加载配置类
    ApplicationContext context =
            new ClassPathXmlApplicationContext("bean1.xml");
    UserService userService = context.getBean("userService",UserService.class);
    System.out.println(userService);
    userService.add();
  }

Guess you like

Origin blog.csdn.net/m0_57448314/article/details/128142464