package maxima.app;
import maxima.config.SpringConfig;
import maxima.models.Account;
import maxima.services.SignUpService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainAnnotation {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
SignUpService signUpService = (SignUpService) applicationContext.getBean("signUpServiceImpl");
signUpService.save(Account.builder()
.firstName("Nikolay")
.lastName("Ivanov")
.email("nick@mail.com")
.password("qwerty!@#45")
.build());
}
}
package maxima.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan(basePackages = "maxima")
@PropertySource("classpath:application.properties")
public class SpringConfig {
}
package maxima.services;
import maxima.models.Account;
import maxima.repository.AccountRepository;
import maxima.repository.AccountRepositoryImpl;
import maxima.validators.BlackListPasswords;
import maxima.validators.BlackListPasswordsImpl;
import maxima.validators.EmailValidator;
import maxima.validators.PasswordValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component("signUpServiceImpl")
public class SignUpServiceImpl implements SignUpService {
private final EmailValidator emailValidator;
private final PasswordValidator passwordValidator;
private final BlackListPasswords blackListPasswords;
private final AccountRepository accountRepository;
@Autowired
public SignUpServiceImpl(@Qualifier("emailByChar") EmailValidator emailValidator,
@Qualifier("passByChar") PasswordValidator passwordValidator,
BlackListPasswordsImpl blackListPasswords, AccountRepositoryImpl accountRepository) {
this.emailValidator = emailValidator;
this.passwordValidator = passwordValidator;
this.blackListPasswords = blackListPasswords;
this.accountRepository = accountRepository;
}
@Override
public void save(Account account) {
blackListPasswords.contains(account.getPassword());
emailValidator.validate(account.getEmail());
passwordValidator.validate(account.getPassword());
accountRepository.save(account);
}
}
package models;
import lombok.Data;
import org.springframework.stereotype.Component;
@Data
@Component("test")
public class Test {
private String name;
}
package config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "java")
public class SpringConfig {
}
package org.example;
import config.SpringConfig;
import models.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
int i =0;
Test test = (Test) applicationContext.getBean("test");
test.setName("TestName");
System.out.println(test.getName());
}
}