mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
2023-05 spring-01 added
This commit is contained in:
@@ -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-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,20 @@
|
||||
<?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>17</maven.compiler.target>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- TODO: Добавьте зависимость spring-context -->
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,17 @@
|
||||
package ru.otus.spring;
|
||||
|
||||
//import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO: создайте здесь класс контекста
|
||||
|
||||
// TODO: Получите Person Service
|
||||
|
||||
// Получите Person "Ivan"
|
||||
Person ivan = null;
|
||||
System.out.println("name: " + ivan.getName() + " age: " + ivan.getAge());
|
||||
}
|
||||
}
|
||||
@@ -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,21 @@
|
||||
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,11 @@
|
||||
<?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: определите бин класса ru.otus.spring.dao.PersonDaoSimple -->
|
||||
|
||||
<!-- TODO: определите бин класса ru.otus.spring.PersonServiceImpl -->
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,56 @@
|
||||
<?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>17</maven.compiler.target>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<spring.version>6.0.9</spring.version>
|
||||
<checkstyle-plugin.version>3.1.2</checkstyle-plugin.version>
|
||||
<checkstyle.version>10.9.1</checkstyle.version>
|
||||
<checkstyle.config.url>https://raw.githubusercontent.com/OtusTeam/Spring/master/checkstyle.xml</checkstyle.config.url>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Это зависимость spring-context, содержит базовую функциональность IoC -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-checkstyle-plugin</artifactId>
|
||||
<version>${checkstyle-plugin.version}</version>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.puppycrawl.tools</groupId>
|
||||
<artifactId>checkstyle</artifactId>
|
||||
<version>${checkstyle.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<configuration>
|
||||
<configLocation>${checkstyle.config.url}</configLocation>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,20 @@
|
||||
package ru.otus.spring;
|
||||
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.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());
|
||||
|
||||
// Данная операция, в принципе не нужна.
|
||||
// Мы не работаем пока что с БД, а Spring Boot сделает закрытие за нас
|
||||
// Подробности - через пару занятий
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
@@ -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,21 @@
|
||||
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,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.spring.dao.PersonDaoSimple">
|
||||
</bean>
|
||||
|
||||
<bean id="personService" class="ru.otus.spring.service.PersonServiceImpl">
|
||||
<constructor-arg index="0" ref="personDao"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user