Spring 基于注解的配置有两个关键的点:
-
用于创建对象的:
它们的的作用就和在 XML 配置文件中编写一个 标签实现的功能是一样的;
@Component 注解:
作用:用于把当前类对象存入 spring 容器中
属性:value:用于指定 bean 的 id,默认是当前类名且首字母小写 -
@Controller:一般用在控制层,作用和属性和 @Component 作用一样
-
@Service:一般用在业务层,作用和属性和 @Component 作用一样
-
@Repository:一般用在持久层,作用和属性和 @Component 作用一样
它们三个注解是 spring 为我们提供明确的三层使用的注解,是我们的三层对象更加清晰
-
用于注入数据的:
它们的作用和在 XML 配置文件中编写一个 标签的作用是一样的; -
@Autowired 注解:
用于对 Bean 的属性变量、属性的 setter 方法及构造方法进行标注,配合对应的处理器完成 Bean 的自动配置工作。@Autowired 注解默认按照 Bean 类型进行装配。@Autowired 注解加上@Qualifier 注解,可以指定直接一个 Bean 实例名称来进行装配。 -
@Qualifier 注解:在按照类注入的基础上再按照名称注入,它给类成员注入时不能单独使用,但是在给方法参数注入时可以。
属性:value:用于指定注入的 bean 的 id。
案例项目:
项目结构:
Account:
package com.itheima.entity;
import java.io.Serializable;
/**
* 账户实体类
*/
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 +
'}';
}
}
IAccountDao:
package com.itheima.dao;
import com.itheima.entity.Account;
import java.util.List;
/**
* 账户的持久层接口
*/
public interface IAccountDao {
/**
* 查询所有
* @return
*/
List<Account> findAllAccount();
/**
* 查询一个
* @return
*/
Account findAccountById(Integer accountId);
/**
* 保存操作
* @param account
*/
void saveAccount(Account account);
void updateAccount(Account account);
void deleteAccount(Integer accountId);
}
AccountDaoImpl
package com.itheima.dao.impl;
import com.itheima.dao.IAccountDao;
import com.itheima.entity.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* 持久层实现类
* @Repository == @Component(默认是当前类名且首字母小写)
*/
@Repository("accountDao")
// 用来创建对象,表明这是一个组件,组件名为 accountDao
public class AccountDaoImpl implements IAccountDao {
@Autowired
// 自动注入 runner
private QueryRunner runner;
/**
* 查询所有的账户
* @return
*/
@Override
public List<Account> findAllAccount() {
try {
return runner.query("select * from account", new BeanListHandler<Account>(Account.class));
} catch (Exception 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 (Exception 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 (Exception 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 (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void deleteAccount(Integer accountId) {
try {
runner.update("delete from account where id = ?", accountId );
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
IAccountService:
package com.itheima.service;
import com.itheima.entity.Account;
import java.util.List;
/**
* 账户的业务层接口
*/
public interface IAccountService {
/**
* 查询所有
* @return
*/
List<Account> findAllAccount();
/**
* 查询一个
* @return
*/`package com.itheima.service.impl;
import com.itheima.dao.IAccountDao;
import com.itheima.entity.Account;
import com.itheima.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 账户的业务层实现类
*/
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Autowired
// 自动寻找bean
private IAccountDao 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);
}
}
`
Account findAccountById(Integer accountId);
/**
* 保存操作
* @param account
*/
void saveAccount(Account account);
void updateAccount(Account account);
void deleteAccount(Integer accountId);
}
AccountServiceImpl:
package com.itheima.service.impl;
import com.itheima.dao.IAccountDao;
import com.itheima.entity.Account;
import com.itheima.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 账户的业务层实现类
*/
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Autowired
// 自动寻找bean 并注入
private IAccountDao 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);
}
}
AccountServiceTest:
package com.itheima.test;
import com.itheima.entity.Account;
import com.itheima.service.IAccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
/**
* 使用Junit单元测试:测试我们的配置
*/
public class AccountServiceTest {
@Test
public void testFindAll() {
// 1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
// 2.得到业务层对象
IAccountService as = ac.getBean("accountService", IAccountService.class);
// 3.执行方法
List<Account> accounts = as.findAllAccount();
for(Account account: accounts) {
System.out.println(account);
}
}
@Test
public void testFindOne() {
// 1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
// 2.得到业务层对象
IAccountService as = ac.getBean("accountService", IAccountService.class);
// 3.执行方法
Account account = as.findAccountById(1);
System.out.println(account);
}
@Test
public void testSave() {
Account account = new Account();
account.setName("test");
account.setMoney(12345f);
// 1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
// 2.得到业务层对象
IAccountService as = ac.getBean("accountService", IAccountService.class);
// 3.执行方法
as.saveAccount(account);
}
@Test
public void testUpdate() {
// 1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
// 2.得到业务层对象
IAccountService as = ac.getBean("accountService", IAccountService.class);
// 3.执行方法
Account account = as.findAccountById(4);
account.setMoney(2345f);
as.updateAccount(account);
}
@Test
public void testDelete() {
// 1.获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
// 2.得到业务层对象
IAccountService as = ac.getBean("accountService", IAccountService.class);
// 3.执行方法
as.deleteAccount(4);
}
}
Spring 基于注解(Annotation)的Bean装配就分享到这里啦!
说在最后的话:编写实属不易,若喜欢或者对你有帮助记得点赞+关注或者收藏哦~