首页新闻找找看学习计划

用main方法类来初始化OA系统权限至数据库时遇到该类不能被spring初始化,怎么解决?

0
悬赏园豆:80 [待解决问题]

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.toplink.ServerSessionFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import cj.oct.action.UserAction;
import cj.oct.entity.Privillage;
import cj.oct.entity.User;
import freemarker.template.Configuration;

@Component
public class Installer {
    public static void main(String[] args) {
        ApplicationContext ac=new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
    
        Installer it=(Installer) ac.getBean("installer");
        System.out.println(it);
        it.install();
    }
    @Resource
    private SessionFactory sessionFactory;
    @Transactional
    public void install(){
        //1.保存超级管理员
        User u=new User();
        u.setLoginName("admin");
        u.setName("超级管理员");
        Session session=null;
        if(sessionFactory.getCurrentSession()!=null){
         session=sessionFactory.getCurrentSession();
        }
        session.save(u);
        //2.保存权限
        Privillage menu1,menu2,menu,menu3,menu4,menu5;
        menu=new Privillage("系统管理",null,null);
        menu1=new Privillage("岗位管理","/role_list",menu);
        menu2=new Privillage("部门管理","/dept_list",menu);
        menu3=new Privillage("用户管理","/user_list",menu);
        
        //岗位管理下的增删改查权限
        session.save(new Privillage("岗位列表","/role_list",menu1));
        session.save(new Privillage("岗位添加","/role_add",menu1));
        session.save(new Privillage("岗位修改","/role_edit",menu1));
        session.save(new Privillage("岗位删除","/role_delete",menu1));
        
        //部门管理下的权限
        session.save(new Privillage("部门列表","/dept_list",menu2));
        session.save(new Privillage("部门添加","/dept_add",menu2));
        session.save(new Privillage("部门修改","/dept_edit",menu2));
        session.save(new Privillage("部门删除","/dept_delete",menu2));
        
        //用户管理权限下的权限
        session.save(new Privillage("用户列表","/user_list",menu3));
        session.save(new Privillage("用户添加","/user_add",menu3));
        session.save(new Privillage("用户修改","/user_edit",menu3));
        session.save(new Privillage("用户删除","/user_delete",menu3));
        //..................
        
        menu1=new Privillage("网上交流",null,null);
        menu2=new Privillage("论坛管理","/forumManage_list",menu);
        menu3=new Privillage("论坛","/forum_list",menu);
        session.save(menu1);
        session.save(menu2);
        session.save(menu3);
        //.................
        menu=new Privillage("审批流转",null,null);
        menu1=new Privillage("审批流程管理","/processDefinition_list",menu);
        menu2=new Privillage("申请模板管理","/template_list",menu);
        menu3=new Privillage("起草申请","/flow_templateList",menu);
        menu4=new Privillage("待我审批","/flow_myTaskList",menu);
        menu5=new Privillage("我的申请查询","/flow_myApplicationList",menu);
        session.save(menu);
        session.save(menu1);
        session.save(menu2);
        session.save(menu3);
        session.save(menu4);
        session.save(menu5);
    }

}

/**

以上就是初始化权限到数据库的class类,遇到下面的异常了,installer不能被实例化

 

*/

2016-12-25 18:40:58,816 WARN [org.hibernate.impl.SessionFactoryObjectFactory] - Could not bind factory to JNDI
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
    at javax.naming.InitialContext.getNameParser(InitialContext.java:499)
    at org.hibernate.util.NamingHelper.bind(NamingHelper.java:75)
    at org.hibernate.impl.SessionFactoryObjectFactory.addInstance(SessionFactoryObjectFactory.java:113)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:365)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1842)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:814)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:732)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1369)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:423)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    at cj.oct.on.Installer.main(Installer.java:22)
clazz...class cj.oct.entity.Farmer
clazz...class cj.oct.entity.Role
2016-12-25 18:41:00,090 WARN [org.hibernate.impl.SessionFactoryObjectFactory] - Could not unbind factory from JNDI
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
    at javax.naming.InitialContext.unbind(InitialContext.java:435)
    at org.hibernate.impl.SessionFactoryObjectFactory.removeInstance(SessionFactoryObjectFactory.java:139)
    at org.hibernate.impl.SessionFactoryImpl.close(SessionFactoryImpl.java:966)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.destroy(AbstractSessionFactoryBean.java:251)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.destroy(LocalSessionFactoryBean.java:865)
    at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:151)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:487)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:462)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:430)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:388)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    at cj.oct.on.Installer.main(Installer.java:22)
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'installer' defined in file [C:\Users\Administrator\Workspaces\MyEclipse Professional 2014\20161203_itcast_depart_OA\spring1\WEB-INF\classes\cj\oct\on\Installer.class]: Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(Z)V
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
    at cj.oct.on.Installer.main(Installer.java:22)
Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(Z)V
    at net.sf.cglib.core.DebuggingClassWriter.<init>(DebuggingClassWriter.java:47)
    at net.sf.cglib.core.DefaultGeneratorStrategy.getClassWriter(DefaultGeneratorStrategy.java:30)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:24)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:117)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
    at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
    at net.sf.cglib.proxy.Enhancer.<clinit>(Enhancer.java:69)
    at org.springframework.aop.framework.Cglib2AopProxy.createEnhancer(Cglib2AopProxy.java:229)
    at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:171)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:110)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:490)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:365)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:325)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:361)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1344)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    ... 14 more

cj_java_web的主页 cj_java_web | 初学一级 | 园豆:17
提问于:2016-12-25 19:01
< >
分享
所有回答(2)
0

我只是觉得 Installer it=(Installer) ac.getBean("installer");这个类中可以包含main方法本身吗???

我也没做测试,只是感觉这样有问题,希望楼主解决后可以分享下。

让我发会呆 | 园豆:2629 (老鸟四级) | 2016-12-26 10:47

可以的,但是我做不出来,因为老师做成功了

支持(0) 反对(0) cj_java_web | 园豆:17 (初学一级) | 2016-12-28 17:20
0

Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(Z)V

小彬 | 园豆:947 (小虾三级) | 2016-12-26 15:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册