前提:对于bean的作用范围以及生命周期,用注解配置和用xml配置并没有区别,详情请见: https://blog.csdn.net/Sunny5319/article/details/91493016
常用注解
用于创建对象的
他们的作用就和在xml配置文件中编写一个<bean>标签实现的功能是一样的
@Component
- 作用:用于把当前类对象存入spring容器中
- 属性:
- value:用于指定bean的id。当我们不写时,它的默认是当前类名,且首字母小写
@Controller 一般用于表现层
@Service 一般用于业务层
@Repository 一般用于持久层
以上三个注解他们的作用和属性与component是一模一样的,他们三个是spring框架为我们提供明确的三层使用的注解,使我们的三层对象更加清晰
用于注入数据的
他们的作用就和在xml配置文件中的bean标签中写一个<property>标签的作用是一样的
@Autowired
- 作用:
- 自动按照类型注入,只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功
- 如果IOC容器中没有任何bean的类型和要注入的变量类型匹配,则报错
- 如果IOC容器中有多个类型匹配时,使用要注入的对象变量名称作为 bean 的 id,在 spring 容器查找,找到了也可以注入成功。找不到就报错。
- 出现位置:
可以是变量上,也可以是方法上
自动按照类型注入解释如图:
@Qualifier
- 作用:在按照类中注入的基础之上再按照名称注入,它在给类成员注入时不能单独使用,但是在给方法参数注入时可以
- 属性:
- value:用于指定注入bean的id
@Resource
- 作用:直接按照bean的id注入,它可以单独使用
- 属性:
- name:用于指定bean的id
以上三个注解只能注入其他bean类型的数据,而基本类型和string类型无法使用上述注解实现
- name:用于指定bean的id
@value
- 作用:用于注入基本类型和string类型的数据
- 属性:
- value:用于指定数据的值,它可以使用spring中的spel(也就是spring的el表达式)
Spel的写法:${表达式}
- value:用于指定数据的值,它可以使用spring中的spel(也就是spring的el表达式)
用于改变作用范围的
他们的作用就和在bean标签中使用scope属性实现的功能是一样的
@ scope
- 作用:用于指定bean的作用范围
- 属性:value:指定范围的取值,常用取值:singleton prototype
和生命周期相关
他们的作用就和在bean标签中使用init-method和destory-method的作用是一样的
@PreDestory
- 作用:用于指定销毁方法
@PostConstruct
- 作用:用于指定初始化方法
新注解
@Configuration
- 作用:完全去掉XML文件时,需要使用config 的java类来代替XML里面的配置,此注解就代表当前类为config配置类
@componentScan
- 作用:用于通过注解指定spring在创建容器时要扫描的包
- 属性:
- value:它和basePackages的作用是一样的,都是用于指定创建容器时要扫描的包
- 我们使用此注解就等同于在xml中配置了:
<context:component-scan base-package="com.itheima"></context:component-scan>
@PropertySource
- 作用:用于指定properties文件的位置
- 属性:
- value:指定文件的名称和路径
- 关键字:classpath,表示类路径下(打包之后在classes文件夹下)
@import
- 作用:用于导入其他的配置类
- 属性:
- value:用于指定其他配置类的字节码
- 当我们使用import注解之后,有import注解的类就是父配置类,而导入的都是子配置类
demo
用测试类调用service即可
AccountServiceImpl
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao accountDao;
@Override
public List<Account> findAllAccount() {
return accountDao.findAllAccount();
}
}
AccountDaoImpl
@Repository("accountDao")
public class AccountDaoImpl implements IAccountDao {
@Autowired
private QueryRunner runner;
@Override
public List<Account> findAllAccount() {
try {
return runner.query("select * from account", new BeanListHandler<Account>(Account.class));
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
SpringConfiguration
@Configuration
@ComponentScan("com.itheima")
@Import(JdbcConfig.class)
@PropertySource("classpath:jdbcConfig.properties")
public class SpringConfiguration {
}
JdbcConfig
//@Configuration
public class JdbcConfig {
//使用properties文件的内容
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean(name = "runner")
@Scope("prototype")
public QueryRunner createQueryRunner(DataSource dataSource) {
return new QueryRunner(dataSource);
}
@Bean(name = "dataSource")
public DataSource createDataSource() {
try {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setDriverClass(driver);
ds.setJdbcUrl(url);
ds.setUser(username);
ds.setPassword(password);
return ds;
} catch (PropertyVetoException e) {
throw new RuntimeException();
}
}
}
jdbcConfig.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=*******
XML 对比注解
- 注解的优势:配置简单
- XML的优势:修改时不用修改源码,不需要编译和部署