-
Okay, turns out that the servlet 3.0 spec says that addMapping() doesn't override already-existing mappings. Resin puts in a default mapping for "/", and so I can't override it (they'll fix that in a future release). But something like "/*" works.
I'm now looking to see how to convert the following XML into annotations.
Code:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.foo.bar.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.use_sql_comments">false</prop>
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager"
p:sessionFactory-ref="sessionFactory" />
I can just create it, something like this:
Code:
@Configuration
@ComponentScan(basePackages = { "com.foo.bar.model", "com.foo.bar.dao" },
excludeFilters = { @Filter(type = FilterType.ANNOTATION, value = Controller.class) })
public static class AppConfig
{
@Bean
public
org.springframework.jndi.JndiObjectFactoryBean
dataSource()
{
org.springframework.jndi.JndiObjectFactoryBean bean = new org.springframework.jndi.JndiObjectFactoryBean();
bean.setJndiName("java:comp/env/jdbc/db");
return bean;
}
@Bean
public
org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
sessionFactory()
{
DataSource dataSource = <get a data source, but how? Can I @Autowire?>;
org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean bean = new org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean();
bean.setDataSource(dataSource);
return bean;
}
}
But as you can see in the code, I'm not sure how to wire it to the data source. Moreover, I don't know if I need to scan any packages, since I've already scanned my model package in the AppConfig.
I guess the general question is, how do I create my beans in code and access created beans in the context.
You had suggested using org.springframework.web.servlet.config.annotation. WebMvcConfigurationSupport, but I'm trying to do this in my AppConfig, not my WebConfig, and I don't see an analogous class to inherit from for aid.
Thanks!
-
Ah. I finally found it in the docs: you can just refer to the @Bean methods, and it magically ensures that only a single instance is used (I think that's how I interpret it):
http://static.springsource.org/sprin...g-dependencies
-
Everything is working now but my automated unit tests. I don't know how to convert the use of @WebAppConfiguration("file:target/build") and @ContextConfiguration({"file:target/build/WEB-INF/config/springContext.xml", "file:target/build/WEB-INF/config/springWebDispatcherConfig.xml"}) to use the servlet-3.0 annotation-based configuration (using my AbstractAnnotationConfigDispatcherServletInitializ er subclass), and using my AppConfig and WebConfig @Configuration classes.
I'm not sure it's even possible to configure spring-test-mvc this way. Is it?