spring 配置注解

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/i_jack/article/details/53048134

报错内容: Context namespace element 'annotation-config' and its parser class [org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser] are only available on JDK 1.5 and higher

原因:使用的是jdk1.8,spring会检测jdk版本,在org.springframework.core.JdkVersion.class这个类中定义了支持的jdk版本,支持jdk1.5以上版本,

但是最多定义到了jdk1.7.

解决办法:

1.更换jdk版本。1.5-1.7版本

2.修改JdkVersion.class这个类,再将这个类编译替换到原来的jar包中。

修改后的文件为:

   package org.springframework.core;


public abstract class JdkVersion {


public static final int JAVA_13 = 0;
public static final int JAVA_14 = 1;
public static final int JAVA_15 = 2;
public static final int JAVA_16 = 3;
public static final int JAVA_17 = 4;
/**
*  for Java 8
* *
*/
public static final int JAVA_18 = 5;
private static final String javaVersion;


private static final int majorJavaVersion;


static {
javaVersion = System.getProperty("java.version");
// for Java8
   if (javaVersion.indexOf("1.8.") != -1) {
majorJavaVersion = JAVA_18;
}
else if (javaVersion.indexOf("1.7.") != -1) {
majorJavaVersion = JAVA_17;
}
else if (javaVersion.indexOf("1.6.") != -1) {
majorJavaVersion = JAVA_16;
}
else if (javaVersion.indexOf("1.5.") != -1) {
majorJavaVersion = JAVA_15;
}
else {
// else leave 1.4 as default (it's either 1.4 or unknown)
majorJavaVersion = JAVA_14;
}
}
public static String getJavaVersion() {
return javaVersion;
}
public static int getMajorJavaVersion() {
return majorJavaVersion;
}
public static boolean isAtLeastJava14() {
return true;
}
public static boolean isAtLeastJava15() {
return getMajorJavaVersion() >= JAVA_15;
}
public static boolean isAtLeastJava16() {
return getMajorJavaVersion() >= JAVA_16;
}


}

展开阅读全文

spring基于注解的事务管理配置 注解不起作用

08-09

项目是j2ee的项目,现已搭建好了spring mvc,也配置了hibernate管理数据库。 由于调用save等需要提交的方法的时候,hibernate没有sql语句输出,数据无法保存进数据库。想可能是事务的问题,所以配置了事务管理,可是事务管理依然没有用。大家帮我看看是什么原因。rnapplicationContext.xml代码如下:rnrnrnrn rn rn rn rn rn rn rn rn rn rnrn rn rn rn rnrn rn rn rn rn rn rn rn rn rn rn rn rn rn rn classpath:com/blrise/entityrn rn rn rn rn org.hibernate.dialect.MySQL5Dialectrn truern org.hibernate.connection.C3P0ConnectionProviderrn rn falsern rn rn rn rn rn rn rn rn rn rnrn rn rnrnrnrnrnDao层:rnpackage com.blrise.daoImpl;rnrnimport java.io.Serializable;rnimport java.lang.reflect.ParameterizedType;rnimport java.lang.reflect.Type;rnimport java.util.List;rnrnimport org.hibernate.Query;rnimport org.hibernate.SessionFactory;rnimport org.springframework.beans.factory.annotation.Autowired;rnimport org.springframework.transaction.annotation.Transactional;rnrnimport com.blrise.dao.IGenericDAO;rnrn@Transactionalrnpublic class GenericDAOHibernate implements IGenericDAO rn rn private Class clz;rn private String entityName;rn @Autowiredrn private SessionFactory sessionFactory;rn /**rn * Constructorrn */rn rn @SuppressWarnings( "rawtypes", "unchecked" )rn public GenericDAOHibernate() rn Class c = getClass();rn Type t = (Type) c.getGenericSuperclass();rn if (t instanceof ParameterizedType ) rn Type[] p = ((ParameterizedType)t).getActualTypeArguments();rn clz = (Class) p[0];rn entityName = clz.getSimpleName();rn rn rn @Overridern @Transactional()rn public T save(T model) rn sessionFactory.openSession().save(model);rn return model;rn rnrn @Overridern public T update(T model) rn sessionFactory.openSession().update(model);rn return model;rn rnrn @Overridern public void delete(T model) rn sessionFactory.openSession().delete(model);rn rnrn @SuppressWarnings("unchecked")rn @Overridern public List list() rn rn Query query = sessionFactory.openSession().createQuery("from " + entityName);rn List result = query.list();rn return result;rn rnrn @SuppressWarnings("unchecked")rn @Overridern public T load(Serializable id) rn rn T model = (T) sessionFactory.openSession().load(clz, id);rn return model;rn rn @Overridern public T findByFullName(String fullName) rn rn rn Query query = sessionFactory.openSession().createQuery("from " + entityName + " where fullName = '" + fullName + "'" );rn return (T) query.uniqueResult();rn rnrnrnrnrnmanagerDao:rnrnpackage com.blrise.daoImpl;rnrnimport org.springframework.stereotype.Repository;rnimport org.springframework.transaction.annotation.Transactional;rnrnimport com.blrise.dao.IManagerDao;rnimport com.blrise.entity.Manager;rnrnrn@Repositoryrn@Transactionalrnpublic class ManagerDaoImpl extends GenericDAOHibernate implements IManagerDao rn rnrnrnrnServiceDao:rnrnpackage com.blrise.serviseImpl;rnrnimport java.util.List;rnrnimport org.springframework.beans.factory.annotation.Autowired;rnimport org.springframework.stereotype.Service;rnimport org.springframework.transaction.annotation.Transactional;rnrnimport com.blrise.dao.IManagerDao;rnimport com.blrise.entity.Manager;rnimport com.blrise.servise.IManagerServise;rnrnrn@Service("ManagerServiseImpl")rn@Transactional(readOnly=false)rnpublic class ManagerServiseImpl implements IManagerServise rnrn @Autowiredrn private IManagerDao managerDao;rn @Overridern public List findAll() rn return managerDao.list();rn rn @Overridern @Transactionalrn public void save(Manager manager) rn managerDao.save(manager);rn rnrnrnrnrnControl层:rnrnpackage com.blrise.test;rnrnimport java.util.ArrayList;rnimport java.util.List;rnrnimport org.apache.log4j.Logger;rnimport org.springframework.beans.factory.annotation.Autowired;rnimport org.springframework.stereotype.Controller;rnimport org.springframework.ui.Model;rnimport org.springframework.web.bind.annotation.RequestMapping;rnimport org.springframework.web.bind.annotation.RequestMethod;rnimport org.springframework.web.servlet.ModelAndView;rnrnimport com.blrise.entity.Manager;rnimport com.blrise.servise.IManagerServise;rnrnrn@Controllerrn@RequestMapping("/test")rnpublic class Test rn private static Logger logger = Logger.getLogger(Test.class);rn rn @Autowiredrn private IManagerServise managerServise;rn rn @RequestMapping(method=RequestMethod.GET)rn public ModelAndView test(Model model)rn List managers =managerServise.findAll();rn rn Manager manager = new Manager(); rn manager.setName("中国");rn managerServise.save(manager);rn if (logger.isDebugEnabled()) rn logger.debug("Managers = " + manager);rn rn model.addAttribute("manager",manager);rn return new ModelAndView("Test.jsp");rn rnrnrnrnrn后台能够输出查询语句,但是save的语句没有。大家帮帮忙。跪谢!~~~ 论坛

没有更多推荐了,返回首页