java类读取spring容器中的bean

spring配置文件中需要配置

<bean id="springContextHolder" class="com.xxx.kf.sso.tool.SpringContextHolder"
lazy-init="false" />


<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="${jdbc.url}"></property>
  </bean>


package com.xxx.kf.sso.tool;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringContextHolder implements ApplicationContextAware,
		DisposableBean {

	private static ApplicationContext applicationContext = null;

	/**
	 * 实现ApplicationContextAware接口, 注入Context到静态变量中.
	 */
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) {
		SpringContextHolder.applicationContext = applicationContext;
	}

	/**
	 * 实现DisposableBean接口, 在Context关闭时清理静态变量.
	 */
	@Override
	public void destroy() throws Exception {

		applicationContext = null;
	}

	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		return (T) applicationContext.getBean(name);
	}

	public static <T> T getBean(Class<T> requiredType) {
		return applicationContext.getBean(requiredType);
	}

}

使用它的java类

package com.xxx.kf.sso.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.xiaomi.kf.sso.model.SsoUser;
import com.xiaomi.kf.sso.model.SsoUserBind;
import com.xiaomi.kf.sso.model.SsoUserLogin;
import com.xiaomi.kf.sso.tool.SpringContextHolder;


public class SsoDAO  {
	
	private static SsoDAO instance = new SsoDAO();
	
	private DataSource ds;

	private SsoDAO() {
		ds = SpringContextHolder.getBean("ds");
	}

	public static SsoDAO getInstance() {
		return instance;
	}		

	public int insertSsoUser(SsoUser ssoUser){
		String sql = "INSERT INTO sso_user(ssoId,name) VALUES (?, ? )";
		PreparedStatement ps = null;
		Connection conn = null;
		int result = 0; 
		try {
			conn = ds.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, ssoUser.getSsoId());
			ps.setString(2, ssoUser.getName());
			result = ps.executeUpdate();
		
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {   
                if (ps != null)  
                    ps.close();  
                if (conn != null)  
                    conn.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
		}
		return result;
	}
	
	public int insertSsoUserBind(SsoUserBind ssoUserBind) throws SQLException{
		String sql = "INSERT INTO sso_user_bind(ssoId,bindType,bindInfo) VALUES (?, ?, ? )";
		PreparedStatement ps = null;
		Connection conn = null;
		int result = 0; 
		try {
			conn = ds.getConnection(); 
			ps = conn.prepareStatement(sql);
			ps.setString(1, ssoUserBind.getSsoId());
			ps.setInt(2, ssoUserBind.getBindType());
			ps.setString(3, ssoUserBind.getBindInfo());
			result = ps.executeUpdate();
		
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {   
                if (ps != null)  
                    ps.close();  
                if (conn != null)  
                    conn.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }
		}
		return result;
	}
	
	public int insertSsoUserLogin(SsoUserLogin ssoUserLogin) throws SQLException{
		String sql = "INSERT INTO sso_user_login(ssoId,mobile,staff_number,pwd) VALUES (?, ?, ?, ? )";
		PreparedStatement ps = null;
		Connection conn = null;
		int result = 0; 
		try {
			conn = ds.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setString(1, ssoUserLogin.getSsoId());
			ps.setString(2, ssoUserLogin.getMobile());
			ps.setString(3, ssoUserLogin.getStaffNumber());
			ps.setString(4, ssoUserLogin.getPwd());
			result = ps.executeUpdate();
		
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {   
                if (ps != null)  
                    ps.close();  
                if (conn != null)  
                    conn.close();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }
		}
		return result;
	}
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值