Java 无法自动装配。没有找到 ... 类型的豆子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24441172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not autowire. No beans of ... type found
提问by Charlie Harper
can you help me solve why i can't autowire a class?? class UserDaoImpl:
你能帮我解决为什么我不能自动装配课程吗??UserDaoImpl 类:
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public void addUser(User user) {
sessionFactory.getCurrentSession().save(user);
}
@Override
public List<User> getUsers() {
return sessionFactory.getCurrentSession().createSQLQuery("SELECT * FROM user").list();
}
}
i want this class autowire into other class:
我希望这个类自动连接到其他类:
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao; //here is the error
@Override
public void addUser(User user) {
}
@Override
public List<User> getUsers() {
return null;
}
}
Controller:
控制器:
@Controller
public class UserController {
@Autowired
UserService userService;
String message = "This should be a list of users";
@RequestMapping("/user")
public ModelAndView showMessage() {
ModelAndView modelAndView = new ModelAndView("user");
modelAndView.addObject("message", message);
return modelAndView;
}
}
where can be the problem? should i show you more files? thanks. EDIT: ok so this is my session-factory.xml file:
问题出在哪里?我应该给你看更多的文件吗?谢谢。编辑:好的,这是我的 session-factory.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.connection.driver_class">org.postgresql.Driver</prop>
<prop key="hibernate.connection.url">jdbc:postgresql://localhost:8080/****</prop>
<prop key="hibernate.connection.username">****</prop>
<prop key="hibernate.connection.password">****</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="show_sql">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>model.User</value>
</list>
</property>
</bean>
</beans>
-class User is simple POJO which is mapped with hibernate. And maybe web.xml can help us :)
-class User 是简单的 POJO,它与 hibernate 映射。也许 web.xml 可以帮助我们:)
<web-app 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"
version="2.5">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
mvc-servlet-dispatcher.xml:
mvc-servlet-dispatcher.xml:
<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"
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.0.xsd">
<context:component-scan base-package="controller" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
and i don't have any applicationContext.xml file, maybe this can be the problem, can you help me with that? I'm adding a screenshot of my project and problem: http://screenshot.cz/75QVH/
而且我没有任何 applicationContext.xml 文件,也许这可能是问题所在,你能帮我解决这个问题吗?我正在添加我的项目和问题的屏幕截图:http: //screenshot.cz/75QVH/
回答by ebell
Your @Repository
is not being picked up (scanned) by <context:component-scan base-package="controller" />
going by what you are showing us. But we would need to see the packages for each class. Spring depend on you to tell it what packages to look in for your @Service
, @Controller
, @Repository
and @Component
.
您@Repository
没有被拾起(扫描)<context:component-scan base-package="controller" />
通过你向我们展示什么打算。但是我们需要查看每个类的包。春天取决于你告诉它在寻找什么包你的@Service
,@Controller
,@Repository
和@Component
。
Have a look at how component scanning works in spring.
看看 Spring 中的组件扫描是如何工作的。
回答by zhouyueyuedsf
Do not print @Autowired before print bean. otherwise,the No beans of … type found will occur
@Autowired
private ProjectRepository projectRepository;
不要在打印 bean 之前打印 @Autowired。否则,会出现 No beans of ... type found
@Autowired
private ProjectRepository projectRepository;