IOC配置数据源
方式一 :
<?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">
<!-- 定义数据源到IOC中 -->
<bean class="org.apache.commons.dbcp2.BasicDataSource" name="ds">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/emps?useUnicode=true&characterEncoding=utf8" />
<property name="username" value="root" />
<property name="password" value="123456" />
<property name="initialSize" value="2"/>
<property name="maxTotal" value="20"/>
<property name="maxIdle" value="2"/>
<property name="minIdle" value="2"/>
<property name="maxWaitMillis" value="60000"/>
</bean>
<!-- 定义DBUtil -->
<bean class="com.yq.sp02.util.DBUtil" name="dbUtil" autowire="byName"></bean>
<!-- 定义EmpDao -->
<bean class="com.yq.sp02.dao.impl.EmpDaoImpl" name="empDao" autowire="byType"/>
<!-- 定义EmpService -->
<bean class="com.yq.sp02.service.impl.EmpServiceImpl" name="empService" autowire="byType"/>
</beans>
方式二:使用属性文件配置
<?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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置外部属性文件 -->
<context:property-placeholder location="classpath:mysql.dbcp.properties" ignore-unresolvable="true"/>
<!-- 定义数据源到IOC中 -->
<bean class="org.apache.commons.dbcp2.BasicDataSource" name="ds">
<property name="driverClassName" value="${db.dbcp.driverClassName}"/>
<property name="url" value="${db.dbcp.url}"/>
<property name="username" value="${db.dbcp.username}"/>
<property name="password" value="${db.dbcp.password}"/>
<property name="initialSize" value="${db.dbcp.initialSize}"/>
<property name="maxTotal" value="${db.dbcp.maxTotal}"/>
<property name="maxIdle" value="${db.dbcp.maxIdle}"/>
<property name="minIdle" value="${db.dbcp.minIdle}"/>
<property name="maxWaitMillis" value="${db.dbcp.maxWaitMillis}"/>
</bean>
<!-- 定义DBUtil -->
<bean class="com.yq.spWeb01.util.DBUtil" name="dbUtil" autowire="byName"/>
<!-- 定义EmpDao -->
<bean class="com.yq.spWeb01.dao.impl.EmpDaoImpl" name="empDao" autowire="byType"/>
<!-- 定义EmpService -->
<bean class="com.yq.spWeb01.service.impl.EmpServiceImpl" name="empService" autowire="byType"/>
</beans>
web应用获取IOC容器
1、 在web.xml中配置
<!-- 配置监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置监听参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:iolCont.xml</param-value>
</context-param>
2、获取IOC容器对象
<!-- 获取IOC容器对象 -->
WebApplicationContext iocCont = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
iocCont.xml切记在WEB-INF中
代理对象
代理对象的类要实现接口
package com.yq.spWeb01.util;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class Test {
public static void main(String[] args) {
// 原对象
// TestService service = new TestServiceImpl();
// 代理对象
// TestService proxyService = (TestService)Proxy.newProxyInstance(
// TestServiceImpl.class.getClassLoader(),
// TestServiceImpl.class.getInterfaces(),
// new InvocationHandler() {
//
// @Override
// public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//
// try {
// System.out.println("开启事务。。。");
//
// Object result = method.invoke(service, args);
//
// System.out.println("提交事务 。。。");
// } catch (Exception e) {
// System.out.println("回滚事务。。。");
// } finally {
// System.out.println("关闭连接。。");
// }
// return null;
// }
// });
// 用对象
//TestService proxyService = Test.<TestService>getProxy(service);
// 用实现类 生成对象
//TestService proxyService = Test.<TestServiceImpl>getProxy(TestServiceImpl.class);
// 用类名字生成对象
TestService proxyService = Test.<TestServiceImpl>getProxy("com.yq.spWeb01.util.TestServiceImpl");
Object result = proxyService.getEmpById(100);
System.out.println(result);
}
// 给一个对象生成代理
public static <T> T getProxy(final Object obj) {
T proxyService = (T)Proxy.newProxyInstance(
obj.getClass().getClassLoader(),
obj.getClass().getInterfaces(),
( proxy, method, args) -> {
try {
System.out.println("开启事务。。。");
Object result = method.invoke(obj, args);
System.out.println("提交事务 。。。");
return result;
} catch (Exception e) {
System.out.println("回滚事务。。。");
} finally {
System.out.println("关闭连接。。");
}
return null;
}
);
return proxyService;
}
// 只有类(实现类 )生成对象
public static <T> T getProxy(Class<T> cls) {
try {
return Test.<T>getProxy(cls.newInstance());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
// 用类名字
public static <T> T getProxy(String classPath) {
try {
Class cls = Class.forName(classPath);
return Test.<T>getProxy(cls);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}