1 目录结构
SpringConfiguration.java
package class04.config;
import class04.service.UserService;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* @author:
* @date:2022/3/9
* @description:
*/
@Configuration
@ComponentScan(basePackageClasses = UserService.class)
public class SpringConfiguration {
// 1.默认value/basePackageClasses 的情况下 被 @ComponentScan 修饰的类,它所处的包下的所有内容都会被扫描
// 2.如果@ComponentScan指定了扫描的路径比如:“@ComponentScan(basePackageClasses = UserService.class)” 他就会扫描UserService.java所在的包下的文件,就不会再去扫描默认的被这个注解标注的类文件所处包下的所有文件。
}
1.默认value/basePackageClasses 的情况下 被 @ComponentScan 修饰的类,它所处的包下的所有内容都会被扫描
2.如果@ComponentScan指定了扫描的路径比如:“@ComponentScan(basePackageClasses = UserService.class)” 他就会扫描UserService.java所在的包下的文件,就不会再去扫描默认的被这个注解标注的类文件所处包下的所有文件。
AccountServiceImp1.java
package class04.service.AccountServiceImp;
import class04.service.AccountService;
import org.springframework.stereotype.Service;
/**
* @author:
* @date:2022/3/9
* @description:
*/
//@Service
@Service("accountServiceImp1")
public class AccountServiceImp1 implements AccountService {
@Override
public void deleteAccount() {
System.out.println("删除用户1");
}
}
UserServiceImp1.java
package class04.service.UserServiceImp;
import class04.service.UserService;
import org.springframework.stereotype.Service;
/**
* @author:
* @date:2022/3/9
* @description:
*/
@Service(value="userServiceImp1")
public class UserServiceImp1 implements UserService {
@Override
public void saveUser() {
System.out.println("保存用户1");
}
}
UserServiceImp2.java
package class04.service.UserServiceImp;
import class04.service.UserService;
import org.springframework.stereotype.Service;
/**
* @author:
* @date:2022/3/9
* @description:
*/
@Service(value="userServiceImp2")
public class UserServiceImp2 implements UserService {
@Override
public void saveUser() {
System.out.println("保存用户2");
}
}
AccountService.java
package class04.service;
public interface AccountService {
public abstract void deleteAccount();
}
UserService.java
package class04.service;
public interface UserService {
public abstract void saveUser();
}
SpringComponentScanTest.java
import class04.config.SpringConfiguration;
import class04.service.AccountService;
import class04.service.UserService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author:
* @date:2022/3/9
* @description:
*/
public class SpringComponentScanTest {
public static void main(String[] args) {
//1.创建容器,基于注解创建方式。
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext("class04/config");
// 2.获取对象
UserService userService = ac.getBean("userServiceImp1", UserService.class);
// 3.调用方法
userService.saveUser();
// 1.创建容器 传入被注解的类的字节码的方式
AnnotationConfigApplicationContext ac2 = new AnnotationConfigApplicationContext(SpringConfiguration.class);
// 2.获取对象
AccountService accountService = ac2.getBean("accountServiceImp1", AccountService.class);
// 3.调用方法
accountService.deleteAccount();
}
}