Bean spring assembly embodiment of the application of (VI)

The first question: For ComponentScan notes, it just scans java category, but more often the real need is to scan the specified class

@ComponentScan there are arranged two items, one basePackages, the other is basePackageClasses.

image

* BasePackages java package may be configured in an array, Spring configuration based on its corresponding package and scans the sub-packet, configured Bean, assembled;

* BasePackageClasses can configure a plurality of classes, Spring based configuration where the bags, Bean scanning assembly for the package and a corresponding sub-packet.

To verify the CI @ComponentScan two, first define an interface IStaffService,

package pro.jamal.blog.demo6.service;

import pro.jamal.blog.demo5.annotation.Staff;

/**
 * @author: lyj
 * @Date: 2019/6/8
 */
public interface IStaffService {
    public void printStaffInfo(Staff staff);
}

Use the interface to write some operations Spring class is recommended, he can define and achieve phase separation, so more flexible. Add a IStaffService implementation class here.

package pro.jamal.blog.demo6.service.impl;

import org.springframework.stereotype.Component;
import pro.jamal.blog.demo5.annotation.Staff;

/**
 * @author: lyj
 * @Date: 2019/6/8
 */
@Component
public class StaffServiceImpl implements  IStaffService {
    @Override
    public void printStaffInfo(Staff staff) {
        System.out.println("[id="+staff.getId()+"]");
        System.out.println("[name="+staff.getName()+"]");
        System.out.println("[salary="+staff.getSalary()+"]");
    }
}

Herein indicates that it @Compent Bean Spring required, and to achieve a corresponding method IStaffService printStaffInfo defined interface.

To assemble the two Bean StaffService and Staff, @ComponentScan need to add the corresponding configuration,

Package pro.jamal.blog.demo6; Import org.springframework.context.annotation.ComponentScan;
 Import pro.jamal.blog.demo5.annotation.Staff;
 Import pro.jamal.blog.demo6.service.impl.StaffServiceImpl; / * * 
 * @author: LYJ 
 * @date: 2019/6/8 
 * / // here scans two package and its subpackages // @ ComponentScan (basePackages = { " pro.jamal.blog.demo5.annotation", " pro.jamal.blog.demo6 "}) // where two scans where the class package and its subpackages 
@ComponentScan (basePackageClasses = {Staff. class , StaffServiceImpl. class })
 public class ApplicationConfig { 
}






 

 

* If @ComponentScan to define a plurality of corresponding packet, but each set a @ ComponentScan, Spring will generate a new object class is defined, which is formulated Bean will generate a plurality of instances, this is often not we need.

* For defined basePackages and basePackageClasses @ ComponentScan's, Spring will be a special distinction, that is to say in the same @ComponentScan even if repeating the same custom package or sub-package defines its existence, it will not cause many times the same Bean scan, resulting in a configuration to generate a plurality of objects. Therefore not recommended to configure multiple @ComponentScan notes, because there must be a repeat of the package and the child package will produce duplicate objects, this is not what we need. For the selection of basePackages and basePackageClasses, basePackage readability would be better, so the project will give priority to use it, but in a large number of reconstruction projects, try not to use basePackages, because very often reconstructed modify the package name It requires repeated configuration, and IDE will not give you any hints. The use of basePackagesClasses, when you move the package, IDE will prompt an error, and can easily handle these errors.

test

package pro.jamal.blog.demo6;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import pro.jamal.blog.demo5.annotation.Staff;
import pro.jamal.blog.demo6.service.impl.StaffServiceImpl;

/**
 * @author: lyj
 * @Date: 2019/6/8
 */
public class Client {
    public static void main(String[] args) {
        ApplicationContext context =
                new AnnotationConfigApplicationContext(ApplicationConfig.class);
        Staff staff = context.getBean(Staff.class);
        StaffServiceImpl staffService = context.getBean(StaffServiceImpl.class);
        staffService.printStaffInfo(staff);

    }
}

Guess you like

Origin www.cnblogs.com/jamal/p/10991849.html