Spring 第二天
Spring 基于注解的 IoC 以及 IoC 案例
-
Spring 中 IoC 的常用注解
-
案例使用 xml 方式和注解方式实现单表的 CRUD 操作
持久层技术选择:dbutils
-
改造基于注解的 IoC 案例,使用纯注解的方式实现 Spring 的一些新注解的使用
-
Spring 和 Junit 整合
基于注解的 IoC 配置
注解和 xml 两种方式都要掌握,因为不同的公司有不同的习惯
常用 IoC 注解按照作用分类
曾经的 xml配置
<bean id="accountService" class="com.sanyue.service.impl.AccountServiceImpl" scope
="" init-method="" destroy-method="">
<property name="" value="" ref=""></property>
</bean>
1. 用于创建对象的注解
它们的作用就和在 xml 配置文件中编写一个 标签实现的功能是一样的
@Component :
作用:用于把当前对象存入 Spring 容器中
属性:value 用于指定 bean 的id,它的默认值是当前的类名且是首字母改小写
@Control
表现层
@Service
业务层
@Repository
持久层
以上三个注解他们的作用和 Component 是一模一样,他们三个是 Spring 为我们提供明确的三层使用的注解,使我们的三层对象更加清晰
2. 用于注入数据注解
它们的作用就和在 xml 配置文件中的 bean 标签中写一个 标签的作用是一样的
Autowird
- 自动按照类型注入,只要容器中有唯一的一个 bean 对象类型和要注入的变量类型匹配,就可以注入成功
- 细节:如果IoC容器中没有任何 bean的类型和要注入的变量类型匹配,则报错
- 如果IoC容器中有多个类型匹配时,
- 出现位置可以是变量上、方法上
- 在使用注解注入时,set 方法就不是必须的
Qualifier
- 作用:在按照类中注入的基础上再按照名称注入,它给类成员注入时不能单独使用,但是在给方法参数注入时可以
Resource
作用
直接按照 bean 的 id 注入,它可以独立使用
属性
Name: 用于指定 bean 的 id
Value
作用
用于注入基本类型和 String类型的数据
属性
value : 用于指定数据的值,它可以使用 Spring 中的 SpEl(也就是spring的 el 表达式)
SpEl的写法: $(表达式)
注意
Autowird、Qualifier、Resource 都只能注入其它 bean 类型的数据,而基本类型和 String 类型无法使用上述注解实现
另外,集合类型的注入只能通过 xml 实现
3. 用于改变作用范围的
它们的作用就和在 xml 配置文件中的 bean 标签使用 scope 属性实现的功能是一样的
scope
作用
用于指定 bean 的作用域
属性
value:指定范围的取值,常用取值 singleton prototype
4. 和生命周期相关 了解即可
它们的作用就和在 xml 配置文件中的 bean 标签使用 init-method 和 destroy-method 的作用是一样的
PreDestory
作用
用于指定销毁方法
PreConstruce
作用
用于指定初始化方法
用于创建的 Component 注解
告知 Spring 在创建容器时,要扫描的包,在扫描时,就会扫描注解,配置所需要的标签不是在 beans 的约束中,而是一个名称为 context 的名称空间和约束中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
自定义
@Component(value = "accountService")
给一个Value属性赋值时可以不写 value,同时有两个属性时,必须要写
自动按照类型注入
用于注入数据的注解
Qualifier
@Component(value = "accountService")
public class AccountServiceImpl implements IAccountService {
public AccountServiceImpl() {
System.out.println("对象创建");
}
@Autowired
@Qualifier("accountDao1")
private IAccountDao accountDao1 = null;
// 业务层调用持久层
public void saveAccount() {
accountDao1.saveAccount();
}
}
Resource
@Component(value = "accountService")
public class AccountServiceImpl implements IAccountService {
public AccountServiceImpl() {
System.out.println("对象创建");
}
@Resource(name = "accountDao2")
private IAccountDao accountDao = null;
// 业务层调用持久层
public void saveAccount() {
accountDao.saveAccount();
}
}
改变作用范围、生命周期的相关注解
@Component(value = "accountService")
//@Scope("prototype")
public class AccountServiceImpl implements IAccountService {
// @Autowired
// @Qualifier("accountDao1")
@Resource(name = "accountDao2")
private IAccountDao accountDao = null;
@PostConstruct
public void init() {
System.out.println("初始化方法执行了");
}
@PreDestroy
public void destroy() {
System.out.println("销毁方法执行了");
}
public void saveAccount() {
accountDao.saveAccount();
}
}
public class Client {
public static void main(String[] args) {
//1.获取核心容器对象
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取Bean对象
IAccountService as = (IAccountService) ac.getBean("accountService");
as.saveAccount();
ac.close();
}
}
测试基于 XML 的 IoC 案例
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--配置service对象-->
<bean id="accountService" class="com.sanyue.service.impl.AccountServiceImpl">
<!--注入Dao对象-->
<property name="accountDao" ref="accountDao"/>
</bean>
<!--配置Dao对象-->
<bean id="accountDao" class="com.sanyue.dao.impl.AccountDaoImpl">
<!--注入QueryRunner-->
<property name="runner" ref="runner"/>
</bean>
<!--配置QueryRunner对象-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype">
<!--注入数据源-->
<constructor-arg name="ds" ref="dataSource"/>
</bean>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--连接数据库的必备信息-->
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy"/>
<property name="user" value="root"/>
<property name="password" value="53116888"/>
</bean>
<!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">-->
<!-- <property name="url" value="jdbc:mysql://localhost:3306/eesy" />-->
<!-- <property name="username" value="root" />-->
<!-- <property name="password" value="53116888" />-->
<!-- <property name="maxActive" value="10"/>-->
<!-- </bean>-->
</beans>