Spring bean config XML, Java config and more

1. XML-based metadata

This is typical bean definition written in XML:

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
   <jee:jndi-lookup id="dataSource" jndi-name="java:databaseName"/>

Or

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
        <property name="url" value="jdbc:jtds:sqlserver://host_name/database_name;schema=dbo"/>
        <property name="username" value="dummy"/>
        <property name="password" value="dummy"/>
        <property name="defaultAutoCommit" value="true"/>
    </bean>

2. Java-based configuration since spring 3.0

This is equal to the above XML bean definition:

@Configuration
public class DataSourceConfig {

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
    @Bean
    public DataSource dataSource() throws NamingException {
        Context ctx = new InitialContext();
        return (DataSource) ctx.lookup("java:databaseName");
    }
}

Note this class is annotated with @Configuration. That requires in the applicationContext.xml to have:

<context:component-scan base-package="com.xx"/>

So that spring bean container will be able to register the bean by looking for @Components in packages.

3. Annotation-based configuration since spring 2.5

In applicationContext.xml to have this:

<context:annotation-config>

Will enable spring container to use annotation for dependency injection such as autowiring bean with @Autowire. It doesn't matter if the bean is registered with XML or Java config.

Note: Annotation injection is performed before XML injection, thus if you have both autowiring and property set in XML bean definition the latter configuration will override the former for properties wired.

4. Mixing Java and XML configuration
4.1 XML central config

<beans>
  <!-- enable processing of annotations such as @Autowired and @Configuration -->
  <context:annotation-config/>
  <context:property-placeholder location="classpath:/com/xx/jdbc.properties"/>

  <!-- pick Java config beans -->
  <bean class="com.xx.AppConfig"/>

  <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="url" value="${jdbc.url}"/>
      <property name="username" value="${jdbc.username}"/>
      <property name="password" value="${jdbc.password}"/>
  </bean>
</beans>

or using <context:component-scan/> to automatically pick up @Configuration classes

<beans>
  <!-- picks up and registers AppConfig as a bean definition -->
  <context:component-scan base-package="com.xx"/>
  <context:property-placeholder location="classpath:/com/xx/jdbc.properties"/>

  <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="url" value="${jdbc.url}"/>
      <property name="username" value="${jdbc.username}"/>
      <property name="password" value="${jdbc.password}"/>
  </bean>
</beans>

4.2 Java config(@Configuration) central

@Configuration
@ImportResource("classpath:/com/xx/properties-config.xml")
public class AppConfig {

  private @Value("${jdbc.url}") String url;
  private @Value("${jdbc.username}") String username;
  private @Value("${jdbc.password}") String password;

  public @Bean DataSource dataSource() {
      return new DriverManagerDataSource(url, username, password);
  }
}

评论

此博客中的热门博文

Nu förbjuder Kina handel med elfenben

Fader av pingyins

Kineserna vill köpa Volvos kompetens