多线程中使用spring容器中的对象

多线程中使用spring容器中的对象,第1张

创建一个自己的类用来实现ApplicationContextAware,然后通过类名或者实例名称来获取spring容器中的实例。

@Component

public class SpringContextUtilsimplements ApplicationContextAware {

private static ApplicationContextcontext;

public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {

context=applicationContext;

}

public static T getBeanByClass(Class c){

return contextgetBean(c);

}

public static Object getBeanByName(String name){

return contextgetBean(name);

}

}

这里一定要有@Component注解,不然还是取不到spring容器里的实例,取到的值是null。

这样就可以在多线程中使用了:

MonitorTrafficService service = SpringContextUtilsgetBeanByClass(MonitorTrafficServiceclass);

通过xml配置文件 bean配置在xml里面,spring提供多种方式读取配置文件得到ApplicationContext 第一种方式:FileSystemXmlApplicationContext 通过程序在初始化的时候,导入Bean配置文件,然后得到Bean实例: ApplicationContext ac = new FileSystemXmlApplicationContext(applicationContextxml) acgetBean(beanName); 第二种方式:WebApplicationContextUtil 在B/S系统中,通常在webxml初始化bean的配置文件,然后由WebAppliCationContextUtil得到ApplicationContext例如: ApplicationContext ctx = WebApplicationContextUtilsgetRequiredWebApplicationContext(ServletContext sc); ApplicationContext ctx =

ThreadcurrentThread()是得到当前运行的线程名,如果你不停的切换线程自然会变,还有ThreadLocal也是会跟着变的。 你要永远记着,每个线程都有自己对应的ThreadLocal,TL只是Thread的局部变量。

前几天,在sf这里也提过这个问题,但是仍未得到解决,上一个问题是这个在Service中注入Dao不成功,Dao为null。但根据大虾们的回答,我想再理顺一下这个项目的逻辑,免得各位看着都不懂我在说什么。本人是spring小白,整个项目环境搭建是前辈写下的,我只是根据他的方法来添加修改,所以有很多框架上的逻辑我并不是看得很懂。

我想实现的是——用多线程通过socket不断获取从客户端发送过来的消息,并对消息联系JDBC进行分析。

PS代码部分都只截取了重要的部分

我先放一下其中一个service和dao的实现:

1)ConcentratorService:

public interface ConcentratorService {

public List<Concentrator> getConcentratorListByMacAddresses(String[] macAddr) throws Exception;

}

2)ConcentratorServiceImpl:

@Service("ConcentratorService")

public class ConcentratorServiceImpl implements ConcentratorService{

@Autowired

ConcentratorDao concentratorDao;

public Concentrator findConcentratorByCaddress(String caddress) throws Exception{

// TODO Auto-generated method stub

return concentratorDaofindConcentratorByCaddress(caddress);

}

3)ConcentratorDao

public interface ConcentratorDao {

public List<Concentrator> getConcentratorListByMacAddresses(String[] macAddr) throws Exception;

}

4)ConcentratorDaoImpl

public class ConcentratorDaoImpl implements ConcentratorDao {

@Autowired

SessionFactory sessionFactory;

Session session = null;

Transaction tx = null;

public void setSessionFactory(SessionFactory sessionFactory) {

thissessionFactory=sessionFactory;

}

@Override

public List<Concentrator> getConcentratorListByMacAddresses(String[] macAddr) throws Exception {

session = sessionFactoryopenSession();

tx = sessionbeginTransaction();

StringBuffer sb = new StringBuffer("from Concentrator where caddress in (");

for(int i=0; i<macAddrlength; i++){

if(i==macAddrlength-1){

sbappend(")");

}else{

sbappend(",");

}

}

Query query=sessioncreateQuery(sbtoString());

for(int i=0; i<macAddrlength; i++){

querysetParameter(i, macAddr[i]);

}

@SuppressWarnings("unchecked")

List<Concentrator> clist=querylist();

txcommit();

sessionclose();

return clist;

}

}

这里再放一下datasourcexml,这个xml是用来配置bean的。

<beans xmlns=">

在初始化时保存ApplicationContext对象

代码:

ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContextxml");

acgetBean("beanId");

说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。

getBean是用来获取applicationContextxml文件里bean的,()写的是bean的id。

一种是singleton,一种是prototype,默认的是singleton,这种定义的bean实例的作用是与spring的容器一致的,只有spring容器初始化,调用getBean得到的singleton实例始终是同一个bean的实例spring定义的bean有两种作用范围,是每当调用getBean得到实例的时候spring都会new一个实例来给你,而prototype的实例。

以上就是关于多线程中使用spring容器中的对象全部的内容,包括:多线程中使用spring容器中的对象、spring怎么获取全部的bean、thread.currentthread有没有办法获取spring的bean等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9814357.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-02
下一篇2023-05-02

发表评论

登录后才能评论

评论列表(0条)

    保存