Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java @自动连线在春天失败_Java_Spring_Spring Mvc_Dependency Injection - Fatal编程技术网

Java @自动连线在春天失败

Java @自动连线在春天失败,java,spring,spring-mvc,dependency-injection,Java,Spring,Spring Mvc,Dependency Injection,各位, 我正在尝试使用SpringDI和MVC(3.2.x版)来设置项目。我尝试注入服务实现,但在某个时候失败了。没有抛出异常。只有服务对象在预期的位置为null。 下面是代码 package com.test.domain.web.controller; ......... @Controller @RequestMapping(value = "/system/**") public class InformationController { @RequestMapping(value =

各位,
我正在尝试使用SpringDI和MVC(3.2.x版)来设置项目。我尝试注入服务实现,但在某个时候失败了。没有抛出异常。只有服务对象在预期的位置为null。 下面是代码

package com.test.domain.web.controller;
.........
@Controller
@RequestMapping(value = "/system/**")
public class InformationController {
@RequestMapping(value = "/classing/{id}", method = RequestMethod.GET)
public ModelAndView getBaleClassing(@PathVariable String id, Model model, HttpServletRequest request) {
    LOG.info("In the system classing...");
    InformationModel informationModel = new InformationModel();
    model.addAttribute("informationModelVO",informationModel.getResult(id));
    return new ModelAndView(SYSTEM_CLASSING_TILE);
}

}

这是业务的模型类。我还尝试在setter方法中注释@Autowired。但是没有运气

package com.test.domain.classing.model;
.........
public class InformationModel  {

@Autowired
InformationService informationService;

public  InformationVO getResult(String id) {
    InformationVO informationVO = null;
    try {
        informationVO = informationService.getInformationById(id); //informationService is null here
    } catch (ServiceException e) {
        LOG.error("Exception: ", e);
    }
    return informationVO;
}

public InformationService getInformationService() {
    return informationService;
}

public void setInformationService(InformationService informationService) {
    this.informationService = informationService;
}
}

这是我的服务实现,是唯一符合条件的候选服务。我依靠Spring进行实例化,我自己不会在任何地方实例化服务或DAO实现。

package com.test.domain.classing.service;
.......
public class InformationServiceImpl implements InformationService {

    @Autowired
    private InformationDAO informationDAO;

    public InformationServiceImpl() {
        LOG.debug("In the InformationServiceImpl default constructor....");
    }

    public InformationVO getResult(String id) throws ServiceException {
        InformationVO informationVO = informationDAO.getResult(id);
        return informationVO;
    }

    public InformationDAO getInformationDAO() {
        return informationDAO;
    }

    public void setInformationDAO(InformationDAO informationDAO) {
        this.informationDAO = informationDAO;
    }
}
这是spring配置(包含服务和dao)。我没有在spring配置中添加InformationModel,因为我每次都需要新实例

applicationContext.xml


。。。。。。。。。。
..........
mvcContext.xml




从日志中,我可以说DAO和服务的实现是实例化的。但是没有自动布线发生。i、 e.将DAO注入服务或将服务注入模型。 在spring尝试注入时有没有调试的方法(在我的例子中,如果有)


非常感谢您的指点。提前感谢。

没有异常意味着根本没有进行Autowire尝试,这反过来意味着您尝试Autowire的类不是由Spring管理的。简单的规则是,如果bean不是由Spring创建的,那么Spring的任何东西(包括自动连接)都不会起作用。
如果出于某种原因,您每次都需要一个新的
InformationModel
实例,那么您最好不要依赖Spring,我会在创建它时将所需的参数传递给
InformationModel
的构造函数。

没有异常意味着根本没有进行自动连线尝试,这反过来意味着您尝试自动连线的类不是由Spring管理的。简单的规则是,如果bean不是由Spring创建的,那么Spring的任何东西(包括自动连接)都不会起作用。
如果出于某种原因,您每次都需要
InformationModel
的新实例,那么你最好不要依赖Spring,我会在你创建InformationModel时将所需参数传递给它的构造函数。

Spring@Autowired injection将只在Spring管理的组件中工作。你的InformationModel不是Spring管理的组件,你试图使用new,所以它失败了。如果您想使用基于注释的配置,那么您可以简单地使用@service注释服务,使用Daos注释服务
@存储库或@Component,而不是xml配置中的bean定义

Spring@Autowired injection将仅在Spring托管组件中工作。您的InformationModel不是Spring托管组件,您尝试使用new实例化,因此失败。如果您希望使用基于注释的配置,则可以使用@service和DAO对服务进行注释 @存储库或@Component,而不是xml配置中的bean定义

保罗是对的

试试这个:

@Controller
@RequestMapping(value = "/system/**")
public class InformationController {

@Autowired
InformationModel informationModel;

@RequestMapping(value = "/classing/{id}", method = RequestMethod.GET)
public ModelAndView getBaleClassing(@PathVariable String id, Model model, HttpServletRequest request) {
    LOG.info("In the system classing...");
    model.addAttribute("informationModelVO",informationModel.getResult(id));
    return new ModelAndView(SYSTEM_CLASSING_TILE);
}

}
以及

还有这个

@Service
public class InformationServiceImpl implements InformationService {
...
}
祝你好运

保罗是对的

试试这个:

@Controller
@RequestMapping(value = "/system/**")
public class InformationController {

@Autowired
InformationModel informationModel;

@RequestMapping(value = "/classing/{id}", method = RequestMethod.GET)
public ModelAndView getBaleClassing(@PathVariable String id, Model model, HttpServletRequest request) {
    LOG.info("In the system classing...");
    model.addAttribute("informationModelVO",informationModel.getResult(id));
    return new ModelAndView(SYSTEM_CLASSING_TILE);
}

}
以及

还有这个

@Service
public class InformationServiceImpl implements InformationService {
...
}

祝你好运

您需要为模型(
@Component
)和服务(
@service
)类添加注释,以便Spring管理它们。非常感谢大家!我将InformationModel制作为原型范围的bean,这样每次都可以创建它。现在它可以正常工作了。您需要为模型(
@Component
)和服务(
@service
)类添加注释,以便Spring管理它们。非常感谢大家!我将InformationModel制作为原型范围的bean,这样每次都可以创建它。现在它可以正常工作了。我不同意您关于避免在配置中混合注释和XML的建议。当Spring配置的这一方面在所有Spring上下文中都是相同的时,注释是最合适的(例如,对于给定的类型,您总是只有一个bean,所以您将它注入@Autowired)。XML允许您在不同的Spring配置之间进行更改(例如,在JUnit测试和生产之间,或者在两个不同的单元测试之间)。默认情况下,我使用注释,只有当我发现在不同的Spring上下文中需要不同的行为时,才会将给定的方面移动到XML。也许我在问一个奇怪的问题。我是否可以使用注释来保持代码简短,并使用仅适用于JUnit的xml配置?比如,我可以在XML配置中单独保留DAO配置,以便在运行JUnitI时可以切换它。我不同意您的建议,即避免在配置中混合注释和XML。当Spring配置的这一方面在所有Spring上下文中都是相同的时,注释是最合适的(例如,对于给定的类型,您总是只有一个bean,所以您将它注入@Autowired)。XML允许您在不同的Spring配置之间进行更改(例如,在JUnit测试和生产之间,或者在两个不同的单元测试之间)。默认情况下,我使用注释,只有当我发现在不同的Spring上下文中需要不同的行为时,才会将给定的方面移动到XML。也许我在问一个奇怪的问题。我是否可以使用注释来保持代码简短,并使用仅适用于JUnit的xml配置?比如,我可以在XML配置中单独保留DAO配置,以便在运行JUnit时可以对其进行切换
@Service
public class InformationServiceImpl implements InformationService {
...
}