//准备工作
//==================================================================================================================
package com.ys.service;
import com.ys.model.User;
/*
* @作者 邓聪
* @Description:UserService
* @Date 15:39 2017/9/27
*/
public interface UserService {
public User login(String uname, String pwd);
}
//==================================================================================================================
package com.ys.service.impl;
import com.ys.dao.UserDao;
import com.ys.model.User;
import com.ys.service.UserService;
/*
* @作者 邓聪
* @Description:UserServiceImpl
* @Date 15:33 2017/9/27
*/
public class UserServiceImpl implements UserService{
private UserDao userDao;
//spring 的依赖注入
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public User login(String uname, String pwd) {
return userDao.login(uname,pwd);
}
}
//==================================================================================================================
package com.ys.dao;
import com.ys.model.User;
/*
* @作者 邓聪
* @Description:
* @Date 15:58 2017/9/27
*/
public interface UserDao {
User login(String uname, String pwd);
}
//==================================================================================================================
package com.ys.dao.impl;
import com.ys.dao.UserDao;
import com.ys.model.User;
import com.ys.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
/*
* @作者 邓聪
* @Description:UserDaoImpl
* @Date 15:38 2017/9/27
*/
public class UserDaoImpl implements UserDao{
private QueryRunner qr=new QueryRunner(C3P0Utils.getDataSource());
@Override
public User login(String uname, String pwd) {
System.out.println("loginSuccess!!..."+"用户名:"+uname+"密码:"+pwd);
return null;
}
}
//======================================华丽的分割线===========================================================================
package com.ys.utils;
import com.ys.dao.UserDao;
import com.ys.dao.impl.UserDaoImpl;
import com.ys.model.User;
import com.ys.service.UserService;
import com.ys.service.impl.UserServiceImpl;
import java.lang.reflect.Method;
/*
* @作者 邓聪
* @Description:控制反转和依赖注入的简单实现
* @Date 15:19 2017/9/27
*/
public class MySpring {
/*
* @作者 邓聪
* @Description:控制反转Ioc
* @Date 15:20 2017/9/27
*/
public static Object getT(String ClassName){
try {
Object obj=Class.forName(ClassName).newInstance();
return obj;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//依赖注入的简单实现 被注入对象 注入对象
public static Object zhuru(Object object_1, Object object_2){
try {
//获取接口
Class<?>[] interfaces = object_2.getClass().getInterfaces();
//获取注入对象全限定名,然后获得类名,XxxImpl
String[] split = object_2.getClass().getName().split("\\.");
String nameImpl= split[split.length-1];
//去掉Impl(如果实现类的后缀名长度不为4,那么该方法会出现问题)
String oName= nameImpl.substring(0,nameImpl.length()-4);
//反射获取注入接口引用的方法
Method method = object_1.getClass().getDeclaredMethod("set"+oName,interfaces);
method.invoke(object_1, object_2);
// System.out.println(method.getName());
//返回被注入的对象
return object_1;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/*
* @作者 邓聪
* @Description:测试成功
* @Date 15:25 2017/9/27
*/
public static void main(String[] args) {
User user=(User)MySpring.getT("com.ys.model.User");
UserService userService=(UserService) MySpring.getT("com.ys.service.impl.UserServiceImpl");
UserDao userDao=(UserDao) MySpring.getT("com.ys.dao.impl.UserDaoImpl");
UserService us=(UserService) MySpring.zhuru(userService, userDao);
us.login("dengcong","123");
}
}
//打印结果
loginSuccess!!...用户名:dengcong密码:123
//====================Spring整合jUnit4===============================================
package com.ys.utils;/*
* @作者 邓聪
* @Description:包依赖commons-logging.jar spring-core.jar spring-context.jar spring-beans.jar spring-expression.jar spring-
test.jar
*
*
* @Date $time$ $date$
*/
import com.ys.dao.UserDao;
import com.ys.service.UserService;
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;
@RunWith(SpringJUnit4ClassRunner.class)//表示整合JUnit4进行测试
@ContextConfiguration(locations={"classpath:applicationContext.xml"})//加载spring配置文件
public class XTest {
@Autowired //自动注入
private UserService userService;
// @Autowired //自动注入
// private UserDao userDaoImpl;
@Test
public void test1(){
userService.login("dengcong","123");
}
}
//打印结果
loginSuccess!!...用户名:dengcong密码:123
//==当然,使用注解配合包扫描实现对象的自动创建和属性的注入可以让代码看起来简洁======
appcationContext.xml下beans中配置包扫描
<context:component-scan base-package="com.ys.dao,com.ys.service"></context:component-scan>
//=====然后包中创建类时配置注解=============================================
package com.ys.service.impl;
import com.ys.dao.UserDao;
import com.ys.model.User;
import com.ys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/*
* @作者 邓聪
* @Description:配置组件注解@Component("userService"),同时注入userDao
* @Date 15:33 2017/9/27
*/
@Component("userService")
public class UserServiceImpl implements UserService{
@Autowired
private UserDao userDao;
//spring 的依赖注入,配置Autowired注解时,不用写这段代码
// public void setUserDao(UserDao userDao) {
// this.userDao = userDao;
// }
@Override
public User login(String uname, String pwd) {
return userDao.login(uname,pwd);
}
}
//===============================================
package com.ys.dao.impl;
import com.ys.dao.UserDao;
import com.ys.model.User;
import com.ys.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.stereotype.Component;
/*
* @作者 邓聪
* @Description:配置组件注解@Component("userDao")
* @Date 15:38 2017/9/27
*/
@Component("userDao")
public class UserDaoImpl implements UserDao{
private QueryRunner qr=new QueryRunner(C3P0Utils.getDataSource());
@Override
public User login(String uname, String pwd) {
System.out.println("loginSuccess!!..."+"用户名:"+uname+"密码:"+pwd);
return null;
}
}