重點(diǎn)內(nèi)容IoC和AOP
Spring
Spring是分層的 Java SE/EE應(yīng)用 full-stack 輕量級(jí)開源框架尤辱,以 IoC(Inverse Of Control:反轉(zhuǎn)控制)和 AOP(Aspect Oriented Programming:面向切面編程)為內(nèi)核汉额,提供了展現(xiàn)層 SpringMVC 和持久層 Spring JDBC 以及業(yè)務(wù)層事務(wù)管理等眾多的企業(yè)級(jí)應(yīng)用技術(shù)呻引,還能整合開源世界眾多著名的第三方框架和類庫量没,逐漸成為使用最多的Java EE 企業(yè)應(yīng)用開源框架
spring 的體系結(jié)構(gòu)
編寫XML 配置spring賬戶轉(zhuǎn)賬案例
實(shí)體類Account .java
package com.neusoft.domain;
import java.io.Serializable;
/**
* @author Eric Lee
* @date 2020/9/5 11:01
*/
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;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
Dao
package com.neusoft.dao;
import com.neusoft.domain.Account;
import java.util.List;
public interface IAccountDao {
List<Account> findAllAccount();
Account findAccountById(Integer accountId);
void saveAccount(Account account);
void updateAccount(Account account);
void deleteAccount(Integer accountId);
}
impl
package com.neusoft.dao.Impl;
import com.neusoft.dao.IAccountDao;
import com.neusoft.domain.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
/**
* @author Eric Lee
* @date 2020/9/5 11:10
*/
public class AccountDaoImpl implements IAccountDao {
private QueryRunner runner;
public void setRunner(QueryRunner runner) {
this.runner = 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);
}
}
@Override
public Account findAccountById(Integer accountId) {
try {
return runner.query("select * from account where id = ?", new BeanHandler<Account>(Account.class), accountId);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void saveAccount(Account account) {
try {
runner.update("insert into account(name,money) values (?, ?)",account.getName(), account.getMoney());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void updateAccount(Account account) {
try {
runner.update("update account set name= ? , money = ? where id=? ",account.getName(), account.getMoney(), account.getId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Override
public void deleteAccount(Integer accountId) {
try {
runner.update("delete from account where id = ?", accountId);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
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">
<!-- 配置service-->
<bean id="accountService" class="com.neusoft.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置dao-->
<bean id="accountDao" class="com.neusoft.dao.Impl.AccountDaoImpl">
<!-- 注入qr-->
<property name="runner" ref="runner"></property>
</bean>
<!-- 配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner">
<!-- 注入數(shù)據(jù)源-->
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<!-- 配置數(shù)據(jù)源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/java9_spring"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
service
package com.neusoft.service;
import com.neusoft.domain.Account;
import java.util.List;
public interface IAccountService {
// 查詢所有
List<Account> findAllAccount();
Account findAccountById(Integer accountId);
void saveAccount(Account account);
void updateAccount(Account account);
void deleteAccount(Integer accountId);
}
實(shí)現(xiàn)
package com.neusoft.service.impl;
import com.neusoft.dao.IAccountDao;
import com.neusoft.dao.Impl.AccountDaoImpl;
import com.neusoft.domain.Account;
import com.neusoft.service.IAccountService;
import java.util.List;
/**
* @author Eric Lee
* @date 2020/9/5 11:39
*/
public class AccountServiceImpl implements IAccountService {
private IAccountDao accountDao;
public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public List<Account> findAllAccount() {
return accountDao.findAllAccount();
}
@Override
public Account findAccountById(Integer accountId) {
return accountDao.findAccountById(accountId);
}
@Override
public void saveAccount(Account account) {
accountDao.saveAccount(account);
}
@Override
public void updateAccount(Account account) {
accountDao.updateAccount(account);
}
@Override
public void deleteAccount(Integer accountId) {
accountDao.deleteAccount(accountId);
}
}
Test
package com.neusoft.test;
import com.neusoft.domain.Account;
import com.neusoft.service.IAccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* @author Eric Lee
* @date 2020/9/5 14:09
*/
public class TestAccountService {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService as = ac.getBean("accountService", IAccountService.class);
@Test
public void testFindAll() {
//3.執(zhí)行方法
List<Account> accounts = as.findAllAccount();
for(Account account : accounts){
System.out.println(account);
}
}
@Test
public void testFindOne() {
//3.執(zhí)行方法
Account account = as.findAccountById(1);
System.out.println(account);
}
@Test
public void testSave() {
Account account = new Account();
account.setName("test");
account.setMoney(12345f);
//3.執(zhí)行方法
as.saveAccount(account);
}
@Test
public void testUpdate() {
//3.執(zhí)行方法
Account account = as.findAccountById(4);
account.setMoney(23456f);
as.updateAccount(account);
}
@Test
public void testDelete() {
//3.執(zhí)行方法
as.deleteAccount(4);
}
}
使用注解進(jìn)行配置
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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.neusoft">
</context:component-scan>
<!-- 配置QueryRunner-->
<bean id="runner" class="org.apache.commons.dbutils.QueryRunner">
<!-- 注入數(shù)據(jù)源-->
<constructor-arg name="ds" ref="dataSource"></constructor-arg>
</bean>
<!-- 配置數(shù)據(jù)源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/java9_spring"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
核心修改
將對(duì)象交給spring去管理不见, 指定掃描的包名
<context:component-scan base-package="com.neusoft">
</context:component-scan>
service層
dao層
@Component
- 作用
把資源讓 spring 來管理霍殴。相當(dāng)于在 xml 中配置一個(gè) bean。 - 屬性
value:指定 bean 的 id独令。如果不指定 value 屬性闺阱,默認(rèn) bean 的 id 是當(dāng)前類的類名炮车。首字母小寫。
@Controller @Service @Repository
他們?nèi)齻€(gè)注解都是針對(duì)一個(gè)的衍生注解酣溃,他們的作用及屬性都是一模一樣的。
他們只不過是提供了更加明確的語義化纪隙。
@Controller :一般用于表現(xiàn)層的注解赊豌。
@Service :一般用于業(yè)務(wù)層的注解。
@Repository :一般用于持久層的注解
細(xì)節(jié):如果注解中有且只有一個(gè)屬性 要賦值時(shí)是 绵咱,且名稱是 value 碘饼,value 在賦值是可以不寫
@Autowired
作用:自動(dòng)按照類型注入。當(dāng)使用注解注入屬性時(shí)悲伶,set方法可以省略艾恼。它只能注入其他 bean 類型。當(dāng)有多個(gè)類型匹配時(shí)麸锉,使用要注入的對(duì)象變量名稱作為 bean 的 id钠绍,在 spring 容器查找,找到了也可以注入成功花沉。找不到就報(bào)錯(cuò)柳爽。
相當(dāng)于: <property name="accountDao" ref="accountDao"></property>