Whatever happened to the spring parent bean?

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?

Answers


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.


Need Your Help

How can I display the latest value in Chart?

c# sql asp.net database charts

I have to display chart that is connected with db dynamically. It is working fine. The code is given below. But my issue is my db is automatically update in every 5 minutes. So I have to display the

What program or process is using a specific serial port in C#

c# .net serial-port

When I'm trying to use a com port in my C# program sometimes the port is open and is used by another program. I want to be specific and generate warning message like this :

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.