mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
2021-03 - 03
This commit is contained in:
@@ -7,7 +7,8 @@ import ru.otus.spring.service.PersonService;
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
|
||||
ClassPathXmlApplicationContext context =
|
||||
new ClassPathXmlApplicationContext("/spring-context.xml");
|
||||
PersonService service = context.getBean(PersonService.class);
|
||||
Person ivan = service.getByName("Ivan");
|
||||
System.out.println("name: " + ivan.getName() + " age: " + ivan.getAge());
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/build/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
@@ -1,17 +1,38 @@
|
||||
package ru.otus.spring;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.ImportResource;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import ru.otus.spring.dao.PersonDao;
|
||||
import ru.otus.spring.dao.PersonDaoSimple;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.service.PersonService;
|
||||
import ru.otus.spring.service.PersonServiceImpl;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
|
||||
PersonService service = context.getBean(PersonService.class);
|
||||
AnnotationConfigApplicationContext context =
|
||||
new AnnotationConfigApplicationContext();
|
||||
|
||||
PersonDao dao = new PersonDaoSimple();
|
||||
|
||||
context.getBeanFactory().registerSingleton("personDao", dao);
|
||||
|
||||
PersonServiceImpl personService = new PersonServiceImpl();
|
||||
personService.setDao(dao);
|
||||
|
||||
context.getBeanFactory().registerSingleton("personService", personService);
|
||||
|
||||
context.refresh();
|
||||
|
||||
|
||||
PersonService service = (PersonService) context.getBean("personService");
|
||||
Person ivan = service.getByName("Ivan");
|
||||
System.out.println("name: " + ivan.getName() + " age: " + ivan.getAge());
|
||||
|
||||
|
||||
// Данная операция, в принципе не нужна.
|
||||
// Мы не работаем пока что с БД, а Spring Boot сделает закрытие за нас
|
||||
// Подробности - через пару занятий
|
||||
|
||||
@@ -2,7 +2,9 @@ package ru.otus.spring.dao;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface PersonDao {
|
||||
|
||||
Person findByName(String name);
|
||||
Optional<Person> findByName(String name);
|
||||
}
|
||||
|
||||
@@ -2,9 +2,22 @@ package ru.otus.spring.dao;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class PersonDaoSimple implements PersonDao {
|
||||
|
||||
public Person findByName(String name) {
|
||||
return new Person(name, 18);
|
||||
private final List<Person> persons = new ArrayList<>();
|
||||
|
||||
public PersonDaoSimple() {
|
||||
persons.add(new Person("Ivan", 18));
|
||||
persons.add(new Person("Masha", 18));
|
||||
}
|
||||
|
||||
public Optional<Person> findByName(String name) {
|
||||
return this.persons.stream()
|
||||
.filter(p -> p.getName().equals(name))
|
||||
.findAny();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,17 @@ import ru.otus.spring.domain.Person;
|
||||
|
||||
public class PersonServiceImpl implements PersonService {
|
||||
|
||||
private final PersonDao dao;
|
||||
private PersonDao dao;
|
||||
|
||||
public PersonServiceImpl(PersonDao dao) {
|
||||
this.dao = dao;
|
||||
public PersonServiceImpl() {
|
||||
}
|
||||
|
||||
public Person getByName(String name) {
|
||||
return dao.findByName(name);
|
||||
return dao.findByName(name)
|
||||
.orElseThrow(IllegalArgumentException::new);
|
||||
}
|
||||
|
||||
public void setDao(PersonDao dao) {
|
||||
this.dao = dao;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
<bean id="personDao" class="ru.otus.spring.dao.PersonDaoSimple">
|
||||
</bean>
|
||||
|
||||
<bean id="personService" class="ru.otus.spring.service.PersonServiceImpl">
|
||||
<constructor-arg name="dao" ref="personDao"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/build/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
@@ -0,0 +1,17 @@
|
||||
<?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>ru.otus</groupId>
|
||||
<artifactId>spring-03</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>spring-03-exercise</module>
|
||||
<module>spring-03-solution</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,24 @@
|
||||
<?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>ru.otus</groupId>
|
||||
<artifactId>spring-03-exercise</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.3.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,17 @@
|
||||
package ru.otus.spring;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.service.PersonService;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
AnnotationConfigApplicationContext context = null;
|
||||
|
||||
PersonService service = context.getBean(PersonService.class);
|
||||
|
||||
Person ivan = service.getByName("Ivan");
|
||||
System.out.println("name: " + ivan.getName() + " age: " + ivan.getAge());
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package ru.otus.spring.config;
|
||||
|
||||
import ru.otus.spring.dao.PersonDao;
|
||||
import ru.otus.spring.dao.PersonDaoSimple;
|
||||
|
||||
public class DaoConfig {
|
||||
|
||||
public PersonDao personDao() {
|
||||
return new PersonDaoSimple();
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.spring.config;
|
||||
|
||||
import ru.otus.spring.dao.PersonDao;
|
||||
import ru.otus.spring.service.PersonService;
|
||||
import ru.otus.spring.service.PersonServiceImpl;
|
||||
|
||||
public class ServicesConfig {
|
||||
|
||||
public PersonService personService(PersonDao dao) {
|
||||
return new PersonServiceImpl(dao);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package ru.otus.spring.dao;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public interface PersonDao {
|
||||
|
||||
Person findByName(String name);
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ru.otus.spring.dao;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public class PersonDaoSimple implements PersonDao {
|
||||
|
||||
public Person findByName(String name) {
|
||||
return new Person(name, 18);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package ru.otus.spring.domain;
|
||||
|
||||
public class Person {
|
||||
|
||||
private final String name;
|
||||
private final int age;
|
||||
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package ru.otus.spring.service;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public interface PersonService {
|
||||
|
||||
Person getByName(String name);
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package ru.otus.spring.service;
|
||||
|
||||
import ru.otus.spring.dao.PersonDao;
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public class PersonServiceImpl implements PersonService {
|
||||
|
||||
private final PersonDao dao;
|
||||
|
||||
public PersonServiceImpl(PersonDao dao) {
|
||||
this.dao = dao;
|
||||
}
|
||||
|
||||
public Person getByName(String name) {
|
||||
return dao.findByName(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?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>ru.otus</groupId>
|
||||
<artifactId>spring-03-solution</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.3.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,22 @@
|
||||
package ru.otus.spring;
|
||||
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.service.PersonService;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
AnnotationConfigApplicationContext context =
|
||||
new AnnotationConfigApplicationContext(Main.class);
|
||||
|
||||
PersonService service = context.getBean(PersonService.class);
|
||||
|
||||
Person ivan = service.getByName("Ivan");
|
||||
System.out.println("name: " + ivan.getName() + " age: " + ivan.getAge());
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.spring.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.otus.spring.dao.PersonDao;
|
||||
import ru.otus.spring.dao.PersonDaoSimple;
|
||||
|
||||
@Configuration
|
||||
public class DaoConfig {
|
||||
|
||||
@Bean
|
||||
public PersonDao personDao() {
|
||||
return new PersonDaoSimple();
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package ru.otus.spring.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.otus.spring.dao.PersonDao;
|
||||
import ru.otus.spring.service.PersonService;
|
||||
import ru.otus.spring.service.PersonServiceImpl;
|
||||
|
||||
@Configuration
|
||||
public class ServicesConfig {
|
||||
|
||||
@Bean
|
||||
public PersonService personService(PersonDao dao) {
|
||||
return new PersonServiceImpl(dao);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package ru.otus.spring.dao;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public interface PersonDao {
|
||||
|
||||
Person findByName(String name);
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ru.otus.spring.dao;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public class PersonDaoSimple implements PersonDao {
|
||||
|
||||
public Person findByName(String name) {
|
||||
return new Person(name, 18);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package ru.otus.spring.domain;
|
||||
|
||||
public class Person {
|
||||
|
||||
private final String name;
|
||||
private final int age;
|
||||
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package ru.otus.spring.service;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public interface PersonService {
|
||||
|
||||
Person getByName(String name);
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package ru.otus.spring.service;
|
||||
|
||||
import ru.otus.spring.dao.PersonDao;
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public class PersonServiceImpl implements PersonService {
|
||||
|
||||
private final PersonDao dao;
|
||||
|
||||
public PersonServiceImpl(PersonDao dao) {
|
||||
this.dao = dao;
|
||||
}
|
||||
|
||||
public Person getByName(String name) {
|
||||
return dao.findByName(name);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user