有关使用基控制器类时继承如何影响 Spring Controller 类的问题

发布于 2024-11-07 19:07:06 字数 1331 浏览 0 评论 0原文

如果我在 Spring MVC 中使用基本控制器类,定义为子类的控制器是否会获取基本控制器中定义的任何私有静态对象的单独实例?

例如:

BaseController:

@Controller
public class BaseController
{

    private UserService userService;

    private static final Logger log = LoggerFactory.getLogger(BaseController.class);

    private static final Map<String, String> NameValueMap = Common.getNameValueMap();

    public final UserService getUserService()
    {
        return userService;
    }

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

}

ControllerA:

@Controller
public class ControllerA extends BaseController
{


}

ControllerB:

@Controller
public class ControllerB extends BaseController
{


}
  1. Do ControllerAControllerB 各有一个单独的 NameValueMap 实例吗?

  2. 在基础控制器中定义的@Autowired userService是否意味着不需要在ControllerAControllerB中定义?

  3. 如果在 ControllerAControllerB 中使用 BaseController 中定义的记录器实例,它是否一定引用为 BaseController 定义的记录器实例,因为

LoggerFactory.getLogger(BaseController.class)

If I use a base controller class in Spring MVC, do controllers defined as subclasses get separate instances of any private static objects defined in the base controller?

For example:

BaseController:

@Controller
public class BaseController
{

    private UserService userService;

    private static final Logger log = LoggerFactory.getLogger(BaseController.class);

    private static final Map<String, String> NameValueMap = Common.getNameValueMap();

    public final UserService getUserService()
    {
        return userService;
    }

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

}

ControllerA:

@Controller
public class ControllerA extends BaseController
{


}

ControllerB:

@Controller
public class ControllerB extends BaseController
{


}
  1. Do ControllerA and ControllerB each have a separate instance of NameValueMap?

  2. Does the @Autowired userService defined in the base controller mean that this does not need to be defined in ControllerA and ControllerB?

  3. If the logger instance defined in the BaseController is used in ControllerA or ControllerB does it necessarily refer to the one defined for the BaseController because of this:

LoggerFactory.getLogger(BaseController.class)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅浅淡淡 2024-11-14 19:07:06

静态不是继承的(尽管有时看起来是这样),并且它不会为子类(重新)初始化。创建这些实例字段(如果您想在子类中访问它们,还可以将它们设为protected而不是private):

protected final Logger log = LoggerFactory.getLogger( getClass() );
protected final Map<String, String> NameValueMap = Common.getNameValueMap();

这样,每个子类都将拥有这些字段的实例,并且也将能够访问它(请注意,对于 Logger 实例化,您现在可以使用 getClass(),这是一个实例方法,每个子类将提供自己的 Class > 对象)。

至于 @Autowired 问题,一般答案是,如果注释使用(元)注释 @Inherited 对自身进行注释,则该注释适用于子类。据我所知,@Autowired 不是。

Static is not inherited (although it seems so sometimes), and it's not (re)initialised for subclasses. Make those instance fields (and also make them protected rather than private if you want to access them in subclasses):

protected final Logger log = LoggerFactory.getLogger( getClass() );
protected final Map<String, String> NameValueMap = Common.getNameValueMap();

This way, every subclass will have an instance of those fields and will also be able to access it (note that for the Logger instantiation you are now able to use getClass(), which is an instance method and every subclass will provide its own Class object).

As for the @Autowired question, the general answer is that an annotation applies to subclasses if it's annotated itself with the (meta)annotation @Inherited. @Autowired is not, as far as I know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文