2019-08 - spring-01 has been added

This commit is contained in:
Yuriy Dvorzhetskiy
2019-09-05 20:29:37 +06:00
parent be3ac01fe8
commit 45b5ab3cfe
18 changed files with 239 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
.idea/
*.iml
target/
+17
View File
@@ -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-01</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<modules>
<module>spring-01-exercise</module>
<module>spring-01-solution</module>
</modules>
</project>
@@ -0,0 +1,19 @@
<?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-01-exercise</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<!-- TODO: Добавьте зависимость spring-context -->
</dependencies>
</project>
@@ -0,0 +1,13 @@
package ru.otus.spring01;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ru.otus.spring01.domain.Person;
import ru.otus.spring01.service.PersonService;
public class Main {
public static void main(String[] args) {
Person ivan = null;
System.out.println("name: " + ivan.getName() + " age: " + ivan.getAge());
}
}
@@ -0,0 +1,8 @@
package ru.otus.spring01.dao;
import ru.otus.spring01.domain.Person;
public interface PersonDao {
Person findByName(String name);
}
@@ -0,0 +1,10 @@
package ru.otus.spring01.dao;
import ru.otus.spring01.domain.Person;
public class PersonDaoSimple implements PersonDao {
public Person findByName(String name) {
return new Person(name, 18);
}
}
@@ -0,0 +1,20 @@
package ru.otus.spring01.domain;
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
@@ -0,0 +1,8 @@
package ru.otus.spring01.service;
import ru.otus.spring01.domain.Person;
public interface PersonService {
Person getByName(String name);
}
@@ -0,0 +1,17 @@
package ru.otus.spring01.service;
import ru.otus.spring01.dao.PersonDao;
import ru.otus.spring01.domain.Person;
public class PersonServiceImpl implements PersonService {
private PersonDao dao;
public PersonServiceImpl(PersonDao dao) {
this.dao = dao;
}
public Person getByName(String name) {
return dao.findByName(name);
}
}
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- TODO: определите бины здесь -->
</beans>
@@ -0,0 +1,23 @@
<?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-01-solution</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.7.RELEASE</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,15 @@
package ru.otus.spring01;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import ru.otus.spring01.domain.Person;
import ru.otus.spring01.service.PersonService;
public class Main {
public static void main(String[] args) {
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,8 @@
package ru.otus.spring01.dao;
import ru.otus.spring01.domain.Person;
public interface PersonDao {
Person findByName(String name);
}
@@ -0,0 +1,10 @@
package ru.otus.spring01.dao;
import ru.otus.spring01.domain.Person;
public class PersonDaoSimple implements PersonDao {
public Person findByName(String name) {
return new Person(name, 18);
}
}
@@ -0,0 +1,20 @@
package ru.otus.spring01.domain;
public class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
@@ -0,0 +1,8 @@
package ru.otus.spring01.service;
import ru.otus.spring01.domain.Person;
public interface PersonService {
Person getByName(String name);
}
@@ -0,0 +1,17 @@
package ru.otus.spring01.service;
import ru.otus.spring01.dao.PersonDao;
import ru.otus.spring01.domain.Person;
public class PersonServiceImpl implements PersonService {
private PersonDao dao;
public PersonServiceImpl(PersonDao dao) {
this.dao = dao;
}
public Person getByName(String name) {
return dao.findByName(name);
}
}
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="personDao" class="ru.otus.spring01.dao.PersonDaoSimple">
</bean>
<bean id="personService" class="ru.otus.spring01.service.PersonServiceImpl">
<constructor-arg name="dao" ref="personDao"/>
</bean>
</beans>