多线程Spring注入失败

    在对Spring进行测试(多线程环境)时发现ExpertBlogService一直注入失败,后来发现多线程环境下Spring是不会对新线程进行管理的,所以需要我们手动加载配置文件,另外将加载部分部分放进静态块中防止多次加载,代码如下:

    private static ApplicationContext ctx = null;

    private static ExpertBlogService expertBlogService;

    private static ExpertBlogMapper expertBlogMapper;

    //When you use @Autowired in a new thread,you will get a nullpointerException
    static {
        ctx = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
        expertBlogMapper = (ExpertBlogMapper) ctx.getBean("expertBlogMapper");
        expertBlogService = new ExpertBlogServiceImpl(expertBlogMapper);
    }

spring注入失败??

03-23

为什么我的spring setter不成功?rnrn我在业务类是这样配置的rnrn[code=XML]rn rn rnrnrnrnrn rnrnrn[/code]rnrn业务类的代码:rnrn[code=Java]rnrnpublic class UserServiceImp implements IUserService rn private IUserDAO userDAO;rn rn private MyValidator userValidator ;rnrn public IUserDAO getUserDAO() rn return userDAO;rn rnrn public void setUserDAO(IUserDAO userDAO) rn this.userDAO = userDAO;rn rnrn public MyValidator getUserValidator() rn return userValidator;rn rnrn public void setUserValidator(MyValidator userValidator) rn this.userValidator = userValidator;rn rn rn //.........省略一些方法rn public User getUserByName(String username) throws MyBusinessException rn User user = null;rn try //从数据库中得到用户实例rn user = userDAO.getUserByName(username);rn //对用户实例进行校验,如果校验结果不符合业务逻辑,则抛出异常rn ValidResult validResutl = userValidator.validate(user);rn rn if(validResutl.isError())rn throw new MyBusinessException(validResutl.getErrorMsg());rn rn rn catch (MyDAOException cre) rn throw new MyBusinessException(cre);rn rn return user;rn rnrn[/code]rnrn但是当每次通过浏览器调用这个业务的时候总是报空指针异常,后来我把UserServiceImp 类的“[color=#FF0000]private MyValidator rnrnuserValidator ;[/color]”;改为[color=#FF0000]private CreditValidator userValidator = new UserValid() ;[/color]就正常了,可知为何???rn难道spring不认得我自己写的校验类?rnrnrnrn

求解,spring注入失败

03-24

今天做项目碰到一个问题,弄了半天也没解决,后来为此单独写了一个测试类来测试,结果还是注入失败。rn这是测试actionrn[code=java]rnpackage com.whm.test;rnrnimport com.opensymphony.xwork2.ActionSupport;rnrnpublic class TestUser extends ActionSupportrn /**rn * rn */rn private static final long serialVersionUID = 5004677857637572313L;rn private User userInfo;rn rn public String execute()rn System.out.println("userinfo = " + userInfo);rn return SUCCESS;rn rnrn /**rn * @return the userInforn */rn public User getUserInfo() rn return userInfo;rn rnrn /**rn * @param userInfo the userInfo to setrn */rn public void setUserInfo(User userInfo) rn this.userInfo = userInfo;rn rnrnrn[/code]rnspring配置文件rn[code=text]rnrnrn rn rnrn rn rn rnrnrn[/code]rnstruts2配置文件rn[code=text]rnrnrnrnrn rn rn /index.jsprn rn rnrn[/code]rnrn可是启动web服务后,测试到userInfo的值为空,注入失败了。rn如果不启动web服务,直接new一个ClassPathXmlApplicationContext来获取,也是可以注入的rn测试类如下rn[code=java]rnpackage com.whm.test;rnrnimport org.springframework.context.support.ClassPathXmlApplicationContext;rnrnpublic class Test rnrn public static void main(String[] args) rn ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("application.xml");rn TestUser test = (TestUser)app.getBean("testUser");rn test.execute();rn rn rnrnrn[/code]rnrn而且如果把spring配置文件中的user改成userInfo,和类中的名称一致,则可以注入。这个问题已经弄的我头都大了,就是不知道哪里有问题啊,各位大神帮忙分析一下,谢谢了

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭