现在的问题是这样的,因为现在是springboot 工程,然后我要用nutz的daocache;
cacheExecutor cacheManager cacheProvider 全部初始化好了,datasource也初始化了;
最后一步初始化org.nutz.dao.impl.NutDao 的时候并没有执行,但是我将bean的名字改一下比如改成mydao 就会执行并初始化.
未初始化:
@Bean
public NutDao dao(){
System.out.println("dao::::");
NutDao dao = new NutDao(druidDataSource());
System.out.println("cacheManagerConfig.cacheExecutor()::::"+cacheManagerConfig.cacheExecutor());
dao.setExecutor(cacheManagerConfig.cacheExecutor());
System.out.println("cacheManagerConfig.cacheExecutor()::::"+cacheManagerConfig.cacheExecutor());
return dao;
}
初始化成功:
@Bean
public NutDao myDao(){
System.out.println("dao::::");
NutDao dao = new NutDao(druidDataSource());
System.out.println("cacheManagerConfig.cacheExecutor()::::"+cacheManagerConfig.cacheExecutor());
dao.setExecutor(cacheManagerConfig.cacheExecutor());
System.out.println("cacheManagerConfig.cacheExecutor()::::"+cacheManagerConfig.cacheExecutor());
return dao;
}
非常困惑,因为我service里依赖的是dao 这个对象,就像问问老板怎么样才能正常执行dao的初始化?
O(∩_∩)O谢谢 555~
贴上代码:
CacheManagerConfig
@SuppressWarnings("all")
@Configuration
@Order(1)
public class CacheManagerConfig {
@Bean
public CacheManager cacheManager(){
URL url = CacheManagerConfig.class.getClassLoader().getResource("ehcache.xml");
CacheManager cacheManager = CacheManager.create(url);
return cacheManager;
}
@Bean(initMethod = "init")
public EhcacheDaoCacheProvider cacheProvider(){
EhcacheDaoCacheProvider cacheProvider = new EhcacheDaoCacheProvider();
cacheProvider.setCacheManager(cacheManager());
return cacheProvider;
}
@Bean
public CachedNutDaoExecutor cacheExecutor(){
List<String> tables = Arrays.asList("sys_user", "sys_role", "sys_menu");
Set<String> cachedTableNames = new HashSet<>(tables);
CachedNutDaoExecutor cacheExecutor = new CachedNutDaoExecutor();
cacheExecutor.setCacheProvider(cacheProvider());
cacheExecutor.setCachedTableNames(cachedTableNames);
return cacheExecutor;
}
}
DaoConfig:
@SuppressWarnings("all")
@Configuration
@Order(2)
public class DaoConfig {
@Value("${spring.datasource.driver-class-name}")
String driver;
@Value("${spring.datasource.url}")
String url;
@Value("${spring.datasource.username}")
String username;
@Value("${spring.datasource.password}")
String password;
@Autowired
private CacheManagerConfig cacheManagerConfig;
@Bean
public NutDao dao(){
System.out.println("dao::::");
NutDao dao = new NutDao(druidDataSource());
System.out.println("cacheManagerConfig.cacheExecutor()::::"+cacheManagerConfig.cacheExecutor());
dao.setExecutor(cacheManagerConfig.cacheExecutor());
System.out.println("cacheManagerConfig.cacheExecutor()::::"+cacheManagerConfig.cacheExecutor());
return dao;
}
@Bean
public ServletRegistrationBean druidServlet() {
return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
}
@Bean(name="druidDataSource")
public DataSource druidDataSource() {
System.out.println("druidDataSource:::::::::::::::");
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driver);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
try {
druidDataSource.setFilters("stat, wall");
} catch (SQLException e) {
e.printStackTrace();
}
return druidDataSource;
}
@Bean
public FilterRegistrationBean filterRegistrationBean() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new WebStatFilter());
filterRegistrationBean.addUrlPatterns("/*");
filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
return filterRegistrationBean;
}
}