1.讲Action纳入spring的IOC控制 <!-- 采用注解方式自动扫描装配 --> <context:component-scan base-package="com.wetalk.*" /> 2.引入jar包 struts2-spring-plugin-2.2.1.jar 3.在Action中使用@Autowired引入即可…
很多情况在进行Web开发的时候需要自己手写Servlet来完成某些功能,而servlet有需要注入Spring容器中的某些bean,这是每次都要手动获取比较麻烦,这里有一个解决方案,只需要写一个servlet的基类,其它的类只需要集成基类后,便可以想action那样进行注入了. 基类Servlet代码如下: public class BaseServlet extends HttpServlet { private static final long serialVersionUID = 1L;…
  错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xx' is defined   错误的一般解决办法: 1.看xxbean是否已经注入,或者得到的bean名字错误. 2.看spring的配置文件<context:component-scan base-package="com.xx"></context:component-scan>是否扫…
无法注入原因: 有的时候我们有一些类并不想注入Spring容器中,有Spring容器实例化,但是我们又想使用Spring容器中的一些对象,所以就只能借助工具类来获取了 工具类: package com.mikey.design.utils; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import o…
问题:在filter和interceptor中经常需要调用Spring的bean,filter也是配置在web.xml中的,请问一下这样调用的话,filter中调用Spring的某个bean,这个bean一定存在吗?现在总是担心filter调用bean的时候,bean还没被实例化? 答案:因为spring bean.filter.interceptor加载顺序与它们在 web.xml 文件中的先后顺序无关.即不会因为 filter 写在 listener 的前面而会先加载 filter.最终得出…
在Spring中使用Quartz有两种方式实现:第一种是任务类继承QuartzJobBean,第二种则是在配置文件里定义任务类和要执行的方法,类和方法可以是普通类.很显然,第二种方式远比第一种方式来的灵活. 测试环境 Spring3 M2 quartz-2.1.7 我们要达到这样的效果 public class CancelUnpaidOrderTask implements Job { @Autowired private AppOrderService orderService; @Over…
       在项目中有时需要根据需要在自己new一个对象,或者在某些util方法或属性中获取Spring Bean对象,从而完成某些工作,但是由于自己new的对象和util方法并不是受Spring所管理的,如果直接在所依赖的属性上使用@Autowired就会报无法注入的错误,或者是没报错,但是使用的时候会报空指针异常.总而言之由于其是不受IoC容器所管理的,因而无法注入.         Spring提供了两个接口:BeanFactoryAware和ApplicationContextAwar…
新手注意的问题 package cn.ryq.web.controller; import cn.ryq.domain.company.Company;import cn.ryq.service.company.CompanyService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springf…
1.在spring中配置如下<context:spring-configured/>     <context:load-time-weaver aspectj-weaving="autodetect"/> 2.spring bean如下 用@configurable进行注解,这样我们可以直接new RealTimeStatisticTask,那么RealTimeStaticDao也能被正常注入了. 3.将spring-instrument-tomcat-4.1…
在有些情况下需要使用main使用Spring bean,但是main方法启动并没有托管给Spring管理,会导致bean失败,报空指针异常. 可以使用 ClassPathXmlApplicationContext 加载配置文件,获获取bean: public static void main(String[] args) { @SuppressWarnings("resource") ClassPathXmlApplicationContext context = new ClassP…