SSH 2 框架无法使用注解注入,注入取到的是null

sky-fantast 2015-08-13 05:40:03
applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
default-lazy-init="false"><!-- 配置DBCP数据源 -->
<bean id="dataSource" class="org.hibernate.dialect.SQLServerDialect">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=hl_project" />
<property name="username" value="sa" />
<property name="password" value="123" />
</bean>

<!-- 配置SessionFactory,由Spring容器来管理Hibernate -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 配置自动扫描包下的实体,也可使用annotatedClasses属性进行单个实体配置-->
<property name="packagesToScan" value="cn.hl.entity"></property>
<!-- 配置Hibernate属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.query.substitutions">true 1, false 0, yes 'Y', no 'N'</prop>
</props>
</property>
<!--
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value> </property>
-->
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 加载事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager"/>


<!-- 使Spring关注Annotation -->
<context:annotation-config />

<!-- 让Spring通过自动扫描来查询和管理Bean -->
<context:component-scan base-package="cn.hl" />

</beans>


web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>

<!-- 配置spring的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>

<!-- 开启监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 配置struts2的过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>




<welcome-file-list>
<welcome-file>LoginServiceUI</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>


</web-app>


Dao.class


package cn.hl.dao;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

@Repository("ZoneDao")
public class ZoneDao extends HibernateDaoSupport {

public void add(){
System.out.println("添加一条数据!");
}
}


Test.class


package cn.hl.junit;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import cn.hl.dao.ZoneDao;

/**
* dao层测试类
*
* */
public class DaoTest {
@Autowired
private ZoneDao zonedao;

@Test
public void show(){
zonedao.add();
}

}
...全文
251 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
新IT民工 2015-08-17
  • 打赏
  • 举报
回复
那实现里面得实现setSessionfactory方法,用了父类的这个才能用。
zero911318 2015-08-17
  • 打赏
  • 举报
回复
使用junit单元测试是无法获取到autowired注解的。 如果你就是为了测试,可以先获取ApplicationContext,然后再使用getBeans方法获取相应的对象。
u010246555 2015-08-15
  • 打赏
  • 举报
回复
在你的测试类打上以下注解:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
尼坤神 2015-08-14
  • 打赏
  • 举报
回复
<context:component-scan base-package="cn.hl" use-default-filters="true" /> <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
sky-fantast 2015-08-14
  • 打赏
  • 举报
回复
引用 5 楼 hamote 的回复:
3楼、4楼 提出的解决方案还是不行,@Autowired 注入得到的还是null
<!-- 使Spring关注Annotation --> <context:annotation-config /> <!-- 让Spring通过自动扫描来查询和管理Bean --> <context:component-scan base-package="cn.hl" /> 我已经引用了
nicholasbobo 2015-08-14
  • 打赏
  • 举报
回复
<!-- 让Spring通过自动扫描来查询和管理Bean --> <context:component-scan base-package="cn.hl" /> 把package路径写完整
sky-fantast 2015-08-14
  • 打赏
  • 举报
回复
引用 5 楼 hamote 的回复:
3楼、4楼 提出的解决方案还是不行,@Autowired 注入得到的还是null
不是这个原因,还是不行啊
董小姐_123 2015-08-14
  • 打赏
  • 举报
回复
添加注解没有,不然@Autowired 这些没有作用
nicholasbobo 2015-08-14
  • 打赏
  • 举报
回复
引用 5 楼 hamote 的回复:
3楼、4楼 提出的解决方案还是不行,@Autowired 注入得到的还是null
你的DaoTest也要在spring中注册才行哦
sky-fantast 2015-08-14
  • 打赏
  • 举报
回复
3楼、4楼 提出的解决方案还是不行,@Autowired 注入得到的还是null
sky-fantast 2015-08-13
  • 打赏
  • 举报
回复
谁有搭建好的SSH 2 的dome发一份我也行(可以使用注解),搭建的太复杂就不要了,我还只是个初学者,复杂了看不懂
sky-fantast 2015-08-13
  • 打赏
  • 举报
回复
文件路径

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧