A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 许多钱 初级黑马   /  2019-7-7 10:05  /  818 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.dao.impl.UserDaoImpl;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {


/*  常规测试
  UserDao dao =new UserDaoImpl();
    public void save() {
        dao.show();
    }*/



  /*  //spring-context测试 未依赖注入
    public void save() {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao dao = app.getBean(UserDao.class);
        dao.show();
        System.out.println(45678);
    }*/

    /*
    依赖注入
      UserDao dao;

        public void setDao(UserDao dao) {
            this.dao = dao;
        }

        public void save() {


          System.out.println(45678);
            dao.show();
            System.out.println(1234567890);
      }*/

    @Autowired
    UserDao dao;
    public void save() {
        System.out.println(45678);
        dao.show();
        System.out.println(1234567890);
    }


}


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">



<!--常规-->
<!--    <bean id="userDao" class=" com.itheima.dao.impl.UserDaoImpl"></bean>

<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">

</bean>-->

<!--    &lt;!&ndash;依赖注入&ndash;&gt;
    <bean id="userDao" class=" com.itheima.dao.impl.UserDaoImpl"></bean>

    <bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
        <property name="dao" ref="userDao"></property>
    </bean>-->


</beans>
package com.itheima.controller;

import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {


    //常规测试
    @org.junit.Test
    public void test1() {
        UserService service = new UserServiceImpl();
        service.save();
    }

    //spring-context测试 未依赖注入
    @org.junit.Test
    public void test2() {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService service = app.getBean(UserService.class);
        service.save();

    }

    //依赖注入
    @org.junit.Test
    public void test3() {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService service = app.getBean(UserService.class);
        service.save();

    }

    //注解
    @org.junit.Test
    public void test4() {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService service = app.getBean(UserService.class);
        service.save();

    }
}

package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {


    public void show() {

        System.out.println("daoshow方法执行");
    }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马