spring started 3 - Understanding the IOC through small projects

Now through a small program to understand the IOC thought, this might be more intuitive, the first project structure is the controller (control layer, he decided to accept the data and distribution), service (he was responsible for abstract some of the service logic), dao (responsible for dealing with database )

 

Look at our model manipulation

package bean;

public class User {
    private int id;
    private String username;
    private String password;

    public User(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

Then look at the database processing module

// UserDao
package dao;

import bean.User;

public interface UserDao {
    User getUser();
}
// DataSources 数据库的相关配置类
package dao.impl;

public class DataSources {
    private String username;
    private String password;
    private String url;
    private String driverClass;

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDriverClass() {
        return driverClass;
    }

    public void setDriverClass(String driverClass) {
        this.driverClass = driverClass;
    }

    public DataSources() {
    }

    public DataSources(String username, String password, String url, String driverClass) {
        this.username = username;
        this.password = password;

        this.url = url;
        this.driverClass = driverClass;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public String toString() {
        return "DataSources{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", url='" + url + '\'' +
                ", driverClass='" + driverClass + '\'' +
                '}';
    }
}

// UserDaoImpl 数据库的相关操作
package dao.impl;

import bean.User;
import dao.UserDao;

public class UserDaoImpl implements UserDao {
    DataSources ds;

    public UserDaoImpl() {
    }

    public UserDaoImpl(DataSources ds) {
        this.ds = ds;
    }

    public User getUser() {
        System.out.println("Simulation Database Connection "+ DS);
         return  new new User(1, "mark", "1234");
    }
}

 Then look at the service

// UserService service对外接口
package service;

import bean.User;

public interface UserService {

    User getUser();
}

// UserServiceImpl 处理
package service.impl;

import bean.User;
import dao.UserDao;
import service.UserService;

public class UserServiceImpl implements UserService {
    UserDao userDao;

    public UserServiceImpl() {
    }

    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    public void setUserDao(UserDao ud) {
        this.userDao = ud;
    }

    public UserDao getUserDao() {
        return this.userDao;
    }

    public User getUser() {
        return userDao.getUser();
    }
}

We look controller

package controller;

import bean.User;
import service.UserService;
import service.impl.UserServiceImpl;

public class UserController {
    UserService userService;

    public void getUser() {
        User user = userService.getUser();
        System.out.println(user);
    }

    public void setUserService(UserServiceImpl userService) {
        this.userService = userService;
    }
}

We focused look at the file applicationContext.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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 数据库连接 使用property注入 -->
    <bean id="db" class="dao.impl.DataSources">
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/user"/>
        <property name="username" value="root"/>
        <property name="password" value="mysql"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    </ the bean>
    <! - database operations using the constructor-arg constructor for injection, because it is the object, we use REF -> 
    <the bean ID = "DAO" class = "dao.impl.UserDaoImpl"> 
        <constructor-arg = index " 0 "name =" ds "ref =" db "/> 
    </ bean> 

    <-! p server operation we use namespace injection, an object so to add ref -> 
    <bean the above mentioned id =" service " class = "service.impl.UserServiceImpl" P: userDao-REF = "DAO" /> 

    ! <- control layer operation -> 
    <the bean ID = "Controller" class = "controller.UserController" P: = REF-that userService " -Service "/> 

</ Beans>

The final look at the test code

import controller.UserController;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestUser {

    @Test
    public void test(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserController uc = context.getBean("controller", UserController.class);
        uc.getUser();

    }
}
//   模拟数据库连接DataSources{username='root', password='mysql', url='jdbc:mysql://127.0.0.1:3306/user', driverClass='com.mysql.jdbc.Driver'}
User{id=1, username='mark', password='1234'}

Code runs through, we see in service, dao, controller, we did not initialize any object, all the information we initialize all applicationContext.xml placed inside the spring to help us carry out related operations, to achieve this understanding coupling, and later need to make changes, such as modifying database, we modified directly in applicationContext.xml like inside, you do not need to look for a layer of code project a project.

Guess you like

Origin www.cnblogs.com/yangshixiong/p/12188734.html