mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
2019-08 - 03 has been added
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
.idea/
|
||||
*.iml
|
||||
|
||||
target/
|
||||
@@ -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,40 @@
|
||||
<?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>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.1.9.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<!-- можно не писать - она транзитивна spring-context -->
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aop</artifactId>
|
||||
<version>5.1.9.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
<version>1.9.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>1.9.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,20 @@
|
||||
package ru.otus.spring03;
|
||||
|
||||
import org.springframework.context.annotation.*;
|
||||
import ru.otus.spring03.domain.Person;
|
||||
import ru.otus.spring03.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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package ru.otus.spring03.dao;
|
||||
|
||||
import ru.otus.spring03.domain.Person;
|
||||
|
||||
public interface PersonDao {
|
||||
|
||||
Person findByName(String name);
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.spring03.dao;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import ru.otus.spring03.domain.Person;
|
||||
|
||||
@Repository
|
||||
public class PersonDaoSimple implements PersonDao {
|
||||
|
||||
public Person findByName(String name) {
|
||||
return new Person(name, 18);
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package ru.otus.spring03.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;
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package ru.otus.spring03.logging;
|
||||
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
public class LoggingAspect {
|
||||
|
||||
public void logBefore(JoinPoint joinPoint) {
|
||||
System.out.println("Вызов метода : " + joinPoint.getSignature().getName());
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package ru.otus.spring03.service;
|
||||
|
||||
import ru.otus.spring03.domain.Person;
|
||||
|
||||
public interface PersonService {
|
||||
|
||||
Person getByName(String name);
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package ru.otus.spring03.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.otus.spring03.dao.PersonDao;
|
||||
import ru.otus.spring03.domain.Person;
|
||||
|
||||
@Service
|
||||
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,40 @@
|
||||
<?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>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.1.9.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<!-- можно не писать - она транзитивна spring-context -->
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-aop</artifactId>
|
||||
<version>5.1.9.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjrt</artifactId>
|
||||
<version>1.9.4</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.aspectj</groupId>
|
||||
<artifactId>aspectjweaver</artifactId>
|
||||
<version>1.9.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,21 @@
|
||||
package ru.otus.spring03;
|
||||
|
||||
import org.springframework.context.annotation.*;
|
||||
import ru.otus.spring03.domain.Person;
|
||||
import ru.otus.spring03.service.PersonService;
|
||||
|
||||
@EnableAspectJAutoProxy
|
||||
@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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package ru.otus.spring03.dao;
|
||||
|
||||
import ru.otus.spring03.domain.Person;
|
||||
|
||||
public interface PersonDao {
|
||||
|
||||
Person findByName(String name);
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package ru.otus.spring03.dao;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
import ru.otus.spring03.domain.Person;
|
||||
|
||||
@Repository
|
||||
public class PersonDaoSimple implements PersonDao {
|
||||
|
||||
public Person findByName(String name) {
|
||||
return new Person(name, 18);
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package ru.otus.spring03.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;
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package ru.otus.spring03.logging;
|
||||
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Before;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
public class LoggingAspect {
|
||||
|
||||
@Before("execution(* ru.otus.spring03.dao.PersonDaoSimple.*(..))")
|
||||
public void logBefore(JoinPoint joinPoint) {
|
||||
System.out.println("Вызов метода : " + joinPoint.getSignature().getName());
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package ru.otus.spring03.service;
|
||||
|
||||
import ru.otus.spring03.domain.Person;
|
||||
|
||||
public interface PersonService {
|
||||
|
||||
Person getByName(String name);
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package ru.otus.spring03.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.otus.spring03.dao.PersonDao;
|
||||
import ru.otus.spring03.domain.Person;
|
||||
|
||||
@Service
|
||||
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