我尝试使用spring框架开发简单的应用程序,当我在eclipse执行项目时出现此异常:
例外:
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientService': Unsatisfied dependency expressed through method 'setClientDao' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [stockManagerWeb.net.alitech.stockManager.props.dao.Clients]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
May 03, 2017 11:34:23 AM org.springframework.web.context.ContextLoader initWebApplicationContext
SEVERE: Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientService': Unsatisfied dependency expressed through method 'setClientDao' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [stockManagerWeb.net.alitech.stockManager.props.dao.Clients]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:648)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [stockManagerWeb.net.alitech.stockManager.props.dao.Clients]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1463)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1094)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1056)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
... 24 more
May 03, 2017 11:34:23 AM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clientService': Unsatisfied dependency expressed through method 'setClientDao' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [stockManagerWeb.net.alitech.stockManager.props.dao.Clients]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:648)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:349)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1219)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4853)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [stockManagerWeb.net.alitech.stockManager.props.dao.Clients]: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1463)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1094)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1056)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
... 24 more
实施
客户:
@Component("clientDao")
public class Clients extends SqlConnectionManager {
public List<Client> getClients() {
return jdbc.query("select * from clients", new RowMapper<Client>() {
public Client mapRow(ResultSet rs, int rowNum) throws SQLException {
Client client = new Client();
client.setClientId(rs.getInt("clientId"));
client.setClientName(rs.getString("clientName"));
client.setAddress(rs.getString("address"));
client.setEmail(rs.getString("email"));
client.setPhone(rs.getString("phone"));
client.setType(rs.getString("type"));
return client;
}
});
}
public List<Client> getClients(String clientName) {
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("clientName", clientName);
return jdbc.query(
"select * from clients where clientName = :clientName",
parameters, new RowMapper<Client>() {
public Client mapRow(ResultSet rs, int rowNum)
throws SQLException {
Client client = new Client();
client.setClientId(rs.getInt("clientId"));
client.setClientName(rs.getString("clientName"));
client.setAddress(rs.getString("address"));
client.setEmail(rs.getString("email"));
client.setPhone(rs.getString("phone"));
client.setType(rs.getString("type"));
return client;
}
});
}
/**
* get client by PHONE number
*
* @param phone
* @return
*/
public List<Client> getClientsByPhoneNumber(String phone) {
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("phone", phone);
return jdbc.query("select * from clients where phone = :phone",
parameters, new RowMapper<Client>() {
public Client mapRow(ResultSet rs, int rowNum)
throws SQLException {
Client client = new Client();
client.setClientId(rs.getInt("clientId"));
client.setClientName(rs.getString("clientName"));
client.setAddress(rs.getString("address"));
client.setEmail(rs.getString("email"));
client.setPhone(rs.getString("phone"));
client.setType(rs.getString("type"));
return client;
}
});
}
/**
*
* @param client
* @return
*/
public boolean create(Client client) {
BeanPropertySqlParameterSource params = new BeanPropertySqlParameterSource(
client);
try {
return jdbc
.update("insert into clients (clientName, address, email, phone, type) "
+ "values (:clientName, :address, :email, :phone, :type)",
params) == 1;
} catch (InvalidDataAccessApiUsageException e) {
e.printStackTrace();
}
return false;
}
/**
* get client by his phone number
* @param phone
* @return
*/
public Client getClient(String phone) {
MapSqlParameterSource parameters = new MapSqlParameterSource();
parameters.addValue("phone", phone);
return jdbc.queryForObject(
"select * from clients where phone = :phone", parameters,
new RowMapper<Client>() {
public Client mapRow(ResultSet rs, int rowNum)
throws SQLException {
Client client = new Client();
client.setClientId(rs.getInt("clientId"));
client.setClientName(rs.getString("clientName"));
client.setAddress(rs.getString("address"));
client.setEmail(rs.getString("email"));
client.setPhone(rs.getString("phone"));
client.setType(rs.getString("type"));
return client;
}
});
}
@Transactional
public int[] create(List<Client> clients) {
SqlParameterSource[] params = SqlParameterSourceUtils
.createBatch(clients.toArray());
return jdbc.batchUpdate(
"insert into clients (clientName, address, email, phone, type) "
+ "values (:clientName, :address, :email, :phone, :type)",
params);
}
}
ClientService:
@Service
public class ClientService {
private Clients clientDao;
@Autowired
public void setClientDao(Clients clientDao){
this.clientDao = clientDao;
}
public void addClient(Client client) {
clientDao.create(client);
}
}
ClientController:
@Controller
public class ClientController {
private ClientService clientService;
@Autowired
public void setService(ClientService service) {
this.clientService = service;
}
@RequestMapping(value = "/doAddClient", method = RequestMethod.POST)
public String doAddClient(@Valid Client client ){
clientService.addClient(client);
return "order";
}
}
的pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.ali.taha</groupId>
<artifactId>stockManagerWeb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.4.Final</version>
</dependency>
</dependencies>
</project>
配置文件 的 beans.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<context:component-scan base-package="stockManager">
</context:component-scan>
<context:property-placeholder
location="stockManager/props/jdbc.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="username" value="${jdbc.username}"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven />
</beans>
道context.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:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
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-4.0.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="stockManager.props.dao">
</context:component-scan>
<jee:jndi-lookup jndi-name="jdbc/spring" id="dataSource"
expected-type="javax.sql.DataSource">
</jee:jndi-lookup>
</beans>
安全-context.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:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<security:authentication-manager>
<security:authentication-provider
user-service-ref="userDetailsService" />
</security:authentication-manager>
<security:http use-expressions="true" auto-config="true">
<security:logout logout-success-url="/login"
invalidate-session="true" logout-url="/logout" />
<security:intercept-url pattern="/dashboard"
access="isAuthenticated()" />
<security:intercept-url pattern="/login"
access="isAuthenticated()" />
<security:intercept-url pattern="/addProduct"
access="isAuthenticated()" />
<security:intercept-url pattern="/doAddProduct"
access="isAuthenticated()" />
<security:intercept-url pattern="/productsList"
access="isAuthenticated()" />
<security:intercept-url pattern="/doOrder"
access="isAuthenticated()" />
<security:intercept-url pattern="/cart"
access="isAuthenticated()" />
<security:intercept-url pattern="/orderDetails"
access="isAuthenticated()" />
<security:intercept-url pattern="/addToCart"
access="isAuthenticated()" />
<security:intercept-url pattern="/addMoreToCart"
access="isAuthenticated()" />
<security:intercept-url pattern="/getOrdersHistory"
access="isAuthenticated()" />
<security:intercept-url pattern="/cancelOrder"
access="isAuthenticated()" />
<security:intercept-url pattern="/users"
access="permitAll" />
<security:intercept-url pattern="/stockManagerWeb"
access="permitAll" />
<security:intercept-url pattern="/" access="permitAll" />
<security:intercept-url pattern="/static/**"
access="permitAll" />
<security:intercept-url pattern="/login"
access="permitAll" />
<security:intercept-url pattern="/logoutSuccess"
access="permitAll" />
<security:form-login login-page="/login"
default-target-url="/dashboard" login-processing-url="/j_spring_security_check"
authentication-failure-url="/login?error" username-parameter="username"
password-parameter="password" />
<security:session-management>
<security:concurrency-control
max-sessions="5" expired-url="/login" />
</security:session-management>
</security:http>
<bean id="userDetailsService" class="stockManagerWeb.net.alitech.stockManager.beans.UserDetailsServiceImpl" />
</beans>
服务上下文:
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
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-4.0.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan
base-package="stockManagerWeb.net.alitech.service">
</context:component-scan>
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/images/**" location="/images/" />
<mvc:resources mapping="/js/**" location="/js/" />
</beans>
感谢您的帮助。
答案 0 :(得分:0)
您应该尝试使用@Component而不是@Component(&#34; clientDao&#34;)。这将注册您的bean名称&#34;客户&#34; 。或者如果您不想这样做,那么第二个选项是@Qualifier(&#34; clientDao&#34;),您可以在@Autiwire之后放置。喜欢
@Autowired
@Qualifier("clientDao")
public void setClientDao(Clients clientDao){
this.clientDao = clientDao;
}
答案 1 :(得分:0)
更改您的ClientService和ClientController,如下所示。
ClientService:
@Service
public class ClientService {
private Clients clientDao;
@Autowired
@Qualifier("clientDao")
public void setClientDao(Clients clientDao){
this.clientDao = clientDao;
}
public void addClient(Client client) {
clientDao.create(client);
}
}
ClientController:
@Controller
public class ClientController {
private ClientService clientService;
@Autowired
@Qualifier("clientService")
public void setService(ClientService service) {
this.clientService = service;
}
@RequestMapping(value = "/doAddClient", method = RequestMethod.POST)
public String doAddClient(@Valid Client client ){
clientService.addClient(client);
return "order";
}
}
您必须在Spring中了解@Autowired和@Qualifer。您可以阅读它here。