大家好,又见面了,我是你们的朋友全栈君。
目录结构:
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
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/106640.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...