我在試的一個項目,Spring+Hibernate的程序 一直提示上下文加載失敗 求大佬看看問題所在
錯誤信息
-250 'javax.annotation.ManagedBean' found and supported for component scanning
七月 08, 2017 12:30:46 上午 org.springframework.web.servlet.FrameworkServlet initServletBean
严重: Context initialization failed
java.lang.IllegalArgumentException
這是配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
">
<!-- JNDI(通过Tomcat)方式配置数据源 -->
<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:config.properties" />
<!-- 配置数据源 这里class可以使用不同的驱动-->
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc_driverClassName}" />
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />
</bean>
<!-- 配置hibernate session工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="current_session_context_class">thread</prop>
</props>
</property>
<!-- 自动扫描注解方式配置的hibernate类文件 -->
<property name="packagesToScan">
<list>
<!-- 此处与entity实体路径对应 -->
<value>pres.uiiooo.model</value>
</list>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 注解方式配置事物 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 支持注解 -->
<mvc:annotation-driven />
<!--自动装配DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter -->
<mvc:default-servlet-handler />
<!-- 设置自动扫描的路径,用于自动注入bean 这里的路径与自己的项目目录对应-->
<!-- 扫描controller路由控制器 -->
<context:component-scan base-package="pres.uiiooo.controller" />
<context:component-scan base-package="pres.uiiooo.dao,pres.uiiooo.service" />
<!-- 视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="suffix" value=".jsp" /> <!-- 视图文件类型 -->
<property name="prefix" value="/WEB-INF/views" /> <!-- 视图文件的文件夹路径 -->
</bean>
</beans>
這是幾個類
package pres.uiiooo.controller;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import pres.uiiooo.dao.UserDAO;
import pres.uiiooo.model.User;
import pres.uiiooo.service.UserService;
@Controller
public class Hello {
@Autowired
UserService userService;
@RequestMapping(value = "/Hello")
public String HelloWorld(Model model) {
System.out.println("!!!!!!!!!!!");
User u = new User(1, 2, "fff", "ppp", 1);
System.out.println(userService.register(u));
return "HelloWorld";
}
}
package pres.uiiooo.dao;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import pres.uiiooo.model.User;
@Repository
@Transactional
public class UserDAO {
@Autowired
SessionFactory sessionFactory;
public int register(User u){
int i = (Integer) sessionFactory.getCurrentSession().save(u);
System.out.println(i+"!!!");
if(i!=0)
return i;
else
return 0;
}
}
package pres.uiiooo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pres.uiiooo.dao.UserDAO;
import pres.uiiooo.model.User;
@Service
@Transactional
public class UserService {
@Autowired
UserDAO userDAO;
public int register (User u ){
return userDAO.register(u);
}
}
折騰巨久了!! 實在難以入眠!求解決