Spring复习笔记(个人查漏补缺)

第一章  Spring概述

Spring是分层的Java SE/EE应用full-stack轻量级开源框架,以IoC(反转控制)和AOP(面向切面编程)为内核,提供了展现层Spring MVC和 持久层Spring JDBC以及业务层事务管理等众多的企业级应用技术。

 

Spring的优势

方便解耦,简化开发

AOP编程的支持

声明式事务的支持

方便程序测试

方便集成各种框架

降低Java EE API的使用难度

Java源码是经典学习范例

 

Spring的体系结构

 

第二章 IoC的概念和作业

 

划分模块的一个准则就是高内聚,低耦合

耦合分类:

内容耦合、公共耦合、外部耦合、控制耦合、标记耦合、数据耦合、非直接耦合

 

耦合是影响软件复杂程度和设计质量的一个重要因素,采用原则:

如果模块间存在耦合,就尽量使用数据耦合,少用控制耦合,限制公共耦合的范围,尽量避免使用内容耦合

 

工厂模式解耦

在实际开发中可以把三层的对象都使用配置文件配置起来,当启动服务器应用加载的时候,让一个类中的方法通过读取配置文件,把这些对象创建出来并存起来。(读取配置文件,创建和获取的类就是工厂)

 

控制反转

1、存哪去?

在应用加载时,创建一个Map,用于存放三层对象。(把这个map叫做容器)

2、嘛是工厂?

负责给我们从容器中去获取指定对象的类

3.明确IoC的作用

削减计算机程序中的耦合(解除代码中的依赖关系)

 

第三章 使用Spring的IoC解决程序中的依赖关系

 

创建业务层接口和实现类
public interface IAccountService {
/**
* 保存账户(此处只是模拟,并不是真的要保存)
*/
void saveAccount();
}
/**
* 账户的业务层实现类
* @author 黑马程序员
* @Company http://www.ithiema.com
* @Version 1.0
*/
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao = new AccountDaoImpl();//此处的依赖关系有待解决
@Override
public void saveAccount() {
accountDao.saveAccount();
} }
创建持久层接口和实现类
public interface IAccountDao {
/**
* 保存账户
*/
void saveAccount();
}
/**
* 账户的持久层实现类
* @author 黑马程序员
* @Company http://www.ithiema.com
* @Version 1.0
*/
public class AccountDaoImpl implements IAccountDao {
@Override
public void saveAccount() {
System.out.println("保存了账户");
} }

 

BeanFactory ApplicationContext 的区别(创建的时间点不一样)
BeanFactory是Spring容器中的顶层接口   什么时候使用 什么时候创建
ApplicationContext是它的子接口               只要读配置文件 默认情况下就会被创建

 

ApplicationContext 接口的实现类

ClassPathXmlApplicationContext
它是从类的根路径下加载配置文件
推荐使用这种
FileSystemXmlApplicationContext
它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。
AnnotationConfigApplicationContext:
当我们使用注解配置容器对象时,需要使用此类来创建 spring 容器。它用来读取注解。

Bean标签

作用:

用于配置对象,让Spring来创建

默认情况下它调用的是类中的无参构造函数。如果没有创建不出来

属性:

id 给对象在容器里提供一个唯一标识

class  类的全限定类名

scope 作用范围

init-method 指定类中初始化方法名称

destory-method 指定类中销毁方法名称

 

单例对象:scope = "singleton"  一个应用只有一个对象的实例,作用于整个引用

多例对象:scopr = "prototype" 每次访问对象时都会重新创建实例

 

spring 的依赖注入
等框架把持久层对象传入业务层,不需要自己去获取
 
 
 
使用 spring IoC 的实现账户的 CRUD

 

创建数据库和编写实体类
create table account(
id int primary key auto_increment,
name varchar(40),
money float
)character set utf8 collate utf8_general_ci;
insert into account(name,money) values('aaa',1000);
insert into account(name,money) values('bbb',1000);
insert into account(name,money) values('ccc',1000);
/**
* 账户的实体类
* @author 黑马程序员
* @Company http://www.ithiema.com
* @Version 1.0
*/
public class Account implements Serializable {
private Integer id;
private String name;
private Float money;
public Integer getId() {
return id; }
public void setId(Integer id) {
this.id = id; }
public String getName() {
return name; }
public void setName(String name) {
this.name = name; }
public Float getMoney() {
return money; }
public void setMoney(Float money) {
this.money = money; } }
编写持久层代码
public interface IAccountDao {
/**
* 保存
* @param account
*/
void save(Account account);
/**
* 更新
* @param account
*/
void update(Account account);
/**
* 删除
* @param accountId
*/
void delete(Integer accountId);
/**
* 根据 id 查询
* @param accountId
* @return
*/
Account findById(Integer accountId);
/**
* 查询所有
* @return
*/
List<Account> findAll();
}
public class AccountDaoImpl implements IAccountDao {
private DBAssit dbAssit;
public void setDbAssit(DBAssit dbAssit) {
this.dbAssit = dbAssit; }
@Override
public void save(Account account) {
dbAssit.update("insert into 
account(name,money)values(?,?)",account.getName(),account.getMoney());
}
@Override
public void update(Account account) {
dbAssit.update("update account set name=?,money=? where 
id=?",account.getName(),account.getMoney(),account.getId());
}
@Override
public void delete(Integer accountId) {
dbAssit.update("delete from account where id=?",accountId);
}
@Override
public Account findById(Integer accountId) {
return dbAssit.query("select * from account where id=?",new
BeanHandler<Account>(Account.class),accountId);
}
@Override
public List<Account> findAll() {
return dbAssit.query("select * from account where id=?",new
BeanListHandler<Account>(Account.class));
} }
编写业务层代码
/**
* 账户的业务层接口
* @author 黑马程序员
* @Company http://www.ithiema.com
* @Version 1.0
*/
public interface IAccountService {
/**
* 保存账户
* @param account
*/
void saveAccount(Account account);
/**
* 更新账户
* @param account
*/
void updateAccount(Account account);
/**
* 删除账户
* @param account
*/
void deleteAccount(Integer accountId);
/**
* 根据 id 查询账户
* @param accountId
* @return
*/
Account findAccountById(Integer accountId);
/**
* 查询所有账户
* @return
*/
List<Account> findAllAccount();
}
/**
* 账户的业务层实现类
* @author 黑马程序员
* @Company http://www.ithiema.com
* @Version 1.0
*/
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao; }
@Override
public void saveAccount(Account account) {
accountDao.save(account);
}
@Override
public void updateAccount(Account account) {
accountDao.update(account);
}
@Override
public void deleteAccount(Integer accountId) {
accountDao.delete(accountId);
}
@Override
public Account findAccountById(Integer accountId) {
return accountDao.findById(accountId);
}
@Override
public List<Account> findAllAccount() {
return accountDao.findAll();
} }
  创建并编写配置文件

bean.xml

<?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">
</beans>
配置对象
<?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.itheima.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置 dao --> <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"> <property name="dbAssit" ref="dbAssit"></property>
</bean>
<!-- 配置 dbAssit 此处我们只注入了数据源,表明每条语句独立事务--> <bean id="dbAssit" class="com.itheima.dbassit.DBAssit">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///spring_day02"></property> <property name="user" value="root"></property> <property name="password" value="1234"></property>
</bean>
</beans>
测试类代码
public class AccountServiceTest {
/**
* 测试保存
*/
@Test
public void testSaveAccount() {
Account account = new Account();
account.setName("黑马程序员");
account.setMoney(100000f);
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = ac.getBean("accountService",IAccountService.class);
as.saveAccount(account);
}
/**
* 测试查询一个
*/
@Test
public void testFindAccountById() {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = ac.getBean("accountService",IAccountService.class);
Account account = as.findAccountById(1);
System.out.println(account);
}
/**
* 测试更新
*/
@Test
public void testUpdateAccount() {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = ac.getBean("accountService",IAccountService.class);
Account account = as.findAccountById(1);
account.setMoney(20301050f);
as.updateAccount(account);
}
/**
* 测试删除
*/
@Test
public void testDeleteAccount() {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = ac.getBean("accountService",IAccountService.class);
as.deleteAccount(1);
}
/**
* 测试查询所有
*/
@Test
public void testFindAllAccount() {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = ac.getBean("accountService",IAccountService.class);
List<Account> list = as.findAllAccount();
for(Account account : list) {
System.out.println(account);
} } }
基于注解
@Component
作用:把资源让Spring来管理, 相当于xml中配置一个Bean
属性:
value:指定bean的id,如果不指定,就是当前类名首字母小写
 
@Autowired
自动按类型注入
 
@Qualifier
在自动按照类型注入的基础上,再按照bean的id注入
 
 
@Resource
直接按照bean的id注入
 
 
@Value
注入基本数据类型和String数据类型
 
@Scope
指定bean的作用范围
 
@PostConstruct
指定初始化方法
 
@PreDestroy
指定销毁方法
 
@ComponentScan
指定Spring初始化容器时要扫描的包
 
@Bean
只能写在方法上,表明使用此方法创建一个对象
 
 
@PropertySource
用于加载properties文件中的配置
 

一个连接数据库的配置类

/**
* 连接数据库的配置类
* @author 黑马程序员
* @Company http://www.ithiema.com
* @Version 1.0
*/
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
/**
* 创建一个数据源,并存入 spring 容器中
* @return
*/
@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 (Exception e) {
throw new RuntimeException(e);
} } }


jdbc.properties 文件:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/day44_ee247_spring
jdbc.username=root
jdbc.password=1234
 
@Import
用于导入其他的配置类
 
 

 

什么是AOP

把重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础上对已有方法增强

动态代理的特点 :

字节码随用随创建,随用随加载、(静态代理一上来就创建好并加载)

装饰者模式就是静态代理的表现

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值