//通过WEB容器得到Spring上下文
ServletContext servletContext = request.getSession().getServletContext();
WebApplicationContext context1 = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
TestService s1 = (TestService) context1.getBean("TestService");//通过文件系统得到Spring上下文
ApplicationContext context2 = new ClassPathXmlApplicationContext("applicationContext.xml");
TestService s2 = (TestService) context2.getBean("TestService");System.out.println(s1==s2);最后结果是false,配置TestService时scope="singleton"。哎,我也是没有办法要通过两种方式得到Spring上下文,这是一个WEB项目,但还是一个Socket服务端,问题就出来了~~我想请问大家:
1、有没有什么办法,能使 s1==s2 ?
2、Spring不能整合Socket么?谢谢大伙了~~
ServletContext servletContext = request.getSession().getServletContext();
WebApplicationContext context1 = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
TestService s1 = (TestService) context1.getBean("TestService");//通过文件系统得到Spring上下文
ApplicationContext context2 = new ClassPathXmlApplicationContext("applicationContext.xml");
TestService s2 = (TestService) context2.getBean("TestService");System.out.println(s1==s2);最后结果是false,配置TestService时scope="singleton"。哎,我也是没有办法要通过两种方式得到Spring上下文,这是一个WEB项目,但还是一个Socket服务端,问题就出来了~~我想请问大家:
1、有没有什么办法,能使 s1==s2 ?
2、Spring不能整合Socket么?谢谢大伙了~~
你要TestService s1 = (TestService) context1.getBean("TestService");
TestService s2 = (TestService) context1.getBean("TestService");
那么s1、s2肯定是一个呀
return beanFactory.getBean(name);
}
两个对象的相等不是这么比较的,你这样比较的相当于
对应的内存地址一致才能为true.
改为
s1.equal(s2);
2、Spring不能整合Socket么?
可以你看下:
spring的 mail包功能强大package cn.ssh.struts.util;import org.springframework.mail.MailException;import org.springframework.mail.MailSender;import org.springframework.mail.SimpleMailMessage;public class SendMail {private MailSender mailSender;private SimpleMailMessage mailMessage;public SendMail() { }public SimpleMailMessage getMailMessage() { return mailMessage;}public void setMailMessage(SimpleMailMessage mailMessage) { this.mailMessage = mailMessage;}public MailSender getMailSender() { return mailSender;}public void setMailSender(MailSender mailSender) { this.mailSender = mailSender;}public void sendMail() { SimpleMailMessage message = new SimpleMailMessage(mailMessage); //设置email内容, message.setText("Hello 我是杨占辉.这是一个用Spring发送的测试邮件."); try { mailSender.send(message); } catch (MailException e) { // TODO Auto-generated catch block System.out.println("O . 发送Email失败了."); e.printStackTrace(); }}}2.在applicationContext.xml中进行相应的如下配置:《!-- ******************************** 以下为邮件自动发送示例的配置 ********************************** --》《!-- mailSender --》《bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"》 《property name="host"》 《value》smtp.163.com《/value》 《/property》 《property name="javaMailProperties"》 《props》 《prop key="mail.smtp.auth"》true《/prop》《!-- 如果要使用用户名和密码验证,这一步需要 --》 《prop key="mail.smtp.timeout"》25000《/prop》 《/props》 《/property》 《property name="username"》 《value》yangzhanhui《/value》 《/property》 《property name="password"》 《value》yangzhanhui《/value》 《/property》《/bean》《!-- 简单的message --》《bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage"》 《property name="to"》 《value》[email protected]《/value》 《/property》 《property name="from"》 《value》[email protected]《/value》 《/property》 《property name="subject"》 《!-- Email 标题 --》 《value》你好,朋友.《/value》 《/property》《/bean》《!-- sendMail --》《bean id="sendMail" class="cn.ssh.struts.util.SendMail"》 《property name="mailMessage"》 《ref bean="mailMessage"/》 《/property》 《property name="mailSender"》 《ref bean="mailSender"/》 《/property》《/bean》3.在Action中调用邮件自动发送业务,代码如下:public class UserAction extends Action {/** Generated Methods*/private User user;private UserService userService;private SendMail sendMail;/*** @return the sendMail*/public SendMail getSendMail() { return sendMail;}/*** @param sendMail the sendMail to set*/public void setSendMail(SendMail sendMail) { this.sendMail = sendMail;}public User getUser() { return user;}public void setUser(User user) { this.user = user;}public UserService getUserService() { return userService;}public void setUserService(UserService userService) { this.userService = userService;}/*** Method execute* * @param mapping* @param form* @param request* @param response* @return ActionForward*/public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UserForm userForm = (UserForm) form; user.setUsername(userForm.getUsername()); user.setPassword(userForm.getPassword()); if (userService.login(user)) { sendMail.sendMail(); return mapping.findForward("success"); } else { return mapping.findForward("fail"); } }}
楼主的想法是可以理解的,虽然都是通过spring获取同一个类的对象,但确实属于不同的两个对象。
原因是存在两个spring的上下文环境。当web应用启动的时候,spring会加载配置文件件并实例化对象,在这个上下文中获取的肯定是同一个对象。下面的代码运行时又会加载一次spring的配置文件,和web中的上下文是完全没关系的,所以又会创建新的对象
ApplicationContext context2=new ClassPathXmlApplicationContext("applicationContext.xml");
TestService s2= (TestService) context2.getBean("TestService");
this.beanFactory = beanFactory;
} public static Object getBean(String name) {
return beanFactory.getBean(name);
} public static <T> T getBean(String name, Class<T> clazz) {
return (T)beanFactory.getBean(name);
} public static boolean containsBean(String name) {
return beanFactory.containsBean(name);
}
}在 Spring 配置中把这个类配置成一个 bean,在启动完成之后,你就可以在类中使用 ClassBeanFactory.getBean() 方法了,这样可以获得与 Servlet 上下文相同的一个实例。
就你列的两种来看
第一种是从web容器 取得上下文第二种是读文件生成的当然不一样了
public class Listener_SpringContext implements ServletContextListener{
Log log = LogFactory.getLog(Listener_SpringContext.class);
public static ApplicationContext context=null; public void contextInitialized(ServletContextEvent arg0) {
log.info("获取spring上下文");
this.context=WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
log.info("保存spring上下文:context="+context);
}