IOC配置数据源、使用属性文件配置、代理对象

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&amp;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;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李鑫海。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值