有关使用基控制器类时继承如何影响 Spring Controller 类的问题
如果我在 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
{
}
Do
ControllerA
和ControllerB
各有一个单独的NameValueMap
实例吗?在基础控制器中定义的
@Autowired userService
是否意味着不需要在ControllerA
和ControllerB
中定义?如果在
ControllerA
或ControllerB
中使用 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
{
}
Do
ControllerA
andControllerB
each have a separate instance ofNameValueMap
?Does the
@Autowired userService
defined in the base controller mean that this does not need to be defined inControllerA
andControllerB
?If the logger instance defined in the BaseController is used in
ControllerA
orControllerB
does it necessarily refer to the one defined for theBaseController
because of this:
LoggerFactory.getLogger(BaseController.class)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
静态不是继承的(尽管有时看起来是这样),并且它不会为子类(重新)初始化。创建这些实例字段(如果您想在子类中访问它们,还可以将它们设为
protected
而不是private
):这样,每个子类都将拥有这些字段的实例,并且也将能够访问它(请注意,对于 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 thanprivate
if you want to access them in subclasses):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 ownClass
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.