Spring 父 bean 发生了什么?

发布于 2024-12-18 19:29:43 字数 475 浏览 2 评论 0原文

我是 Spring 3 的新手,特别是它奇特的附加注释

我不知道如何拥有一个扩展控制器类 A 的控制器类 B,其中 A 是通过 xml 文件定义的,具有一些有用的东西 - 例如数据库连接或其他东西。

如果我有一个(抽象)父 bean A 和一个具体 bean B,并且 B 是一个控制器,并且我正在使用注释自动装配(你知道, ),我可以以某种方式将 BA 联系起来吗?我使用注释吗?鉴于 A 需要放入一些随机的 spring bean,我在哪里定义 A

I'm new to Spring 3, and its fancy additional annotations in particular

I have no idea how to have a controller class B, that extends controller class A, where A is defined via the xml files to have something useful - say, a db connection or whatever.

If I have an (abstract) parent bean A, and a concrete bean B, and B is a controller, AND i'm using the annotations-auto-wiring (you know,
<context:component-scan base-package="package"/>
), can I somehow tie B to A? Do i use annotations? Where do i define A, given that A needs to have some random spring bean put into it?

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

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

发布评论

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

评论(2

扛起拖把扫天下 2024-12-25 19:29:43

正如 @Shakedown 提到的,在控制器中建立数据库连接是一种不好的做法。我知道这只是一个例子,但我想确保指出这一点。

对于大多数项目来说,使用 Spring 注释,您几乎可以完全摆脱 XML。

通常有一个 AbstractController 类,从中派生出其他具体类。

@Controller
@RequestMapping(value = "/action")
public class AController extends AbstractController
{
    @Autowired
    private AService aService;
    ...
}

可以使用 @Autowired 注解将依赖项注入到 Spring 类中。如果您想将依赖项注入到您的 AbstractController 中,而从技术上讲,它不是 Spring 托管类 - 您只需添加 @Component 注释即可使其成为一个托管类。

@Component
public abstract class AbstractController
{
    @Autowired
    protected CommonService commonService;

    @ExceptionHandler(MyException.class)
    public ModelAndView handleMyException(MyException e)
    {  
        ModelAndView model = new ModelAndView();

        model.addObject("errorMessage", e.getMessage());

        model.setViewName("error");

        return model;
    }

    ...
}

然后您可以在子类中使用该依赖项。

Having a database connection in your controller, as @Shakedown mentions, is a bad practice. I know it's just an example, but I wanted to make sure that was pointed out.

With Spring annotations, you can get away from XML almost completely, for most projects.

It is common to have an AbstractController class, from which other concrete classes are derived.

@Controller
@RequestMapping(value = "/action")
public class AController extends AbstractController
{
    @Autowired
    private AService aService;
    ...
}

Dependencies can be injected into Spring classes using the @Autowired annotation. If you want to inject dependencies into your AbstractController, which is not technically a Spring managed class - you can make it one simply by adding the @Component annotation.

@Component
public abstract class AbstractController
{
    @Autowired
    protected CommonService commonService;

    @ExceptionHandler(MyException.class)
    public ModelAndView handleMyException(MyException e)
    {  
        ModelAndView model = new ModelAndView();

        model.addObject("errorMessage", e.getMessage());

        model.setViewName("error");

        return model;
    }

    ...
}

You can then use the dependency in your subclasses.

缱倦旧时光 2024-12-25 19:29:43

这并不是真正的答案 - 但它是我最终采取的解决方案!它几乎只是扩展了博和斯卡夫曼给出的答案,我只是把它放在这里是为了完整。

给定:

@Controller
class B extends A {

... B things done here! They use something!

}

以及

abstact class A {

 private Object something;

 public void setSomething(Object something);
 public Object getSomething();
}

我曾经拥有的地方:

<bean id="A" abstract="true"
    class="package.to.A">
    <property name="something">
        <bean ref to something here>
    </property>        
</bean>

并且

<bean id="B" parent="A"
    class="package.to.B">
</bean>

我知道有(dun dun DUUUN):

<context:component-scan base-package="package.to.A"/>

<bean ref to something here with an id - it was an inner bean before>

<!-- no A or B beans defined -->

abstact class A {

 @Autowired
 private Object something;

 public void setSomething(Object something);
 public Object getSomething();
}

我在 AbstractController 派生的抽象类上不需要 @Component,但我认为你的意思是我需要 @Component,如果 想用abstractController做点什么吗?

This isn't really an answer - but it is the solution for i took in the end! It pretty much just expands on the answer given by Beau and skaffman, I'm just putting it here to be complete.

Given:

@Controller
class B extends A {

... B things done here! They use something!

}

and

abstact class A {

 private Object something;

 public void setSomething(Object something);
 public Object getSomething();
}

and where I used to have:

<bean id="A" abstract="true"
    class="package.to.A">
    <property name="something">
        <bean ref to something here>
    </property>        
</bean>

and

<bean id="B" parent="A"
    class="package.to.B">
</bean>

I know have (dun dun DUUUN):

<context:component-scan base-package="package.to.A"/>

<bean ref to something here with an id - it was an inner bean before>

<!-- no A or B beans defined -->

and

abstact class A {

 @Autowired
 private Object something;

 public void setSomething(Object something);
 public Object getSomething();
}

I didn't need the @Component on my AbstractController-derived abstract class, but I take it you mean i need the @Component if I want to do something with abstractController?

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