Skip to main content

Spring-Annotation v1.0.2 just released

Posted by urubatan on November 29, 2006 at 12:38 PM PST

Some additions to this new version:
* Support for XML Schema Configuration, now you just need to put the tag <sa:annotation-autoload> to enable the annotation module
* There is no need to create the empty to.properties anymore if all your classes are in some open directory, you just need it in the root of the jars you want to use the annotations
* new @Alias annotation that enables the configuration of aliases to beans
* Suport for filters in the loading of classes to enable the use in diferent contxts or modules, to enable this just use the tags includePackage or excludePackage inside the <sa:annotation-autoload> , both uses regular expressions to filter the package name.
* The JSF module does not forces the use of the AspectJ loadtimeweaving anymore, working on a way to support both approaches
* Now the annotation reading module supports the use of any kind of annotation, but there is no documentation of it for now, expect some news for the next release
* Added support for some of the JSR 250 annotations, now you can use @PostCreate and @PreDestroy to specify init and destroy methods of your bean, and with the security module (very early stage) you can use the annotations @DenyAll, @PermitAll and @RolesAllowed to configure the secrity of your project
* Started the port of some of the new JSF features, like the new Scopes (flash and conversation) to the basic web module
* now there is no need to add a RequestContextListener to your web.xml it is already included in the dummy.tld inside the web module
* Some more unit tests written for the framework, there is a lot missing yet.
* The litle existent documentation is a litle bether, and the JavaDocs are starting to be written

in resume you can do this:
create a file named appContext.xml and put this inside it:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:sa="https://spring-annotation.dev.java.net/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
https://spring-annotation.dev.java.net/context https://spring-annotation.dev.java.net/context.xsd"
default-autowire="byName">
<import resource="classpath*:applicationContext.xml"/>
<sa:annotation-autoload />
</beans>

then you create a java class named Main with this code:

@Bean(name="main")
public class Main {
private TestObject test;

/**
* this method should be called from spring to set the variable to the other bean in the context which has the name set to test
* @param test
*/
public void setTestBean(TestObject test) {
this.test = test;
}

public static void main(String[] args){
ClassPathXmlApplicationContext appctx = new ClassPathXmlApplicationContext("/appContext.xml");
Main m = (Main) appctx.getBean("main");
m.test.printName();
}

}

and another java class named TestObject with this content:

@Bean(name = "testBean")
public class TestObject {
public void printName() {
System.out.println("No Name, just an example");
}
}

All set, you have your first application running.
if you want a web application, just do like this:
one java class named ExampleController:

@Bean
@UrlMapping("/test")
public class ExampleController implements Controller {
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
return new ModelAndView("example","message","This page is the /WEB-INF/jsp/example.jsp and it was openned from the Example Controller using the Spring View Resolver");
}
}

one applicationContext.xml inside your WEB-INF directory:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:sa="https://spring-annotation.dev.java.net/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
https://spring-annotation.dev.java.net/context https://spring-annotation.dev.java.net/nonav/context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"
default-autowire="byName">
<import resource="classpath*:applicationContext.xml" />
<sa:annotation-autoload />
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

and then setup your web.xml:

<web-app 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>Spring-Annotation Example App</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>net.java.dev.springannotation.utils.FakeWebApplicationContext</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/web/*</url-pattern>
</servlet-mapping>
</web-app>

as you can see, the web.xml is just a vanilla Spring MVC web.xml (the FakeWebApplicationContext is just to make the dispatcher servlet not need the dispatcher-servlet.xml, if you create this file you can remove that parameter)

there are some examples in the project website, and the portuguese version of this post in this blog.
if you liked the idea and want more examples, just comment here and I`ll try to write some more.
or if you would like to contribute with some examples I`ll thank you a lot!!