Java Spring控制器未创建为单例

Java Spring控制器未创建为单例,java,spring,Java,Spring,我有一个Spring控制器,我认为它是根据我调试代码时看到的对象ID多次实例化的 控制器: @Controller @RequestMapping("/services/user") public class UserController{ private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } p

我有一个Spring控制器,我认为它是根据我调试代码时看到的对象ID多次实例化的

控制器:

@Controller
@RequestMapping("/services/user")
public class UserController{

private UserService userService;

public void setUserService(UserService userService) {
    this.userService = userService;
}

public UserService getUserService() {
    return userService;
}

@RequestMapping(method = RequestMethod.POST, value = "/createUser")
public @ResponseBody String createUser(@RequestBody User user) throws UserNotCreatedException {

    try {
        userService.createUser(user);
        return user.getEmail();
    } catch (Exception e) {
        e.printStackTrace();
        throw new UserNotCreatedException("User not created");

    }
}
Spring配置文件:

  <?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- Scans the classpath of this application for @Components to deploy as 
    beans -->
<context:component-scan base-package="com.xyz.controller" />

<context:annotation-config />

<bean id="userController" class="com.xyz.UserController">
    <property name="userService" ref="userService" />
</bean>

<bean id="userService" class="com.xyz.UserServiceImpl">
    <property name="userDao" ref="userDaoMysql" />
</bean>

<bean id="userDaoMysql" class="com.xyz.UserDaoImpl" >
    <property name="dataSource" ref="dataSource"></property>
    <property name="template" ref="jdbcTemplate"></property>
</bean>
   </beans>

当我通过UserController发出请求时,当我意识到userService为null时,我注意到了这个问题。然而当我放置断点时,我看到userService确实在UserController的另一个实例中设置

以@adashr的答案为基础

如果您删除手动配置,那么您现在正在基于注释初始化内容。@Controller注释将正确创建控制器,但现在用户服务中没有任何连接


用户服务需要@Autowired来实例化它,因为spring配置现在是注释驱动的

以@adashr的答案为基础

如果您删除手动配置,那么您现在正在基于注释初始化内容。@Controller注释将正确创建控制器,但现在用户服务中没有任何连接


用户服务需要@Autowired来实例化它,因为spring配置现在是注释驱动的

此线程中的其他注释没有考虑UserController类的包。提供的Spring配置将com.xyz.controller上的组件扫描初始化为基本包。UserController类在com.xyz中看起来不在该类的范围内,因此不会被组件扫描获取,除非这是一个输入错误@user269091-这可能是您的问题

我应该注意到,我不明白为什么控制器不在控制器包中。将控制器移动到那里,将@Autowired添加到private-UserService-UserService行的上方,这将是最干净的;并删除Spring配置中的手动UserController定义,以便组件扫描只是自动拾取bean并连接UserService


话虽如此,我真的不明白为什么你所展示的会有问题。您的代码中是否有新的UserController?是否还有其他Spring配置?

此线程中的其他注释没有考虑UserController类的包。提供的Spring配置将com.xyz.controller上的组件扫描初始化为基本包。UserController类在com.xyz中看起来不在该类的范围内,因此不会被组件扫描获取,除非这是一个输入错误@user269091-这可能是您的问题

我应该注意到,我不明白为什么控制器不在控制器包中。将控制器移动到那里,将@Autowired添加到private-UserService-UserService行的上方,这将是最干净的;并删除Spring配置中的手动UserController定义,以便组件扫描只是自动拾取bean并连接UserService


话虽如此,我真的不明白为什么你所展示的会有问题。您的代码中是否有新的UserController?还有其他Spring配置吗?

您正在混合注释和BeanXML配置。 如果使用@Controller注释,则不需要在XML中定义控制器bean

指令

<context:component-scan base-package="com.xyz.controller" />
负责为您注册控制器bean—您的控制器bean位于该包中


我建议您在配置SpringMVC时使用mvc名称空间,以保持配置的最少性和可读性。

您正在混合注释和BeanXML配置。 如果使用@Controller注释,则不需要在XML中定义控制器bean

指令

<context:component-scan base-package="com.xyz.controller" />
负责为您注册控制器bean—您的控制器bean位于该包中


我建议您在配置Spring mvc时使用mvc名称空间,以保持配置的最少性和可读性。

您有一个自动连接的实例和一个控制器的手动配置@adarshr自动连线的实例在哪里?我没有看到@Autowired…我只是看到一个属性。也许我误解了你有一个自动连接的实例和一个手动配置的控制器@adarshr自动连线的实例在哪里?我没有看到@Autowired…我只是看到一个属性。也许我误解了