Spring的基本业务流程与类的多实现

Spring的基本业务流程与类的多实现Spring的基本业务流程与类的多实现

大家好,又见面了,我是你们的朋友全栈君。

目录结构:
在这里插入图片描述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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

    <context:component-scan base-package="com.xbj"/>
</beans>

UserCoutroller.java

package com.xbj.controller;

import com.xbj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

/** * @Author:晓宇码匠 * @Date:2019/6/15 0015 */
@Controller
public class UserCoutroller { 
   
    /* * Autowired:注入的是接口的实现类 * 如果没有实现类 无法注入 * 如果有多个实现类 也无法自动注入,需要通过Qualifier 指定ID * ID的默认值是 类名的首字母小写 * */
    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;

    public String addUser() { 
   
        userService.addUser();
        return "SUCCESS";
    }
}

UserDaoImpl.java

package com.xbj.dao.impl;
import com.xbj.dao.UserDao;
import org.springframework.stereotype.Repository;
/** * @Author:晓宇码匠 * @Date:2019/6/15 0015 */
@Repository
public class UserDaoImpl implements UserDao { 
   

    public void addUser() { 
   
        System.out.println("addUser");
    }
}

UserDao.java

package com.xbj.dao;
/** * @Author:晓宇码匠 * @Date:2019/6/15 0015 */
public interface UserDao { 
   
     void addUser();
}

UserServiceImpl.java

package com.xbj.service.impl;

import com.xbj.dao.UserDao;
import com.xbj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/** * @Author:晓宇码匠 * @Date:2019/6/15 0015 * * @Scope如果是设为多例,在方法结束时,不会进行销毁,而是变成休眠态 * 反之,单例就会执行销毁方法 */
@Service
@Scope(value = "prototype")//将实现类设置成多例的 、 不设默认是单例的singleton
public class UserServiceImpl implements UserService { 
   

    @Autowired
    private UserDao userDao;

    public void addUser() { 
   
        userDao.addUser();
    }

    @PostConstruct
    public void init(){ 
   
        //初始化
        System.out.println("初始化");
    }
    @PreDestroy
    public void dest(){ 
   
        //销毁
        System.out.println("销毁");
    }
}

UserServiceImpl2.java

package com.xbj.service.impl;

        import com.xbj.dao.UserDao;
        import com.xbj.service.UserService;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.context.annotation.Scope;
        import org.springframework.stereotype.Service;

        import javax.annotation.PostConstruct;
        import javax.annotation.PreDestroy;

/** * @Author:晓宇码匠 * @Date:2019/6/15 0015 */
@Service
@Scope(value = "prototype")//将实现类设置成多例的 、 不设默认是单例的singleton
public class UserServiceImpl2 implements UserService { 
   

    @Autowired
    private UserDao userDao;

    public void addUser() { 
   
        userDao.addUser();
    }

}

UserService.java

package com.xbj.service;

/** * @Author:晓宇码匠 * @Date:2019/6/15 0015 */
public interface UserService { 
   
    void addUser();
}

test.java

import com.xbj.controller.UserCoutroller;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/** * @Author:晓宇码匠 * @Date:2019/6/15 0015 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { 
   "classpath:applicationContext.xml"})
public class test { 
   
    @Autowired
    private  UserCoutroller userCoutroller;
    @Test
    public void t1(){ 
   
        System.out.println(userCoutroller.addUser());
    }
}

单例,控制台输出:

初始化
addUser
SUCCESS
销毁

多例,控制台输出:

初始化
addUser
SUCCESS
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/106640.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号