在Spring中,不需要自己创建对象,只需要告诉Spring,哪些类需要创建出对象;在项目启动的时候Spring就会自动创建出该对象。
注入简介
在Spring中通过注解即可方便地实现注入:
- 在定义类时使用‘注册注解’(@Service等),标记类需要注册生成Bean;
- 需要地方,通过‘使用注解’(@Autowired等)声明,来引入Bean。
注解分类
注解分为使用Bean的注解与注册Bean的注解。
使用Bean的注解,即完成属性方法的封装(一般用于修饰‘字段’、‘构造方法’、‘设置方法’),生成要使用的Bean:
- @Autowired:byType自动注入;自动从spring的上下文找到合适的Bean来注入(默认按类型匹配注入Bean,类型可以是接口);
@Autowired
private UserDao userDao;
- @Resource:byName自动注入;通过name属性指定(若不指定,自动采用标注处的变量名和方法名作为Bean的名称)名称:
@Resource(name = "userServiceImpl")
private IUserService userService;
- @Qualifier:指示要使用哪个类实现(可与@Autowired注入接口时,指定具体实现类)
@Autowired
@Qualifier("userServiceImpl")
private IUserService userService;
注册Bean,一般修饰类,在Spring扫描时实例化对象Bean并放在IoC容器中:
- @Service,@Controller,@Repository分别标记Service层,Controller层,Dao层的类,spring扫描注解配置时,会标记这些类要生成Bean。
- @Component是一种泛指,标记类是组件,spring扫描注解配置时,会标记这些类要生成Bean。
- @Configration:定义配置类(可自动对应配置文件中相应字段);
spring:
# DataSource Config
datasource:
driver-class-name: org.h2.Driver
schema: classpath:db/schema-h2.sql
url: jdbc:h2:file:./dbAccount
username: root
@Data
@Configuration
@ConfigurationProperties("spring.datasource")
public class DbSourceConfig {
private String driverClassName;
private String schema;
private String username;
}
配置文件中的中划线,对应到类变量时改为驼峰命名即可。
Bean扫描规则
默认情况下,从**<Application启动类>所在包
**的位置,从上往下扫描;若需要扫描的类不在启动类所在的包
或启动类所在包的子包
,则需要在启动类中配置@ComponentScan
注解来添加这些类的包。
package com.example.study;
@SpringBootApplication
@ComponentScan(basePackages = {"com.other.toScan"})
public class StudyApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(StudyApplication.class, args);
}
}
所有com.example.study
包与其子包中的类会被自动扫描,其他类则需要通过@ComponentScan
来添加。
条件注入
条件注解可根据不同的条件做不同的事情。
Condtion接口
所有条件注解,都是通过扩展Condtion接口实现的:
@FunctionalInterface
public interface Condition {
boolean matches(ConditionContext var1, AnnotatedTypeMetadata var2);
}
@Conditional注解,就是利用Condition接口判断条件是否满足,来决定是否初始化并向容器注入Bean。
常见条件注解
Spring中提供了一系列相关注解:
- @ConditionalOnMissingBean:当指定Bean不存在时,才会创建当前类的Bean。
- @ConditionalOnBean:当指定Bean存在时(@ConditionalOnBean(name=“dependedBean”)),才会创建当前类的Bean。
@Component
@ConditionalOnBean(name="dependedBean")
public class ToCreateBean {
// ...
}
- @ConditionalOnMissingClass:当指定Class不存在时,才会创建当前类的Bean。
- @ConditionalOnClass:当指定Class存在时(@ConditionalOnClass(name=“existedBean”)),才会创建当前类的Bean。
@Component
@ConditionalOnClass(DependedClass.class)
public class ToCreateBean {
// ...
}
- @ConditionalOnExpression:根据spel(Spring Expression Language,即Spring表达式语言)表达式执行结果确定是否创建Bean(为true时创建);
- @ConditionalOnProperty:根据配置参数,来决定是否需要创建这个Bean;
@ConditionalOnProperty
根据配置参数,来决定是否要创建Bean:
//yml
stored:
inRedis: false
@Service
@ConditionalOnProperty(name = "stored.inRedis", havingValue = "false")
public class StoreAcc_LocalDB implements IStoreHandler
{}
@Service
@ConditionalOnProperty(name = "stored.inRedis", havingValue = "true")
public class StoreAcc_Redis implements IStoreHandler
{}
// 使用
@Autowired
IStoreHandler handler;
版权声明:本文为alwaysrun原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。