Skip to content

Enable the definition of beans in the application context using annotations instead of XML [SPR-1987] #6680

@spring-projects-issues

Description

@spring-projects-issues

Rodrigo Urubatan Ferreira Jardim opened SPR-1987 and commented

I think some bean definitions are just configuration, as the Data Source, Transaction Manager, Mail Server, ...
but most of then are the application code, and not configuration.
for the seccond case, I think it would be very easy and clean, to register it on the application with annotations, because it would use a lot less code than the XML approach, and this beans registrations (like my application DAOs, or my Spring MVC controllers), does not change during the life time of any application.
for this to be reached it would be necessary to scan the classpath for the annotated classes.

I have wrote a litle bouch of classes that make the application context behave exaclty this way, enabling it to use bean registration by annotations.
please consider including it in spring framework as an optional extension.

I`m not sure the application way I did it (creating another application context class) is the best one, but it is an idea of implementation for it :D

the code I have wrote is atacched.
what it does right now is:

  1. @Bean - equals to <bean>
    • autoWire (NO, BY_NAME, BY_TYPE, INHERITED)
    • dependencyCheck
    • destroyMethod
    • factoryMethod
    • initMethod
    • lazy
    • name
    • scope (APPLICATION, REQUEST, SESSION) - short cut to aop:scope
    • singleton
  2. @UrlMapping - attaches the bean to a URL using an bean factor post processor (AnnotationHandlerMaping)
  3. @UrlMappings - enables multiple UrlMappings per bean
  4. @Property
  5. @ConstructorArgs

in the code, there is an example applicationContext.xml that enables WebServices using JSR 181 webservices through xfire/spring integration, and has already a propertyPlaceholderConfigurer.

the annotation scan, for now scans every directory/jar in the classpath, that contains a file names to.properties in the root, and the propertyPlaceholderConfigurer uses this files too.

with this jar, I was able to define only a datasource, transaction manager,sessionFactory,tx:annotation-driven,and a defaultViewResolver
and write all the rest of my application without touching in a XML file.
this way, all I need to know about my beans was in the bean code.

the installation of it is preaty clean too:

<?xml version="1.0" encoding="UTF-8"?>

<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>TO - Novo Site</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>contextClass</param-name>
<param-value>
br.com.techoffice.spring.web.TOAnnotationXmlWebApplicationContext
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<filter>
<filter-name>requestContext</filter-name>
<filter-class>
org.springframework.web.filter.RequestContextFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>requestContext</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.to</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/web/*</url-pattern>
</servlet-mapping>
</web-app>

a blank dispatcher-servlet.xml (does this is really needed? what if I want all my configuration to be application-wide, I really need to create a xml file to each servlet, or in this case to the only servlet I have?)

again, please consider it as an optional module for spring framework.
and thanks for the great framework, it has saved a few of my projects in clients in the last 2 years.


Attachments:

Issue Links:

4 votes, 5 watchers

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions