
рдЖрдк рд╡рд┐рднрд┐рдиреНрди рдкреНрд░рдпреЛрдЬрдиреЛрдВ рдХреЗ рд▓рд┐рдП рдЗрд╕ рдореИрдиреБрдЕрд▓ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░ рд╕рдХрддреЗ рд╣реИрдВ:
- рдпрд╣ рд╕рдордЭрдиреЗ рдХреЗ рд▓рд┐рдП рдХрд┐ рд╕реНрдкреНрд░рд┐рдВрдЧ рдлреНрд░реЗрдорд╡рд░реНрдХ рдХреНрдпрд╛ рд╣реИ
 - рдЗрд╕рдХреА рдореБрдЦреНрдп рд╡рд┐рд╢реЗрд╖рддрд╛рдПрдВ рдХреИрд╕реЗ рдХрд╛рдо рдХрд░рддреА рд╣реИрдВ: рдЬреИрд╕реЗ рдХрд┐ рдирд┐рд░реНрднрд░рддрд╛ рдЗрдВрдЬреЗрдХреНрд╢рди рдпрд╛ рд╡реЗрдм рдПрдорд╡реАрд╕реА
 - рдпрд╣ рднреА рдПрдХ рд╕рд╛рдорд╛рдиреНрдп рдкреНрд░рд╢реНрди рд╣реИ (рдЕрдХреНрд╕рд░ рдкреВрдЫреЗ рдЬрд╛рдиреЗ рд╡рд╛рд▓реЗ рдкреНрд░рд╢реНрди рд╕реВрдЪреА)
 
рдиреЛрдЯ: ~ 9,000 рд╢рдмреНрджреЛрдВ рдХрд╛ рдПрдХ рд▓реЗрдЦ рд╢рд╛рдпрдж рдореЛрдмрд╛рдЗрд▓ рдбрд┐рд╡рд╛рдЗрд╕ рдкрд░ рдкрдврд╝рдиреЗ рд▓рд╛рдпрдХ рдирд╣реАрдВ рд╣реИред рдЗрд╕реЗ рдмреБрдХрдорд╛рд░реНрдХ рдХрд░реЗрдВ рдФрд░ рдмрд╛рдж рдореЗрдВ рд╡рд╛рдкрд╕ рдЖрдПрдВред рдФрд░ рдпрд╣рд╛рдВ рддрдХ тАЛтАЛрдХрд┐ рдХрдВрдкреНрдпреВрдЯрд░ рдкрд░, рдЗрд╕ рд╣рд╛рдереА рдХреЛ рдПрдХ рд╕рдордп рдореЗрдВ рдПрдХ рдЯреБрдХрдбрд╝рд╛ рдЦрд╛рдПрдВ :-)
рд╕рд╛рдордЧреНрд░реА
- рдкрд░рд┐рдЪрдп
 - рдирд┐рд░реНрднрд░рддрд╛ рдЗрдВрдЬреЗрдХреНрд╢рди рдореВрд▓ рдмрд╛рддреЗрдВ
 - рд╕реНрдкреНрд░рд┐рдВрдЧ IOC / рдирд┐рд░реНрднрд░рддрд╛ рдЗрдВрдЬреЗрдХреНрд╢рди рдХрдВрдЯреЗрдирд░
 - Spring AOP (- )
 - Spring
 - Spring Web MVC
 - Spring Framework
 - Spring Framework:
 
:
. .
Spring
Spring, spring.io, , Spring 21 . !
, Spring , , Spring Boot Spring Data.
, : Spring Framework. ?
, Spring Framework . Spring Boot, Spring Data, Spring Batch тАФ Spring.
:
- Spring Framework . , . Spring Boot, , , Spring Framework .
 - ~ 15 , 80% Spring Framework, .
 
Spring Framework?
:
Spring Framework , (: , , - , RPC, - MVC). Java-.
, ?
, :
.
, , Spring IOC / Dependency Injection. .
?
, Java , . DAO ( ) . , UserDAO.
public class UserDao {
    public User findById(Integer id) {
        
    }
}
UserDAO , .
SQL , UserDAO . Java , , , DataSource. , :
import javax.sql.DataSource;
public class UserDao {
    public User findById(Integer id) throws SQLException {
        try (Connection connection = dataSource.getConnection()) { 
               PreparedStatement selectStatement = connection.prepareStatement("select * from users where id =  ?");
               
        }
    }
}
- , UserDao ? , DAO SQL-.
 
new()
, , . , MySQL UserDAO :
import com.mysql.cj.jdbc.MysqlDataSource;
public class UserDao {
    public User findById(Integer id) {
        MysqlDataSource dataSource = new MysqlDataSource(); 
        dataSource.setURL("jdbc:mysql://localhost:3306/myDatabase");
        dataSource.setUser("root");
        dataSource.setPassword("s3cr3t");
        try (Connection connection = dataSource.getConnection()) { 
             PreparedStatement selectStatement = connection.prepareStatement("select * from users where id =  ?");
             
             return user;
        }
    }
}
- MySQL, MysqlDataSource url/username/password .
 - .
 
, , , UserDao , findByFirstName.
, . UserDAO , newDataSource.
import com.mysql.cj.jdbc.MysqlDataSource;
public class UserDao {
    public User findById(Integer id) {
        try (Connection connection = newDataSource().getConnection()) { 
               PreparedStatement selectStatement = connection.prepareStatement("select * from users where id =  ?");
               
        }
    }
    public User findByFirstName(String firstName) {
        try (Connection connection = newDataSource().getConnection()) { 
               PreparedStatement selectStatement = connection.prepareStatement("select * from users where first_name =  ?");
               
        }
    }
    public DataSource newDataSource() {
        MysqlDataSource dataSource = new MysqlDataSource(); 
        dataSource.setUser("root");
        dataSource.setPassword("s3cr3t");
        dataSource.setURL("jdbc:mysql://localhost:3306/myDatabase");
        return dataSource;
    }
}
- findById newDataSource().
 - findByFirstName newDataSource().
 - , .
 
, :
, ProductDAO, SQL? ProductDAO DataSource, UserDAO. , DataSource.
SQL. , DataSource - Java- . . , , , . UserDao, , DAO.
, Application, :
import com.mysql.cj.jdbc.MysqlDataSource;
public enum Application {
    INSTANCE;
    private DataSource dataSource;
    public DataSource dataSource() {
        if (dataSource == null) {
            MysqlDataSource dataSource = new MysqlDataSource();
            dataSource.setUser("root");
            dataSource.setPassword("s3cr3t");
            dataSource.setURL("jdbc:mysql://localhost:3306/myDatabase");
            this.dataSource = dataSource;
        }
        return dataSource;
    }
}
UserDAO :
import com.yourpackage.Application;
public class UserDao {
    public User findById(Integer id) {
        try (Connection connection = Application.INSTANCE.dataSource().getConnection()) { 
               PreparedStatement selectStatement = connection.prepareStatement("select * from users where id =  ?");
               
        }
    }
    public User findByFirstName(String firstName) {
        try (Connection connection = Application.INSTANCE.dataSource().getConnection()) { 
               PreparedStatement selectStatement = connection.prepareStatement("select * from users where first_name =  ?");
               
        }
    }
}
:
- UserDAO DataSource, Application . DAO.
 - ( , INSTANCE), DataSource.
 
:
- UserDAO , , тЖТ Application.INSTANCE.dataSource().
 - , , Application.java, . / ..
 
(IoC, Inversion of Control)
.
, UserDAO ? , Application.INSTANCE.dataSource(), UserDAO (-) , , , / / ?
, (Inversion of Control).
, UserDAO, .
import javax.sql.DataSource;
public class UserDao {
    private DataSource dataSource;
    private UserDao(DataSource dataSource) { 
        this.dataSource = dataSource;
    }
    public User findById(Integer id) {
        try (Connection connection = dataSource.getConnection()) { 
               PreparedStatement selectStatement = connection.prepareStatement("select * from users where id =  ?");
               
        }
    }
    public User findByFirstName(String firstName) {
        try (Connection connection = dataSource.getConnection()) { 
               PreparedStatement selectStatement = connection.prepareStatement("select * from users where first_name =  ?");
               
        }
    }
}
- , UserDao , .
 - findByX .
 
UserDao . , , . , ┬л ( ) , ┬╗.
, . ┬лnew UserService()┬╗, UserDao(dataSource).
public class MyApplication {
    public static void main(String[] args) {
        UserDao userDao = new UserDao(Application.INSTANCE.dataSource());
        User user1 = userDao.findById(1);
        User user2 = userDao.findById(2);
        
    }
}
, , , , UserDAO , , DataSource .
, - , UserDAO DataSource, , ? : DataSource UserDao?
- , Spring.
Spring IOC / Dependency Injection
, Spring Framework , (. ). , .
ApplicationContext? ?
, (: ), ApplicationContext Spring.
, ( UserDao DataSource , , ):
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import javax.sql.DataSource;
public class MyApplication {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(someConfigClass); 
        UserDao userDao = ctx.getBean(UserDao.class); // (2)
        User user1 = userDao.findById(1);
        User user2 = userDao.findById(2);
        DataSource dataSource = ctx.getBean(DataSource.class); // (3)
        
    }
}
- Spring ApplicationContext. , , .
 - ApplicationContext UserDao, DataSource.
 - ApplicationContext , , UserDao.
 
, ? , , , ApplicationContext !
?
ApplicationContextConfiguration? ApplicationContexts .
someConfigClass AnnotationConfigApplicationContext. :
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyApplication {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(someConfigClass); 
        
    }
}
, ApplicationContext, , :
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyApplicationContextConfiguration {  
    @Bean
    public DataSource dataSource() {  
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUser("root");
        dataSource.setPassword("s3cr3t");
        dataSource.setURL("jdbc:mysql://localhost:3306/myDatabase");
        return dataSource;
    }
    @Bean
    public UserDao userDao() { 
        return new UserDao(dataSource());
    }
}
- ApplicationContext, 
@Configuration, Application.java . - , DataSource 
@Bean Spring. - , UserDao UserDao, bean- dataSource.
 
Spring.
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyApplication {
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(MyApplicationContextConfiguration.class);
        UserDao userDao = ctx.getBean(UserDao.class);
        // User user1 = userDao.findById(1);
        
        DataSource dataSource = ctx.getBean(DataSource.class);
    }
}
, Spring AnnotationConfigApplicationContext , .
AnnotationConfigApplicationContext? ApplicationContext?
Spring ApplicationContext, , XML, Java . ApplicationContext.
MyApplicationContextConfiguration . Java, Spring. ConfigApplicationContext.
ApplicationContext XML, ClassPathXmlApplicationContext.
, Spring , .
@Bean? Spring Bean?
ApplicationContext . , , UserDao, , DataSource.
, , bean-. : ( Spring) , .
: Spring?
Spring bean scope?
DAO Spring? , bean scope ( ).
- Spring singleton: DAO ?
 - Spring prototype: DAO ?
 - , : HttpRequest? HttpSession? WebSocket?
 
, , .
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyApplicationContextConfiguration {
    @Bean
    @Scope("singleton")
    
    public DataSource dataSource() {
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUser("root");
        dataSource.setPassword("s3cr3t");
        dataSource.setURL("jdbc:mysql://localhost:3306/myDatabase");
        return dataSource;
    }
}
(scope annotation) , Spring. , , :
- Scope("singleton") тЖТ , .. .
 - Scope("prototype") тЖТ , - , Spring . ( , , ).
 - Scope("session") тЖТ HTTP .
 - ..
 
: Spring bean-, bean- (, , , websocket ..).
, ApplicationContexts, Beans & Scopes, , UserDAO DataSource.
Spring Java Config?
bean- ApplicationContext @Bean Java.
, Spring Java Config, XML, Spring. , :
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyApplicationContextConfiguration {
    @Bean
    public DataSource dataSource() {
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUser("root");
        dataSource.setPassword("s3cr3t");
        dataSource.setURL("jdbc:mysql://localhost:3306/myDatabase");
        return dataSource;
    }
    @Bean
    public UserDao userDao() { 
        return new UserDao(dataSource());
    }
}
- : UserDao() dataSource()? Spring ?
 
@ComponentScan.
@ComponentScan?
, , тАФ @ComponentScan.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan  
public class MyApplicationContextConfiguration {
    @Bean
    public DataSource dataSource() {
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setUser("root");
        dataSource.setPassword("s3cr3t");
        dataSource.setURL("jdbc:mysql://localhost:3306/myDatabase");
        return dataSource;
    }
    
    
}
@ComponentScan.- , UserDAO !
 
, @ComponentScan, Spring: Java , , Spring Bean!
, , MyApplicationContextConfiguration com.marcobehler, Spring , , com.marcobehler Spring.
Spring , - Spring? : , @Component.
@Component @Autowired?
@Component UserDAO.
import javax.sql.DataSource;
import org.springframework.stereotype.Component;
@Component
public class UserDao {
    private DataSource dataSource;
    private UserDao(DataSource dataSource) { 
        this.dataSource = dataSource;
    }
}
- Spring, 
@Bean, : , , @Component @ComponentScan, Spring, , ! 
( , @Controller, @Service @Repository, , , , @Component!).
. Spring , DataSource, @Bean, UserDAO DataSource?
, : @Autowired. , .
import javax.sql.DataSource;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
@Component
public class UserDao {
    private DataSource dataSource;
    private UserDao(@Autowired DataSource dataSource) {
        this.dataSource = dataSource;
    }
}
Spring , bean- UserDAO:
- UserDAO 
@Component тЖТ Spring - UserDAO 
@Autowired тЖТ Spring , @Bean - Spring , NoSuchBeanDefinition .
 
Autowired
- . Spring ( 4.2) @Autowired, .
Spring , @Autowired . .
@Component
public class UserDao {
    private DataSource dataSource;
    private UserDao(DataSource dataSource) {
        this.dataSource = dataSource;
    }
}
@Autowired ? , @Autowired , .
тАФ тАФ .
Field Injection? Setter Injection?
, Spring .
.
import javax.sql.DataSource;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
@Component
public class UserDao {
    @Autowired
    private DataSource dataSource;
}
, Spring .
import javax.sql.DataSource;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
@Component
public class UserDao {
    private DataSource dataSource;
    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
}
(, ) , : Spring Bean. , , .
, , , , .
, , (setter) .
:
- , . , .
 - : 80% , 10% 10%.
 - Spring : / . : .
 
Spring IoC
, Spring.
, , ApplicationContexts, Beans, , .
, Spring, .
Spring AOP (- )
, , Spring. ApplicationContextConfiguration:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyApplicationContextConfiguration {
    @Bean
    public UserService userService() { 
        return new UserService();
    }
}
- , UserService тАФ , .
 
Spring:
Spring , @Bean, , Spring , UserService.
Spring - , UserService. ? ?
Spring
Spring @Bean , ( ) UserService, .
.
- - UserService, , .

, Spring - Cglib, - (, - JDK): Cglib - . ( , .)
Spring ?
Spring . , , - (: AOP) .
AOP тАФ Spring @Transactional.
Spring @Transactional
UserService :
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
@Component
public class UserService {
    @Transactional           
    public User activateUser(Integer id) {  
        
        
        
    }
}
- activUser, SQL-, , , .
 @Transactional Spring, / . Spring .
: Spring UserService applicationContext, UserService. , .
, UserService, . , - , , UserService.
ContextConfiguration.
@Configuration
@EnableTransactionManagement 
public class MyApplicationContextConfiguration {
    @Bean
    public UserService userService() { 
        return new UserService();
    }
}
- , Spring: , 
@Transactional, Cglib . - Spring UserService . Cglib- , , UserService, UserService .
 
, Spring . - Spring : , тАФ . , , .
Spring Cglib?
AOP Spring. , AspectJ, -. AspectJ .
: Spring AOP AspectJ?
Spring AOP
, - , , Spring AOP, @Transactional Spring Security, @Secured. AOP, .
, , @Transactional Spring, @Transactional.
Spring
. , Spring. Spring.
, Java HTTP FTP. URL Java .
, ? , - ( , packaged.jar).
, , , , , (URL-, , ).
: Spring. .
import org.springframework.core.io.Resource;
public class MyApplication {
    public static void main(String[] args) {
            ApplicationContext ctx = new AnnotationConfigApplicationContext(someConfigClass); 
            Resource aClasspathTemplate = ctx.getResource("classpath:somePackage/application.properties"); 
            Resource aFileTemplate = ctx.getResource("file:///someDirectory/application.properties"); 
            Resource anHttpTemplate = ctx.getResource("https://marcobehler.com/application.properties"); 
            Resource depends = ctx.getResource("myhost.com/resource/path/myTemplate.txt"); 
            Resource s3Resources = ctx.getResource("s3://myBucket/myFile.txt"); 
    }
}
- , ApplicationContext .
 - getResource() applicationContext , classpath:, Spring application classpath.
 - getResource() , file:, Spring .
 - getResource() , https: ( http), Spring .
 - , , . .
 - Spring Framework, , Spring Cloud, s3://.
 
, Spring . :
public interface Resource extends InputStreamSource {
    boolean exists();
    String getFilename();
    File getFile() throws IOException;
    InputStream getInputStream() throws IOException;
    
}
, :
, , , , .
, , Spring: Properties.
Spring Environment?
тАФ (properties), , , ..
.properties, :
Spring environment.
import org.springframework.core.env.Environment;
public class MyApplication {
    public static void main(String[] args) {
           ApplicationContext ctx = new AnnotationConfigApplicationContext(someConfigClass);
           Environment env = ctx.getEnvironment(); 
           String databaseUrl = env.getProperty("database.url"); 
           boolean containsPassword = env.containsProperty("database.password");
           
    }
}
- applicationContext (environment) Spring .
 - (environment), , , , .
 
environment?
Spring @PropertySources?
, Spring . :
- /mydir/application.properties
 - classpath:/application-default.properties
 
(. , ┬лdev┬╗ ┬лproduction┬╗, ).
- Spring MVC ServletConfig/Context, JNDI JVM. , , .
, @PropertySources :
import org.springframework.context.annotation.PropertySources;
import org.springframework.context.annotation.PropertySource;
@Configuration
@PropertySources(
        {@PropertySource("classpath:/com/${my.placeholder:default/path}/app.properties"),
         @PropertySource("file://myFolder/app-production.properties")})
public class MyApplicationContextConfiguration {
    
}
, Spring . .
@PropertySource Spring Spring: , : http://, file://, classpath: ..,
@PropertySources тАФ , , , ? , .
Spring @Value
bean-, , @Autowired. @Value.
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Value;
@Component
public class PaymentService {
    @Value("${paypal.password}")  
    private String paypalPassword;
     public PaymentService(@Value("${paypal.url}") String paypalUrl) { 
         this.paypalUrl = paypalUrl;
    }
}
@Value ...- .
 
. , @Value, Spring () тАФ , .
Spring Web MVC
Spring Web MVC, Spring MVC, - Spring. , , - -. , Spring Boot.
MVC?
MVC, Model-View-Controller , .
HTML-, , , MVC Spring:
- Model () , -. HTML, Java (, ), .
 - View () HTML-, HTML-, . , , , .
 - Controller () 
@Controller, HTTP- /account , HTTP- Java, Java HTML. 
, Spring, HTTP-, (/account), HTML-, , .
- JSON XML.
, , - -- Java: API Java ( Spring MVC). ).
HttpServlet
MVC: -, HTTP Java, , , HttpServlets ( : , , ). HTTP-, HTTP-.
, Tomcat Jetty. , , URL - . "/*", HTTP- .
:
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServlet extends HttpServlet { 
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) { 
        if (request.getRequestURI().startsWith("/account")) {
            String userId = request.getParameter("userId");
            
        } else if (request.getRequestURI().startsWith("/status")) {
            
        } 
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) { 
        
    }
}
- Java HttpServlet.
 - doGet(), Http GET. "/" GET. , ┬л/status┬╗, ┬л/info┬╗, ┬л/account┬╗ doGet.
 - doPost() POST Http. "/" POST. , ┬л/register┬╗, ┬л/submit-form┬╗, ┬л/password-recovery┬╗ doPost().
 
тАФ , , MVC, .
, MyServlet () HTTP- , URI , , requestParameters .
, Spring . DispatcherServlet.
DispatcherServlet?
Uber- Spring MVC Spring , DispatcherServlet.
DispatcherServlet, HTTP-, Java Controller.
, HTML / JSON / XML, , . ( , DispatcherServlet ).

, .
- Spring HTTP .
 - .
 - () ( Java ).
 
Controller .
Controller
, Controller , /account.
/account HTML- , , , ..
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Controller 
public class AccountController {
    @GetMapping("/account/{userId}")
    public String account(@PathVariable Integer userId) { 
        
        return "templates/account"; 
    }
}
, , .
AccountController, @Controller. Spring: HTTP- , DispatcherServlet .
Java-, account(). , @GetMapping. DispatcherServlet, , /account/{userId}, . , dispatcherServlet {userId}, !
Java account. , (HTML-). .
HTML (view) Spring Web MVC
Spring MVC , HTML. , , HTML- , , Velocity Freemarker. Spring .
, ():
classpath:/templates/account.vm
<html>
  <body>
    Hello $user.name, this is your account!
    
  </body>
</html>
HTML-, , $user.name. ? . .
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@Controller
public class AccountController {
    @GetMapping("/account/{userId}")
    public String account(Model model, @PathVariable Integer userId) { 
        
        model.addAttribute("user", userDao.findById(userId)); 
        return "templates/account"; 
    }
}
- Spring "Model" . , , , .. .
 - Spring map (), .
 - , view, , .
 
, Spring MVC !
JSON / XML () Spring Web MVC
- HTML, XML JSON. , Spring MVC .
, , Jackson, , Controller , Spring: , Java XML / JSON, , .
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HealthController {
    @GetMapping("/health")
    @ResponseBody 
    public HealthStatus health() {
        return new HealthStatus(); 
    }
}
@ResponseBody , Spring, Java- HealthStatus HttpResponse (, XML JSON).- Java- , .
 
Spring , XML, JSON - ?
(content negotiation) Spring MVC
, , , Spring MVC, Spring MVC.
- Accept, ┬лAccept: application/json┬╗ ┬лAccept: application/xml┬╗. " ", XML JSON.
 - URL , /health.json /health.xml. Spring MVC .
 - , /health?Format = json. Spring MVC .
 
Spring : @ResponseBody, ┬лJSON┬╗. , , JSON? , , HealthStatus JSON. .
, .
HTTP- Spring?
Spring MVC , HTTP тАФ .
, JSON, XML HTTP (Multipart) Fileupload, Spring Java.
HTTP- Spring MVC?
Spring MVC HttpServletResponse тАФ .
HTML, JSON, XML WebSocket. , Java .
Spring MVC?
Spring MVC , , -.
, RequestParams, , , ViewHandlers, RootContexts, , , . , .
.
: Spring Boot
Spring Boot, , @RestController, , , Spring Boot.
, Spring Boot ( ), , , Spring MVC. :
@Controller
@ResponseBody
public @interface RestController {
}
, Spring MVC @RestController тАФ , Spring MVC @Controller Spring MVC @ResponseBody тАФ , - Spring Boot.
: Spring MVC
Spring MVC тАФ MVC, HTML / JSON / XML - -. Spring, .
: Java-, , , Http- , .
Spring Framework
IoC Spring, Spring Web MVC Spring. . ?
Spring Framework?
Spring Framework , . 20 Spring spring.io. , Spring.
, ?
, , Spring , Java. JDK, . Spring Framework .
: Java API , , , . .
Spring API- Java Mail API, , , , Spring.
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
public class SpringMailSender {
    @Autowired
    private JavaMailSender mailSender; 
    public void sendInvoice(User user, File pdf) throws Exception {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); 
        helper.setTo("john@rambo.com");
        helper.setText("Check out your new invoice!");
        FileSystemResource file = new FileSystemResource(pdf);
        helper.addAttachment("invoice.pdf", file);
        mailSender.send(mimeMessage);
    }
}
- , (URL, , ), MailSender, Spring, , .
 - Spring , MimeMessageHelper, , , , .
 
, , Spring Framework , ┬л┬╗ Java, , , API Spring.
Spring Framework?
, , Spring. , . .
Spring Framework: FAQ
Spring ?
Spring :
- Spring Boot, Spring . , Spring Boot 2.2.x, Spring 5.2.x ( ).
 - Spring , , . Spring 5.2.3.RELEASE, Spring.
 - Spring , Spring, ( EOL ) тАФ Spring (. ).
 
Spring 4.x-5.x, , , Spring 3.x ( : 2009).
Spring? ?
, Spring:

, Spring ~ 17 , 3-4 . , .
- , Spring 4.3 2016 2020 .
 - Spring 5.0 5.1 2020 , Spring 5.2, 2019 .
 
. EOL() ( ) Spring, 5.2, 31 2020 .
, Spring?
, , Spring. spring-context. , Spring.
Maven Gradle, (. : Spring ?) тАФ .jar .
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.3.RELEASE</version>
</dependency>
// Gradle
compile group: 'org.springframework', name: 'spring-context', version: '5.2.3.RELEASE'
Spring ( Spring JDBC JMS) .
Spring, Maven artifactIds . :
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.2.3.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>5.2.3.RELEASE</version>
</dependency>
Spring?
JVM, Spring , , ( ) - Spring 1.0 xml Spring 5.0 ( , , ). , , , 3 5 (. ).
, , Spring Spring ( , , Python 2 3). , , Spring 3 4, Spring 5.
, 7 Spring, :
:
( , ..) . Spring Java, , -, ..
20 Spring.io? -?
, , , , .
: Spring Framework .
Spring Spring Boot?
, , Spring Boot Spring. , Spring Boot, , ┬л ┬╗ Spring Boot.
Spring .properties , @PropertySource. JSON REST Web MVC.
, . Spring Boot, , . :
- application.properties тАЛтАЛ .
 - Tomcat, Rest .
 - / JSON, Maven / Gradle.
 
Java, @SpringBootApplication. , Spring Boot Maven / Gradle, .jar, :
java -jar mySpringBootApp.jar
, Spring Boot тАФ , , Spring Framework, .
Spring AOP AspectJ?
Spring Cglib?" Spring AOP . , . , AOP, Spring.
AspectJ, , - . .
Spring AOP AspectJ AOP .
, :
Spring Spring Batch?
Spring Batch тАФ , , .. ┬л 95 CSV- 3 ┬╗.
, Spring Framework, .
, , Spring Framework Spring Batch.
Spring Spring Web MVC?
, , Spring Web MVC Spring.
Spring- - DispatcherServlet, @Controller.
RestControllers ( XML JSON ) HTML-, HTML , Thymeleaf, Velocity Freemarker.
Spring Struts?
: Spring Web MVC Struts?
: Spring Web MVC Struts, , , Spring (. ).
, , Struts 2, , , Spring Web MVC , Spring. Spring Webflow RestControllers Spring Boot.
? Spring XML Java ?
Spring XML. , / Java .
, XML , , Java .
:
- , XML / / Java Config , .
 - Spring, XML, Java, .
 
? ?
, . , : 83% 17%.
Spring: , / , .
, : .
Spring?
, Java:
, Dagger , . Guice , ( Guice Persist).
, , Spring Framework.
, Spring ( Spring Boot Spring Data) , , ┬л Spring ?┬╗
, (~ = ).
Spring Framework тАФ DIY- (~ = ), , (~ = / ) (~ = Web MVC) . ( Java).
(рдзреНрдпрд╛рди рджреЗрдВ: рдпрд╣ рдордд рдкреВрдЫреЛ рдХрд┐ рдореИрдВ рдЗрди рддреБрд▓рдирд╛рдУрдВ рдХреЗ рд╕рд╛рде рдХреИрд╕реЗ рдЖрдпрд╛?))
рдЖрдЬ рдХреЗ рд▓рд┐рдП рдЗрддрдирд╛ рд╣реАред рдпрджрд┐ рдЖрдкрдХреЗ рдХреЛрдИ рдкреНрд░рд╢реНрди рдпрд╛ рд╕реБрдЭрд╛рд╡ рд╣реИрдВ, рддреЛ рдореБрдЭреЗ marco@marcobehler.com рдкрд░ рд▓рд┐рдЦреЗрдВ рдпрд╛ рдиреАрдЪреЗ рдПрдХ рдЯрд┐рдкреНрдкрдгреА рдЫреЛрдбрд╝ рджреЗрдВред рд╡реНрдпрд╛рд╡рд╣рд╛рд░рд┐рдХ рдЕрднреНрдпрд╛рд╕ рдХреЗ рд▓рд┐рдП, рд╕реНрдкреНрд░рд┐рдВрдЧ рдлреНрд░реЗрдорд╡рд░реНрдХ рдЯреНрдпреВрдЯреЛрд░рд┐рдпрд▓ рджреЗрдЦреЗрдВ ред
рдкрдврд╝рдиреЗ рдХреЗ рд▓рд┐рдП рдзрдиреНрдпрд╡рд╛рджред Auf Wiedersehenред
рд╕реНрд╡реАрдХреГрддрд┐рдпрд╛рдБ
рдмрд╣реБрдд рдзрдиреНрдпрд╡рд╛рдж:
- рдкреИрдЯреНрд░рд┐рдХрд┐рдпреЛ рдореЛрд╕реНрдХреЛрдХреЛрд╡рд┐рдЪ, рдиреЗ рдЗрд╕ рд▓реЗрдЦ рдХреЛ рдкрдврд╝рдиреЗ рдФрд░ рдЫреЛрдЯреА рдЧрд▓рддрд┐рдпреЛрдВ рдХрд╛ рдПрдХ рдЧреБрдЪреНрдЫрд╛ рдЗрдВрдЧрд┐рдд рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдПрдХ рдЕрджреНрднреБрдд рдХрд╛рдо рдХрд┐рдпрд╛ рд╣реИред
 - рдпрд╣ рдмрддрд╛рдиреЗ рдХреЗ рд▓рд┐рдП рдХрд┐ 
@RestControllerрд╡рд╣ рд╣рдореЗрд╢рд╛ рд╕реНрдкреНрд░рд┐рдВрдЧ рдПрдорд╡реАрд╕реА рдХрд╛ рд╣рд┐рд╕реНрд╕рд╛ рдерд╛, рди рдХрд┐ рд╕реНрдкреНрд░рд┐рдВрдЧ рдмреВрдЯред
(рдореВрд▓ рд▓реЗрдЦ рдХреЗ рд▓реЗрдЦрдХ рдХреЗ рд▓рд┐рдП рдзрдиреНрдпрд╡рд╛рдж - рд▓рдЧрднрдЧред рдЕрдиреБрд╡рд╛рджред)