第一章 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("保存了账户");
} }
ApplicationContext 接口的实现类
Bean标签
作用:
用于配置对象,让Spring来创建
默认情况下它调用的是类中的无参构造函数。如果没有创建不出来
属性:
id 给对象在容器里提供一个唯一标识
class 类的全限定类名
scope 作用范围
init-method 指定类中初始化方法名称
destory-method 指定类中销毁方法名称
单例对象:scope = "singleton" 一个应用只有一个对象的实例,作用于整个引用
多例对象:scopr = "prototype" 每次访问对象时都会重新创建实例
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);
} } }
一个连接数据库的配置类
/**
* 连接数据库的配置类
* @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
什么是AOP
把重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础上对已有方法增强
动态代理的特点 :
字节码随用随创建,随用随加载、(静态代理一上来就创建好并加载)
装饰者模式就是静态代理的表现