1 但是我要通过点击右键main方法 得到hibernate链接。拿到connection?
2 大家说我main方法里面有3个业务判断怎么通过Spring管理事务?最好给个代码,因为我通过某个设备触发这个main。
请大哥们回答一上这两个问题。
12 个解决方案
#1
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"ApplicationContext-jotm.xml"});
BeanFactory factory = (BeanFactory) context;
UserService userService = (UserService)factory.getBean("userService");
}
还是可以由Spirng去管理事务
#2
JAVA所以的程序都是从main开始的。
如果你想从main中拿到hibernate的链接,只要将hibernate的配置文件,已经spring的配置文件放到classpath中就可以了。
如果你想从main中拿到hibernate的链接,只要将hibernate的配置文件,已经spring的配置文件放到classpath中就可以了。
#3
2楼的大哥,我现在又很多ApplicationContext-XX-.xml 我有一个总的plugins.xml
userInfoAction = (userInfoAction )factory.getBean("userAction");
我tomcat服务器现在是启动的等于重新加载了一下plugins.xml
每次执行main的时候他都会重新加载吗?这样会不会影响性能问题?
#4
如果从性能上考虑,你就需要使用单例模式生成ApplicationContext context对象
#5
#6
怎么通过单利模式呢?
#7
如果从性能上考虑,你就需要使用单例模式生成ApplicationContext context对象
从性能上说没有比让容器自己管理再好的了
你还要自己拿连接,你就算是把连接close()调用了 你能确定他就真的关掉了嘛
自己用JDBC管理连接池是很复杂的 要不要沉迷于自己什么都能干
从性能上说没有比让容器自己管理再好的了
你还要自己拿连接,你就算是把连接close()调用了 你能确定他就真的关掉了嘛
自己用JDBC管理连接池是很复杂的 要不要沉迷于自己什么都能干
#8
参考设计模式例子。。。。。。。。将里面内容换成你的内容
#9
/**
* 得到对象
*
* @return
*/
public static synchronized IAccountRegService getConn() {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"plugins.xml"});
BeanFactory factory = (BeanFactory) context;
IAccountRegService userService = (IAccountRegService)factory.getBean("accountRegServiceImpl");
return userService;
}
行吗?
* 得到对象
*
* @return
*/
public static synchronized IAccountRegService getConn() {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"plugins.xml"});
BeanFactory factory = (BeanFactory) context;
IAccountRegService userService = (IAccountRegService)factory.getBean("accountRegServiceImpl");
return userService;
}
行吗?
#10
private static ConnDB obj = null;
public static ConnDB getInstance() {
if (obj == null) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"plugins.xml"});
BeanFactory factory = (BeanFactory) context;
IAccountRegService userService = (IAccountRegService)factory.getBean("accountRegServiceImpl");
obj = new ConnDB();
}
return obj;
}
public static ConnDB getInstance() {
if (obj == null) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"plugins.xml"});
BeanFactory factory = (BeanFactory) context;
IAccountRegService userService = (IAccountRegService)factory.getBean("accountRegServiceImpl");
obj = new ConnDB();
}
return obj;
}
#11
/**
* 单例模式
*/
private final static ConnDB obj = new ConnDB();
private final static ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"plugins.xml"});
/**
* 单例模式
* description:
* @return
* @author sunrise
*/
public static ConnDB getInstance() {
return obj;
}
/**
* 得到对象
*
* @return
*/
public static IAccountRegService getConn() {
BeanFactory factory = (BeanFactory) context;
IAccountRegService userService = (IAccountRegService)factory.getBean("accountRegServiceImpl");
return userService;
}
* 单例模式
*/
private final static ConnDB obj = new ConnDB();
private final static ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"plugins.xml"});
/**
* 单例模式
* description:
* @return
* @author sunrise
*/
public static ConnDB getInstance() {
return obj;
}
/**
* 得到对象
*
* @return
*/
public static IAccountRegService getConn() {
BeanFactory factory = (BeanFactory) context;
IAccountRegService userService = (IAccountRegService)factory.getBean("accountRegServiceImpl");
return userService;
}
#12
package com.hongxun.sp.dao;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hongxun.sp.service.account.yxw.IAccountRegService;
public class ConnDB {
/**
* 单例模式
*/
private final static ConnDB obj = new ConnDB();
private final static ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"plugins.xml"});
/**
* 单例模式
* description:
* @return
* @author sunrise
*/
public static ConnDB getInstance() {
return obj;
}
/**
* 得到对象
*
* @return
*/
public static IAccountRegService getConn() {
BeanFactory factory = (BeanFactory) context;
IAccountRegService userService = (IAccountRegService)factory.getBean("accountRegServiceImpl");
return userService;
}
}
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hongxun.sp.service.account.yxw.IAccountRegService;
public class ConnDB {
/**
* 单例模式
*/
private final static ConnDB obj = new ConnDB();
private final static ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"plugins.xml"});
/**
* 单例模式
* description:
* @return
* @author sunrise
*/
public static ConnDB getInstance() {
return obj;
}
/**
* 得到对象
*
* @return
*/
public static IAccountRegService getConn() {
BeanFactory factory = (BeanFactory) context;
IAccountRegService userService = (IAccountRegService)factory.getBean("accountRegServiceImpl");
return userService;
}
}
#1
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"ApplicationContext-jotm.xml"});
BeanFactory factory = (BeanFactory) context;
UserService userService = (UserService)factory.getBean("userService");
}
还是可以由Spirng去管理事务
#2
JAVA所以的程序都是从main开始的。
如果你想从main中拿到hibernate的链接,只要将hibernate的配置文件,已经spring的配置文件放到classpath中就可以了。
如果你想从main中拿到hibernate的链接,只要将hibernate的配置文件,已经spring的配置文件放到classpath中就可以了。
#3
2楼的大哥,我现在又很多ApplicationContext-XX-.xml 我有一个总的plugins.xml
userInfoAction = (userInfoAction )factory.getBean("userAction");
我tomcat服务器现在是启动的等于重新加载了一下plugins.xml
每次执行main的时候他都会重新加载吗?这样会不会影响性能问题?
#4
如果从性能上考虑,你就需要使用单例模式生成ApplicationContext context对象
#5
#6
怎么通过单利模式呢?
#7
如果从性能上考虑,你就需要使用单例模式生成ApplicationContext context对象
从性能上说没有比让容器自己管理再好的了
你还要自己拿连接,你就算是把连接close()调用了 你能确定他就真的关掉了嘛
自己用JDBC管理连接池是很复杂的 要不要沉迷于自己什么都能干
从性能上说没有比让容器自己管理再好的了
你还要自己拿连接,你就算是把连接close()调用了 你能确定他就真的关掉了嘛
自己用JDBC管理连接池是很复杂的 要不要沉迷于自己什么都能干
#8
参考设计模式例子。。。。。。。。将里面内容换成你的内容
#9
/**
* 得到对象
*
* @return
*/
public static synchronized IAccountRegService getConn() {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"plugins.xml"});
BeanFactory factory = (BeanFactory) context;
IAccountRegService userService = (IAccountRegService)factory.getBean("accountRegServiceImpl");
return userService;
}
行吗?
* 得到对象
*
* @return
*/
public static synchronized IAccountRegService getConn() {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"plugins.xml"});
BeanFactory factory = (BeanFactory) context;
IAccountRegService userService = (IAccountRegService)factory.getBean("accountRegServiceImpl");
return userService;
}
行吗?
#10
private static ConnDB obj = null;
public static ConnDB getInstance() {
if (obj == null) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"plugins.xml"});
BeanFactory factory = (BeanFactory) context;
IAccountRegService userService = (IAccountRegService)factory.getBean("accountRegServiceImpl");
obj = new ConnDB();
}
return obj;
}
public static ConnDB getInstance() {
if (obj == null) {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"plugins.xml"});
BeanFactory factory = (BeanFactory) context;
IAccountRegService userService = (IAccountRegService)factory.getBean("accountRegServiceImpl");
obj = new ConnDB();
}
return obj;
}
#11
/**
* 单例模式
*/
private final static ConnDB obj = new ConnDB();
private final static ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"plugins.xml"});
/**
* 单例模式
* description:
* @return
* @author sunrise
*/
public static ConnDB getInstance() {
return obj;
}
/**
* 得到对象
*
* @return
*/
public static IAccountRegService getConn() {
BeanFactory factory = (BeanFactory) context;
IAccountRegService userService = (IAccountRegService)factory.getBean("accountRegServiceImpl");
return userService;
}
* 单例模式
*/
private final static ConnDB obj = new ConnDB();
private final static ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"plugins.xml"});
/**
* 单例模式
* description:
* @return
* @author sunrise
*/
public static ConnDB getInstance() {
return obj;
}
/**
* 得到对象
*
* @return
*/
public static IAccountRegService getConn() {
BeanFactory factory = (BeanFactory) context;
IAccountRegService userService = (IAccountRegService)factory.getBean("accountRegServiceImpl");
return userService;
}
#12
package com.hongxun.sp.dao;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hongxun.sp.service.account.yxw.IAccountRegService;
public class ConnDB {
/**
* 单例模式
*/
private final static ConnDB obj = new ConnDB();
private final static ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"plugins.xml"});
/**
* 单例模式
* description:
* @return
* @author sunrise
*/
public static ConnDB getInstance() {
return obj;
}
/**
* 得到对象
*
* @return
*/
public static IAccountRegService getConn() {
BeanFactory factory = (BeanFactory) context;
IAccountRegService userService = (IAccountRegService)factory.getBean("accountRegServiceImpl");
return userService;
}
}
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hongxun.sp.service.account.yxw.IAccountRegService;
public class ConnDB {
/**
* 单例模式
*/
private final static ConnDB obj = new ConnDB();
private final static ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"plugins.xml"});
/**
* 单例模式
* description:
* @return
* @author sunrise
*/
public static ConnDB getInstance() {
return obj;
}
/**
* 得到对象
*
* @return
*/
public static IAccountRegService getConn() {
BeanFactory factory = (BeanFactory) context;
IAccountRegService userService = (IAccountRegService)factory.getBean("accountRegServiceImpl");
return userService;
}
}