目录
1、ClassPathXmlApplicationContext
2、FileSystemXmlApplicationContext
6.3、AnnotationConfigApplicationContext
一、常用注解
1、ComponentScan
ComponentScan主要就是定义扫描的路径,从中找出标识了需要装配的类自动装配到spring的bean容器中
2、Autowired和Resource
区别:
1.2.1、@Autowired
由Spring提供,只按照byType注入,可以配合@Qualifier一起使用
1.2.2、@Resource
由J2EE提供,默认按照byName自动注入
@Resource有两个重要的属性:name和type
1.2.3、当一个service有多个实现类时
1)、方式一
public interface UserService {
void show();
}
@Slf4j
@Service
public class UserServiceImpl implements UserService {
@Override
public void show() {
log.info("=======UserService-----UserServiceImpl=======");
}
}
@Slf4j
@Service
public class UserServiceEs implements UserService {
@Override
public void show() {
log.info("=======UserService-----UserServiceEs=======");
}
}
@RestController
public class TestController {
@Autowired
@Qualifier("userServiceImpl")
private UserService userService;
//通过type
@Resource(type = UserServiceEs.class)
private UserService userServiceEs;
@GetMapping("/hello")
public String test() {
userService.show();
userServiceEs.show();
return "hello ma!!!";
}
}
执行结果
=======UserService-----UserServiceImpl=======
=======UserService-----UserServiceEs=======
2)、方式二
public interface UserService {
void show();
}
@Slf4j
@Service("userServiceImpl")
public class UserServiceImpl implements UserService {
@Override
public void show() {
log.info("=======UserService-----UserServiceImpl=======");
}
}
@Slf4j
@Service("userServiceEs")
public class UserServiceEs implements UserService {
@Override
public void show() {
log.info("=======UserService-----UserServiceEs=======");
}
}
@RestController
public class TestController {
@Autowired
@Qualifier("userServiceImpl")
private UserService userService;
//通过名称
@Resource(name="userServiceEs")
private UserService userServiceES;
@GetMapping("/hello")
public String test() {
userService.show();
userServiceES.show();
return "hello ma!!!";
}
}
执行结果
=======UserService-----UserServiceImpl=======
=======UserService-----UserServiceEs=======
3、@configuration和@component
区别:
@Component注解的范围最广,所有类都可以注解,但是@Configuration注解一般注解在这样的类上:这个类里面有@Value注解的成员变量和@Bean注解的方法,就是一个配置类。
@Configuration
中所有带 @Bean
注解的方法都会被动态代理,因此调用该方法返回的都是同一个实例
@Data
@Service
public class Org {
}
@Data
public class Unit {
private Org org;
public Unit(Org org) {
this.org = org;
}
}
@Configuration
public class MyBeanConfig {
@Bean
public Org orgBean() {
return new Org();
}
@Bean
public Unit unitBean() {
return new Unit(orgBean());
}
}
@SpringBootTest
class DemoApplicationTests {
@Test
void test() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyBeanConfig.class);
Org orgBean = (Org) applicationContext.getBean("orgBean");
Unit unitBean = (Unit) applicationContext.getBean("unitBean");
System.out.println(orgBean==unitBean.getOrg());
}
}
执行结果
true
@Data
@Service
public class Org {
}
@Data
public class Unit {
private Org org;
public Unit(Org org) {
this.org = org;
}
}
@Component
public class MyBeanConfig {
@Bean
public Org orgBean() {
return new Org();
}
@Bean
public Unit unitBean() {
return new Unit(orgBean());
}
}
@SpringBootTest
class DemoApplicationTests {
@Test
void test() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MyBeanConfig.class);
Org orgBean = (Org) applicationContext.getBean("orgBean");
Unit unitBean = (Unit) applicationContext.getBean("unitBean");
System.out.println(orgBean==unitBean.getOrg());
}
}
执行结果
false
4、Scope
基本作用域(singleton,prototype),web作用域(request,session,globalsession)
@Scope(value=ConfigurableBeanFactory.SCOPE_PROTOTYPE)这个是说在每次注入的时候回自动创建一个新的bean实例
@Scope(value=ConfigurableBeanFactory.SCOPE_SINGLETON)单例模式,在整个应用中只能创建一个实例
@Scope(value=WebApplicationContext.SCOPE_GLOBAL_SESSION)全局session中的一般不常用
@Scope(value=WebApplicationContext.SCOPE_APPLICATION)在一个web应用中只创建一个实例
@Scope(value=WebApplicationContext.SCOPE_REQUEST)在一个请求中创建一个实例
@Scope(value=WebApplicationContext.SCOPE_SESSION)每次创建一个会话中创建一个实例
二、ApplicationContext的三个常用实现类
1、ClassPathXmlApplicationContext
可以加载类路径下的配置文件,不在的话加载不了
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
Hello hello = (Hello) act.getBean("hello");
hello.show();
2、FileSystemXmlApplicationContext
可以加载磁盘任意路径下的配置文件(必须有访问权限)
ApplicationContext act = new FileSystemXmlApplicationContext("D:/javaWorkSpace/spring2/src/applicationContext.xml");
person persons = (person) act.getBean("persons");
persons.show();
6.3、AnnotationConfigApplicationContext
用于读取注解创建容器的
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(DemoApplication.class);
UserService bean = applicationContext.getBean(UserService.class);
bean.show();