Я учусь использовать среду Spring-Boot.
Я настраиваю часть модели. Поэтому я создал таблицы базы данных, используя аннотации @Entity, @Table
После создания таблиц я создал DAO для каждой из них.
Я следую инструкциям, но не делаю ничего, кроме него.
Единственное отличие, которое я сделал, — это добавил эту строку в " application.properties", иначе приложение не будет работать (это до реализации части модели). После создания части модели я столкнулся с этой новой проблемой
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Когда я запускаю и собираю проект, я получаю следующую ошибку, поскольку приложение не запускается. Вот результат:
:: Spring Boot :: (v2.2.2.RELEASE)
2020-01-17 10:59:41.645 INFO 12464 --- [ main] c.m.m.MyFirstArtifactApplication : Starting MyFirstArtifactApplication on SII-AS1 with PID 12464 (C:\Users\g.barbera\Desktop\my-first-artifact\target\classes started by g.barbera in C:\Users\g.barbera\Desktop\my-first-artifact)
2020-01-17 10:59:41.648 INFO 12464 --- [ main] c.m.m.MyFirstArtifactApplication : No active profile set, falling back to default profiles: default
2020-01-17 10:59:42.500 INFO 12464 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8094 (http)
2020-01-17 10:59:42.507 INFO 12464 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-01-17 10:59:42.507 INFO 12464 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.29]
2020-01-17 10:59:42.590 INFO 12464 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-01-17 10:59:42.590 INFO 12464 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 909 ms
2020-01-17 10:59:42.624 WARN 12464 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myFirstArtifactApplication': Unsatisfied dependency expressed through field 'userDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.myfirstgroup.myfirstartifact.daos.UserDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2020-01-17 10:59:42.626 INFO 12464 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-01-17 10:59:42.637 INFO 12464 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-01-17 10:59:42.702 ERROR 12464 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field userDao in com.myfirstgroup.myfirstartifact.MyFirstArtifactApplication required a bean of type 'com.myfirstgroup.myfirstartifact.daos.UserDao' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.myfirstgroup.myfirstartifact.daos.UserDao' in your configuration.
Process finished with exit code 1
Может ли кто-нибудь мне помочь?
Объекты:
- Пользователь
package com.myfirstgroup.myfirstartifact.entities;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@Entity
@Table(name = "users")
@AllArgsConstructor @NoArgsConstructor
public class User {
//String ID, String USERNAME, String PASSWORD, String PERMISSION
@Id //JPA id of the table
@Column(name = "ID") //JPA (if column name is different from variable name)
@NotEmpty @NotBlank @NotNull //Lombok annotation
@Getter @Setter //JSR-303 Validation
private String id;
@Column(name = "USERNAME") //JPA (if column name is different from variable name)
@NotEmpty @NotBlank @NotNull //Lombok annotation
@Getter @Setter //JSR-303 Validation
private String username;
@Column(name = "PASSWORD") //JPA (if column name is different from variable name)
@NotEmpty @NotBlank @NotNull //Lombok annotation
@Getter @Setter //JSR-303 Validation
private String password;
@Column(name = "PERMISSION") //JPA (if column name is different from variable name)
@NotEmpty @NotBlank @NotNull //Lombok annotation
@Getter @Setter //JSR-303 Validation
private String permission;
}
- Аккаунт
package com.myfirstgroup.myfirstartifact.entities;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@Entity
@Table(name = "accounts")
@AllArgsConstructor @NoArgsConstructor
public class Account {
//String ID, String FK_USER, Double TOTAL
@Id //JPA id of the table
@Column(name = "ID") //JPA (if column name is different from variable name)
@NotNull @NotBlank @NotEmpty //Lombok annotation
@Getter @Setter //JSR-303 Validation
private String id;
@Column(name = "FK_USER") //JPA (if column name is different from variable name)
@NotNull @NotBlank @NotEmpty //Lombok annotation
@Getter @Setter //JSR-303 Validation
private String fkUser;
@Column(name = "TOTAL") //JPA (if column name is different from variable name)
@Getter @Setter //JSR-303 Validation
private Double total;
}
- Работа
package com.myfirstgroup.myfirstartifact.entities;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;
@Entity
@Table(name = "operations")
@AllArgsConstructor @NoArgsConstructor
public class Operation {
//String ID, Date DATE, Double Value, String DESCRIPTION, String FK_ACCOUNT1, String FK_ACCOUNT2
@Id //JPA id of the table
@Column(name ="ID") //JPA (if column name is different from variable name)
@Getter @Setter //JSR-303 Validation
@NotEmpty @NotNull @NotBlank //Lombok annotation
private String id;
@Column(name ="DATE") //JPA (if column name is different from variable name)
@Getter @Setter //JSR-303 Validation
private Date date; //Lombok annotation
@Column(name ="DESCRIPTION") //JPA (if column name is different from variable name)
@Getter @Setter //JSR-303 Validation
private String description;
@Column(name ="VALUE") //JPA (if column name is different from variable name)
@Getter @Setter //JSR-303 Validation
@NotNull //Lombok annotation
private Double value;
@Column(name ="FK_ACCOUNT1") //JPA (if column name is different from variable name)
@Getter @Setter //JSR-303 Validation
@NotEmpty @NotNull @NotBlank //Lombok annotation
private String fkAccount1;
@Column(name ="FK_ACCOUNT2") //JPA (if column name is different from variable name)
@Getter @Setter //JSR-303 Validation
private String fkAccount2;
@PrePersist
void getTimeOperation(){
this.date = new Date();
}
-UserDao
package com.myfirstgroup.myfirstartifact.daos;
import com.myfirstgroup.myfirstartifact.entities.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
public interface UserDao extends JpaRepository {
Optional findById(String id);
}
- AccountDao
package com.myfirstgroup.myfirstartifact.daos;
import com.myfirstgroup.myfirstartifact.entities.Account;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
public interface AccountDao extends JpaRepository {
@Query(value = "SELECT * FROM accounts WHERE FK_USER=:user", nativeQuery = true)
List getAllAccountPerUser(@Param("user")String user);
List findByFkUser(String fkUser); - Операция Дао
package com.myfirstgroup.myfirstartifact.daos;
import com.myfirstgroup.myfirstartifact.entities.Operation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
public interface OperationDao extends JpaRepository {
@Query(value = "SELECT * FROM operations WHERE FK_ACCOUNT1:=account OR FK_ACCOUNT2:=account", nativeQuery = true)
List findAllOperationByAccount(@Param("account") String account);
package com.myfirstgroup.myfirstartifact;
import com.myfirstgroup.myfirstartifact.daos.AccountDao;
import com.myfirstgroup.myfirstartifact.daos.OperationDao;
import com.myfirstgroup.myfirstartifact.daos.UserDao;
import com.myfirstgroup.myfirstartifact.entities.Account;
import com.myfirstgroup.myfirstartifact.entities.Operation;
import com.myfirstgroup.myfirstartifact.entities.User;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import java.util.Date;
@SpringBootApplication
public class MyFirstArtifactApplication implements CommandLineRunner{
@Autowired
UserDao userDao;
@Autowired
AccountDao accountDao;
@Autowired
OperationDao operationDao;
private static final Logger log = LoggerFactory.getLogger(MyFirstArtifactApplication.class);
public static void main(String[] args) {
SpringApplication.run(MyFirstArtifactApplication.class, args);
System.out.println("ciao");
}
@Override
public void run(String... strings) throws Exception{
log.info("ciao23");
userDao.save(new User("AAABBB12C456D", "Aaaaaaa", "bbbbbbb", "user"));
userDao.save(new User("RSSMRA85T10A562S", "Mario Rossi", "abba", "user"));
userDao.save(new User("PLLPNC82B02G224Z", "Pinco Pallino", "salutpass", "user"));
accountDao.save(new Account("account_number_1","AAABBB12C456D",3000.00));
accountDao.save(new Account("account_number_2","AAABBB12C456D",4000.00));
accountDao.save(new Account("account_number_3","RSSMRA85T10A562S",7000.00));
accountDao.save(new Account("account_number_4","PLLPNC82B02G224Z",2000.00));
accountDao.save(new Account("account_number_5","PLLPNC82B02G224Z",8000.00));
operationDao.save(new Operation("3452",new Date(),"Bonifico Bancario",100.00,"account_number_1","account_number_3"));
operationDao.save(new Operation("3453",new Date(),"Pagamento Tasse",-100.00,"account_number_2","account_number_5"));
operationDao.save(new Operation("3454",new Date(),"Postagiro",230.00,"account_number_3","account_number_4"));
operationDao.save(new Operation("3455",new Date(),"Vaglia Postale",172.00,"account_number_1","account_number_5"));
operationDao.save(new Operation("3456",new Date(),"Acquisto Azioni",-3400.00,"account_number_2","account_number_4"));
operationDao.save(new Operation("3457",new Date(),"Vendita Azioni",100.00,"account_number_2","account_number_3"));
operationDao.save(new Operation("3458",new Date(),"Prelevamento",-100.00,"account_number_3",""));
operationDao.save(new Operation("3459",new Date(),"Deposito",1100.00,"account_number_5","account_number_1"));
}
}
pom.xml:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
com.myfirstgroup
my-first-artifact
0.0.1-SNAPSHOT
my-first-artifact
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
org.projectlombok
lombok
1.18.10
provided
javax.xml.bind
jaxb-api
2.3.1
org.springframework.boot
spring-boot-maven-plugin
application.properties:
server.port=8094
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
Подробнее здесь: https://stackoverflow.com/questions/597 ... figuration