Java Spring3-自动连线没有发生。对象为空
servlet.xml 我正在使用注释,而不是在XML中创建bean 我在XML中添加了“context:annotation-config”Java Spring3-自动连线没有发生。对象为空,java,spring,spring-mvc,autowired,Java,Spring,Spring Mvc,Autowired,servlet.xml 我正在使用注释,而不是在XML中创建bean 我在XML中添加了“context:annotation-config” <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/sc
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.hemanths.expense.manager"/>
<mvc:annotation-driven/>
<tx:annotation-driven/>
<context:annotation-config />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="WEB-INF/hibernate.cfg.xml"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>
UserService.java
package com.hemanths.expense.manager.service.impl;
import com.hemanths.expense.manager.hibernate.dao.UserDao;
import com.hemanths.expense.manager.hibernate.entity.User;
import com.hemanths.expense.manager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
public UserServiceImpl() {
}
public UserServiceImpl(UserDao userDao){
this.userDao = userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
@Transactional
public void addUser(User user) {
userDao.addUser(user);
}
UserServiceImplTest.java
public class UserServiceImplTest {
@Autowired
UserService userService;
@Test
public void shouldAddUser() {
User user = new User("firstName", "lastName", new Date(), "M", "username", "password");
userService.addUser(user);
}
}
堆栈跟踪
java.lang.NullPointerException
at com.hemanths.expense.manager.service.impl.UserServiceImplTest.shouldAddUser(UserServiceImplTest.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
目录结构
您的hibernate的示例用法
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.base.package"/>
<mvc:annotation-driven/>
<tx:annotation-driven/>
<context:annotation-config />
<bean id="_hibernateSessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="WEB-INF/hibernate.cfg.xml"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- userDao Repo -->
<bean id="_userDao"
class="com.hemanths.expense.manager.hibernate.dao.impl.UserDaoImpl">
<property name="_hibernateSessionFactory">
<ref bean="_hibernateSessionFactory"></ref>
</property>
</bean>
<!-- userService Repo -->
<bean id="_userDao"
class="com.hemanths.expense.manager.service.impl.UserServiceImpl">
<property name="_hibernateSessionFactory">
<ref bean="_hibernateSessionFactory"></ref>
</property>
</bean>
</beans>
在控制器中需要的位置使用autowire:
@Autowired
private UserServiceImpl _userServiceImpl;
正如评论中提到的,您不需要在
servlet.xml
文件中为服务和DAO类创建bean:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.hemanths.expense.manager"/>
<mvc:annotation-driven/>
<tx:annotation-driven/>
<context:annotation-config />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="WEB-INF/hibernate.cfg.xml"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>
我得到了答案。谢谢@Andrei Stefan 因为我正在运行一个测试,所以我必须用xml文件路径对测试进行如下注释
@ContextConfiguration(locations = {"classpath:mvc-dispatcher-servlet.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class UserServiceImplTest {
@Autowired
UserService userService;
@Test
public void shouldAddUser() {
User user = new User("firstName", "lastName", new Date(), "M", "username", "password");
userService.addUser(user);
}
}
每个类的包是什么?如果您计划自动检测它们,为什么要为
userDao
和userService
手动创建两个bean?或者,组件扫描的基本包
没有覆盖dao和服务所在的包?您应该这样做,因为没有创建应用程序上下文。方法如下:@ContextConfiguration(locations={“classpath:servlet.xml”}@RunWith(SpringJUnit4ClassRunner.class)
。在locations
下,您需要放置servlet.xml文件的路径。userserviceinmpltest
是一个测试类(可能与JUnit一起),为了使它工作,您需要告诉Spring使用什么xml文件来构建您的测试将使用的应用程序上下文。这两个注释是这样一个类成功执行所必需的。我建议阅读Spring参考文档中的相关章节。你的控制器与你的测试类无关,这是另外一个故事。你不需要getter和setter吗?我已经删除并检查了它,但仍然抛出NullPointerException。我已经更新了问题。请check@HemanthGowda:干吗?当您使用autowire bean时,您将在已创建实例的依赖类中隐式设置它们。如果你能发布你的用户类的代码?Hibernate可能没有正确识别和映射您的类。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.hemanths.expense.manager"/>
<mvc:annotation-driven/>
<tx:annotation-driven/>
<context:annotation-config />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="WEB-INF/hibernate.cfg.xml"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
</beans>
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;
// Methods here
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
// Methods here
}
@ContextConfiguration(locations = {"classpath:mvc-dispatcher-servlet.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class UserServiceImplTest {
@Autowired
UserService userService;
@Test
public void shouldAddUser() {
User user = new User("firstName", "lastName", new Date(), "M", "username", "password");
userService.addUser(user);
}
}