NutzCN Logo
问答 nutz-integration-spring 配置自己的dao 不能初始化
发布于 164天前 作者 dengwubogit 286 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

现在的问题是这样的,因为现在是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;
    }

  


}
11 回复
   @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()); // 这没走ioc吧?? 类似的写法还有很多, spring是这样的??
      return cacheProvider;
   }

应该是可以的,有几个bean都是这样配的,但是问题是现在dao初始化不了 ~~~~(>_<)~~~~

注入的地方是怎么写的?

@Service
public class SysMenuServiceImpl extends BaseServiceImpl<Sys_menu> implements SysMenuService{
    @Autowired
    public SysMenuServiceImpl(Dao dao) {
        super(dao);
    }

dao()的时候是报错还是怎样?

    @Bean
    public Dao dao(){

有springboot的starter, 不需要自己写吧

不报错,能正常使用,我觉得是因为在初始化之前就已经初始化了一个dao,所以就没有执行这个Bean的初始化

另外一个dao?? 重名了?

嗯 就是用的spring-nutz的starter,因为我不知道怎么在application.yml中配置缓存管理器啥的,所以就手动创建的bean

添加回复
请先登陆
回到顶部