搭建一个SSM项目 - 2:第一个S--Spring

2020-08-26  本文已影响0人  轻云绿原

前一章:搭建一个SSM项目 - 1:新建项目

引入Spring的目标是,引入inversion of control(IOC),IOC的的另一个名字是DI。

1:这里是Maven项目,所以要在pom.xml里引入spring的依赖坐标。

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${springVersion}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${springVersion}</version>
    </dependency>

2:应用IOC

创建一个配置文件,名字随意.我这里取名spring-config.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
        https://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.ppf" />
</beans>
<context:component-scan base-package="com.ppf" />

这一行的解释如下:

Scans the classpath for annotated components that will be auto-registered as Spring beans. By default, the Spring-provided @Component, @Repository, @Service, @Controller, @RestController, @ControllerAdvice, and @Configuration stereotypes will be detected.

就是把指定包下面的带特定注解的对象,注册到Spring的容器中,成为bean

3:我们加两个bean试试看:

第一个bean持久层

@Repository("accountDao")
public class AccountDaoImp implements IAccountDao {
    @Override
    public Account fetchAccount(Integer id) {
        System.out.println("fetchAccount " + id);
        return null;
    }

    @Override
    public Account fetchAccount(String name, String password) {
        System.out.println("fetchAccount " + name + password);
        return null;
    }

    @Override
    public List<Account> fetchSomeAccounts(Integer offset, Integer count) {
        System.out.println("fetchSomeAccounts " + offset + " " + count);
        return null;
    }

    @Override
    public Account addAccount(String name, String password, String avatarURI) {
        System.out.println("addAccount " + name + " " + avatarURI);
        return null;
    }
}

@Repository("accountDao"):意思为持久层的bean

第二个bean业务层

@Service("accountService")
public class AccountServiceImp implements IAccountService {

    @Autowired
    @Qualifier("accountDao")
    private IAccountDao accountDao;

    @Override
    public Account fetchAccount(Integer id) {
        System.out.println("service => fetchAccount" + id);
        return accountDao.fetchAccount(id);
    }

    @Override
    public Account fetchAccount(String name, String password) {
        return accountDao.fetchAccount(name,password);
    }

    @Override
    public List<Account> fetchSomeAccounts(Integer offset, Integer count) {
        return accountDao.fetchSomeAccounts(offset,count);
    }

    @Override
    public Account addAccount(String name, String password, String avatarURI) {
        return accountDao.addAccount(name,password,avatarURI);
    }
}

4:怎么用?

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
IAccountService service = (IAccountService) context.getBean("accountService");
Account account = service.fetchAccount(2);
上一篇下一篇

猜你喜欢

热点阅读