我有一个使用spring boot运行的应用程序。我需要使用hibernate。我得到了这个例外。我不确定我应该拥有的配置文件。因为我已经搜索了很多新的东西:我有pom.xml,我不知道我是否应该使用web.xml。我需要尽快让它工作。这是我的例外:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'userController' defined in file
[C:\Users\Mehrnaz\JEEKeplerWorkspace\rest-stack\target\classes\com\jl\crm\contoller\UserController.class]:
Unsatisfied dependency expressed through constructor argument with index 0 of type
[com.jl.crm.services.CrmService]: : Error creating bean with name 'jpaCrmService' defined in file
[C:\Users\Mehrnaz\JEEKeplerWorkspace\rest-stack\target\classes\com\jl\crm\services\JpaCrmService.class]:
Unsatisfied dependency expressed through constructor argument with index 0 of type
[com.jl.crm.repository.CustomerRepository]: : No qualifying bean of type
[com.jl.crm.repository.CustomerRepository] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {};
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.jl.crm.repository.CustomerRepository]
found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {};
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'jpaCrmService' defined in file
[C:\Users\Mehrnaz\JEEKeplerWorkspace\rest-stack\target\classes\com\jl\crm\services\JpaCrmService.class]:
Unsatisfied dependency expressed through constructor argument with index 0 of type
[com.jl.crm.repository.CustomerRepository]: : No qualifying bean of type
[com.jl.crm.repository.CustomerRepository] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
=======
我的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId>
<artifactId>rest-stack</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.springframework.hateoas</groupId>
<artifactId>spring-hateoas</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- MySQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- other project -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring3</artifactId>
<version>2.0.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我的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">
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.jl.crm.services.ServiceConfiguration</param-value>
</context-param>
<filter>
<filter-name>hiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>hiddenHttpMethodFilter</filter-name>
<servlet-name>appServlet</servlet-name>
</filter-mapping>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
我有log4j.properties:
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# Root logger option
log4j.rootLogger=DEBUG,stdout
和Application.properties:
#
## Database configuration
#
spring.datasource.url= jdbc:mysql://localhost:3307/spring
spring.datasource.username= spring
spring.datasource.password= spring
spring.datasource.driverClassName= com.mysql.jdbc.Driver
spring.view.prefix=/WEB-INF/jsp/
spring.view.suffix=.jsp
这是service.configuration类,我认为可能有问题:
@ComponentScan
@Configuration
@EnableJpaRepositories
public class ServiceConfiguration {
public static final String CRM_NAME = "crm";
/**
* The root directory to which all uploads for the application are uploaded.
*/
public static final File CRM_STORAGE_DIRECTORY = new File(
System.getProperty("user.home"), CRM_NAME);
/**
* Things are first uploaded by the application server to this directory. it's a sort
* of staging directory
*/
public static final File CRM_STORAGE_UPLOADS_DIRECTORY = new File(CRM_STORAGE_DIRECTORY, "uploads");
/**
* When a profile photo is uploaded, the resultant, completely uploaded image is
* stored in this directory
*/
public static final File CRM_STORAGE_PROFILES_DIRECTORY = new File(CRM_STORAGE_DIRECTORY, "profiles");
@PostConstruct
protected void setupStorage() throws Throwable {
File[] files = {CRM_STORAGE_DIRECTORY, CRM_STORAGE_UPLOADS_DIRECTORY, CRM_STORAGE_PROFILES_DIRECTORY};
for (File f : files) {
if (!f.exists() && !f.mkdirs()) {
String msg = String.format("you must create the profile photos directory ('%s') " +
"and make it accessible to this process. Unable to do so from this process.", f.getAbsolutePath());
throw new RuntimeException(msg);
}
}
}
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory( JpaVendorAdapter adapter, DataSource dataSource) {
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
emf.setPackagesToScan(User.class.getPackage().getName());
emf.setDataSource(dataSource);
emf.setJpaVendorAdapter(adapter);
return emf;
}
@Bean
PlatformTransactionManager transactionManager( EntityManagerFactory emf) {
return new JpaTransactionManager(emf);
}
@Configuration
@Profile({"default", "test"})
static class DefaultDataSourceConfiguration {
private Log log = LogFactory.getLog(getClass());
@PostConstruct
protected void setupTestProfileImages() throws Exception {
long userId = 5;
File profilePhotoForUser5 = new File(ServiceConfiguration.CRM_STORAGE_PROFILES_DIRECTORY, Long.toString(userId));
if (!profilePhotoForUser5.exists()) {
// copy the profile photo back
String pathForProfilePhoto = "/sample-photos/spring-dog-2.png";
ClassPathResource classPathResource = new ClassPathResource(pathForProfilePhoto);
Assert.isTrue(classPathResource.exists(), "the resource " + pathForProfilePhoto + " does not exist");
OutputStream outputStream = new FileOutputStream(profilePhotoForUser5);
InputStream inputStream = classPathResource.getInputStream();
try {
IOUtils.copy(inputStream, outputStream);
} finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
}
log.debug("setup photo " + profilePhotoForUser5.getAbsolutePath() + " for the sample user #" + Long.toString(userId) + "'s profile photo.");
}
if (!profilePhotoForUser5.exists()) {
throw new RuntimeException("couldn't setup profile photos at " + profilePhotoForUser5.getAbsolutePath() + ".");
}
}
}
}
答案 0 :(得分:0)
String url = "<url_link for http or https>";
int timeout = 30*1000; // 30 seconds
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("<proxy_host>", <proxy_port>));
// if need to auth for proxy
Authenticator authenticator = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return (new PasswordAuthentication("<user>",
"<password>".toCharArray()));
}
};
Authenticator.setDefault(authenticator);
// open connection using proxy directly for this connection
// if not, setting in the JVM startup argus or using System.setProperty for app global scope
HttpURLConnection conn = new URL(url).openConnection(proxy); // Also HttpsURLConnection
conn.setConnectTimeout(timeout); // set Timeout
您错过了@ComponentScan
@Configuration(basePackages = {"<Specify your base package here>"})
@EnableJpaRepositories
public class ServiceConfiguration {
---
}
注释的基本包名称。