我正在尝试编写基本的春季项目。但是,通过服务使用@Autowired,我遇到了一个大问题。我没有找到问题,并在下面得到了错误:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.blackfirday.services.UserService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
... 19 common frames omitted
这是我的代码:
UserServiceInterface
package com.blackfirday.services;
import com.blackfriday.generated.User;
public interface UserService {
public User userLogin(final String userId, final String userPassword);
}
UserServiceImpl
package com.blackfirday.services;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.blackfriday.dao.UserDao;
import com.blackfriday.generated.User;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired(required=true)
@Qualifier(value="userDao")
private UserDao userDao_;
@Override
public User userLogin(String userId, String userPassword) {
// TODO: Varify the user password in the returned user object.
return userDao_.getUserDetails(userId);
}
}
UserDao界面
package com.blackfriday.dao;
import com.blackfriday.generated.User;
public interface UserDao {
public User getUserDetails(final String userId);
}
UserDao Impl
package com.blackfriday.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.springframework.stereotype.Repository;
import com.blackfriday.generated.User;
@Repository("userDao")
public class UserDaoImpl implements UserDao {
private final String connectionURL = "jdbc:msql://localhost:3306/blackfridaybank";
private static final String DRIVER_PREFIX = "jdbc:apache:commons:dbcp:";
@Override
public User getUserDetails(String userId) {
// TODO Make the database as centralized connection pool.
Connection conn = null;
User user = new User();
try {
conn = getConnection(connectionURL);
conn.setAutoCommit(true);
Statement stmnt = conn.createStatement();
ResultSet rs = stmnt.executeQuery("select * from user where id="+userId);
while(rs.next()){
user.setUserId(rs.getString("id"));
user.setUserName(rs.getString("name"));
user.setUserGender(rs.getString("gender"));
user.setUserStatus(rs.getString("status"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try{
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return user;
}
private Connection getConnection(final String connectURL) throws SQLException {
return DriverManager.getConnection(DRIVER_PREFIX + connectURL, "blackfriday", "blackfriday");
}
}
UserServicesEndPoint
package com.blackfriday.webservices.endpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
import com.blackfirday.services.UserService;
import com.blackfriday.generated.UserLoginRequest;
import com.blackfriday.generated.UserLoginResponse;
@Endpoint
public class UserServicesEndPoint {
private static final String USER_LOGIN_REQUEST = "http://www.blackfridaybank.org/webservices/UserOperation";
private UserService userService;
@Autowired
public UserServicesEndPoint(UserService userService_i){
userService = userService_i;
}
@PayloadRoot(localPart = "UserLoginRequest", namespace = USER_LOGIN_REQUEST)
@ResponsePayload
public UserLoginResponse getUserDetails(@RequestPayload UserLoginRequest request) {
// 1.we need to make an instance of a response
UserLoginResponse response = new UserLoginResponse();
// 2.use the request data to in the services class and populated to
// response
response.setUserDetails(userService.userLogin(request.getUserId(), request.getPassword()));
// 3.return a data populated response.
return response;
}
}
启动课程
package com.blackfriday.startup;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages={"com.blackfriday"})
public class Startup {
public static void main(String[] args) {
SpringApplication.run(Startup.class, args);
}
}
Web Config
package com.blackfriday.startup;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;
@EnableWs
@Configuration
public class WebSerivceConfig extends WsConfigurerAdapter{
@Bean
public ServletRegistrationBean messageDispatcherServlet (ApplicationContext context) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(context);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean(servlet, "/ws/*");
}
@Bean(name="useroperation")
public DefaultWsdl11Definition defaultWsdl11Definition (XsdSchema UserOperation) {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setPortTypeName("useroperationport");
wsdl11Definition.setLocationUri("/ws");
wsdl11Definition.setTargetNamespace("http://www.blackfridaybank.org/webservices/UserOperation");
wsdl11Definition.setSchema(UserOperation);
return wsdl11Definition;
}
@Bean
public XsdSchema UserOperation() {
return new SimpleXsdSchema(new ClassPathResource("UserOperation.xsd"));
}
//adding the following method to return the correct xsd file before loaded to the soapui.
@Bean
public XsdSchema UserDetails() {
return new SimpleXsdSchema(new ClassPathResource("UserDetails.xsd"));
}
}