我在Spring MVC中使用以下代码将xml转换为Java类。任何人都可以告诉我如何设置bean创建的顺序。当我运行以下代码时。 JDBCTemplate bean在DataSource Bean之前创建并提供异常,因为Datasource为null。
package com.outbottle.config;
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.apache.tomcat.jdbc.pool.DataSource;
@Configuration
@ComponentScan("com.outbottle")
@EnableWebMvc
public class Config extends WebMvcConfigurerAdapter
{
org.apache.tomcat.jdbc.pool.DataSource dataSource;
JdbcTemplate jdbcTemplate;
@Bean
public UrlBasedViewResolver setupViewResolver() {
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
@Bean
public org.apache.tomcat.jdbc.pool.DataSource setDataSource()
{
dataSource = new DataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUsername("muhiuddin");
dataSource.setPassword("muhiuddin");
dataSource.setUrl("jdbc:oracle:thin:@172.19.0.10:1521:db10g");
dataSource.setMaxIdle(5);
dataSource.setInitialSize(5);
return dataSource;
}
@Bean
public JdbcTemplate setJdbcTemplate()
{
jdbcTemplate= new JdbcTemplate();
// setDataSource(); // if I call this function then every thing is OK.
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry)
{
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/*");
}
}
答案 0 :(得分:1)
您可以使用@DependsOn
注释设置bean创建的顺序
类似的东西:
@Bean
@DependsOn("setDataSource")
public JdbcTemplate setJdbcTemplate()
{
jdbcTemplate= new JdbcTemplate();
// setDataSource(); // if I call this function then every thing is OK.
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
但你也可以自己调用方法:
@Bean
public org.apache.tomcat.jdbc.pool.DataSource getDataSource()
{
DataSource dataSource = new DataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUsername("muhiuddin");
dataSource.setPassword("muhiuddin");
dataSource.setUrl("jdbc:oracle:thin:@172.19.0.10:1521:db10g");
dataSource.setMaxIdle(5);
dataSource.setInitialSize(5);
return dataSource;
}
@Bean
public JdbcTemplate setJdbcTemplate()
{
jdbcTemplate= new JdbcTemplate();
// setDataSource(); // if I call this function then every thing is OK.
jdbcTemplate.setDataSource(getDataSource());
return jdbcTemplate;
}
答案 1 :(得分:1)
您的代码应为:
@Bean
public javax.sql.DataSource dataSource()
{
final DataSource dataSource = new DataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUsername("muhiuddin");
dataSource.setPassword("muhiuddin");
dataSource.setUrl("jdbc:oracle:thin:@172.19.0.10:1521:db10g");
dataSource.setMaxIdle(5);
dataSource.setInitialSize(5);
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(final javax.sql.DataSource dataSource)
{
final JdbcTemplate jdbcTemplate= new JdbcTemplate();
// setDataSource(); // if I call this function then every thing is OK.
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
这样Spring 知道要创建一个JdbcTemplate
bean,它需要一个DataSource
bean;所以它会正确决定订单。
你已经通过使用实例变量来绕过Spring,Spring无法确定。的创建时间。
另外两件事:
@Bean
方法的名称设置bean名称,你真的想要一个名为DataSource
的{{1}} bean吗?setDataSource
或interface
而不是特定类型,这是为了鼓励abstract class
进行自动装配。