Spring核心思想的簡單實現
//準備工作
//==================================================================================================================
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;
}
}
本文出自 “12155865” 博客,請務必保留此出處http://12165865.blog.51cto.com/12155865/1969193
Spring核心思想的簡單實現