Spring Step By Step今日看点程序员

03 Spring IoC之对象间的相互关系和 Spring 应

2016-11-24  本文已影响290人  赖赖oO

转载请注明来源 赖赖的博客

导语

你知道的越多,越容易接受新的知识。

Spring IoC是有应用场景的,其特定决定了应用的结构;也就是说,一旦你用了Spring,也就受到了框架的限制,只能使用一些特定的设计模式。

实例

项目工程目录结构和代码获取地址

获取地址(版本Log将会注明每一个版本对应的课程)

https://github.com/laiyijie/SpringLearning

目录结构

目录结构

乍一看,工程似乎打出上一次不少,多了好多;然而程序员做事,向来不会毫无章法,自有其中的规律,而其中的规律了解了,文件再多,也只是重复的工作量;

这个所谓的章法,就是今天要说的重点之一,应用的分层;

Spring应用结构

现在工程有三个包,包之间的关系如下:

具体含义如下:

me.laiyijie.demo

最上层的应用层

me.laiyijie.demo.service

业务逻辑层,所有的业务逻辑应该在这一层实现;

me.laiyijie.demo.dataaccess

数据连接层,屏蔽底层于文件、数据库等持久化操作;

小结

分层满足两个原则:

  1. 各层职责明确
  2. 仅调用自己的下一层,不会越层调用

运行工程

运行具有Main函数的 App.java
得到如下输出

false

详细解读

程序入口为App.java从这里入口读代码

App.java

package me.laiyijie.demo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import me.laiyijie.demo.service.UserService;

public class App {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("root-context.xml");
        
        UserService userService = context.getBean(UserService.class);
        
        System.out.println(userService.login("lailai", "laiyijie","127.0.0.1"));
        
        context.close();
    }
}

Main函数中依旧只有四行代码,这里引用到了UserService,并且调用了UserServicelogin方法。

我们来看下UserService

UserService.java

package me.laiyijie.demo.service;

public interface UserService{
    
    boolean login(String username,String password,String ip);
    
}

OK,非常简单,只是一个接口,定义了有这么一个方法,具体实现是在同一个包下面的另一个文件,UserServiceImpl.java中

UserServiceImpl.java

package me.laiyijie.demo.service;

import me.laiyijie.demo.dataaccess.AccountAccess;
import me.laiyijie.demo.dataaccess.LoginLogAccess;

public class UserServiceImpl implements UserService {

    private AccountAccess accountAccess;
    
    private LoginLogAccess loginLogAccess;
    
    public boolean login(String username, String password,String ip) {
        
        if (!accountAccess.isAccountExist(username)) {
            return false;
        }
        
        if (accountAccess.isPasswordRight(username, password)) {
            accountAccess.updateLastLoginTime(username);
            loginLogAccess.addLoginLog(username, ip);
            return true;
        }
        return false;
    }

    public AccountAccess getAccountAccess() {
        return accountAccess;
    }

    public void setAccountAccess(AccountAccess accountAccess) {
        this.accountAccess = accountAccess;
    }

    public LoginLogAccess getLoginLogAccess() {
        return loginLogAccess;
    }

    public void setLoginLogAccess(LoginLogAccess loginLogAccess) {
        this.loginLogAccess = loginLogAccess;
    }
}

乍一看这个代码,好长,函数好多。其实并不复杂,
如果你对Java有一些了解的话应该知道,后面的四个函数是getter和setter函数,是向外提供了操作私有变量的方法;
而最前面一个函数正是实现了UserService这个接口。

在这个类中,我们可以看到,其通过私有变量的方式引用了dataaccess层的两个对象!

private AccountAccess accountAccess;

private LoginLogAccess loginLogAccess;

那么,在没有使用Spring的情况下,我们的main函数应该这样调用login方法:

public static void main(String[] args) {

    AccountAccess accountAccess = new AccountAccessImpl();
    LoginLogAccess loginLogAccess = new LoginLogAccessImpl();
    
    UserServiceImpl userServiceImpl = new UserServiceImpl();
    userServiceImpl.setAccountAccess(accountAccess);
    userServiceImpl.setLoginLogAccess(loginLogAccess);
    
    userServiceImpl.login("lailai", "laiyijie", "127.0.0.1");
}

然而在App.java中,我们直接

UserService userService = context.getBean(UserService.class);
        
System.out.println(userService.login("lailai", "laiyijie","127.0.0.1"));

也就是说,对象创建,以及对象之间的依赖关联通过root-context.xml来完成!

root-context.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-4.3.xsd">

    <bean id="accountAccessImpl" class="me.laiyijie.demo.dataaccess.AccountAccessImpl" ></bean>
    <bean id="loginLogAccessImpl" class="me.laiyijie.demo.dataaccess.LoginLogAccessImpl"></bean>
    
    <bean class="me.laiyijie.demo.service.UserServiceImpl">
        <property ref="accountAccessImpl" name="accountAccess"></property>
        <property ref="loginLogAccessImpl" name="loginLogAccess"></property>
    </bean>

</beans>

经过前两节课的基础,你应该可以轻易看懂这些配置,无非就是完成了对象的创建和属性的设置操作;也就是与这段代码的功能一样:

    AccountAccess accountAccess = new AccountAccessImpl();
    LoginLogAccess loginLogAccess = new LoginLogAccessImpl();
    
    UserServiceImpl userServiceImpl = new UserServiceImpl();
    userServiceImpl.setAccountAccess(accountAccess);
    userServiceImpl.setLoginLogAccess(loginLogAccess);

而这种配置方法,也就被称为属性注入

小结

附:

dataaccess层均为模拟,未实际接入持久层,待后续添加。

AccountAccess.java

package me.laiyijie.demo.dataaccess;

public interface AccountAccess {
    
    boolean isAccountExist(String username);

    boolean isPasswordRight(String username, String password);
    
    void updateLastLoginTime(String username);
}

AccountAccessImpl.java

package me.laiyijie.demo.dataaccess;

public class AccountAccessImpl implements AccountAccess {

    public boolean isAccountExist(String username) {
        
        if ("laiyijie".equals(username)) {
            return true;
        }
        return false;
    }

    public void updateLastLoginTime(String username) {

    }

    public boolean isPasswordRight(String username, String password) {
        
        if ("laiyijie".equals(username)&&"123456".equals(password)) {
            return true;
        }
        return false;
    }

}

LoginLogAccess.java

package me.laiyijie.demo.dataaccess;

public interface LoginLogAccess {
    void addLoginLog(String username,String ip);
}

LoginLogAccessImpl.java

package me.laiyijie.demo.dataaccess;

public class LoginLogAccessImpl implements LoginLogAccess {

    public void addLoginLog(String username, String ip) {

    }
}

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>me.laiyijie</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>

    </dependencies>
</project>
上一篇 下一篇

猜你喜欢

热点阅读