Skip to content

Commit

Permalink
BAEL-2399: Migrate Spring DI example to spring-core module
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-martin committed Feb 6, 2019
1 parent 874acdd commit 01fa4cf
Show file tree
Hide file tree
Showing 26 changed files with 129 additions and 78 deletions.
64 changes: 24 additions & 40 deletions guice/pom.xml
Original file line number Diff line number Diff line change
@@ -1,45 +1,29 @@
<?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>com.baeldung.examples.guice</groupId>
<artifactId>guice</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>guice</name>
<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>com.baeldung.examples.guice</groupId>
<artifactId>guice</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>guice</name>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<dependencies>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${guice.version}</version>
</dependency>
<dependencies>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>${guice.version}</version>
</dependency>
</dependencies>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<properties>
<guice.version>4.1.0</guice.version>
</properties>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springtest.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
<guice.version>4.2.2</guice.version>
<spring.version>5.1.3.RELEASE</spring.version>
<springtest.version>5.1.3.RELEASE</springtest.version>
</properties>

</project>
</project>
3 changes: 0 additions & 3 deletions guice/src/main/java/com/baeldung/examples/common/Account.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.baeldung.examples.common;

import org.springframework.stereotype.Component;

@Component
public class Account {

private String accountNumber;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.baeldung.examples.common;

import org.springframework.stereotype.Component;

@Component
public class AccountServiceImpl implements AccountService {

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.baeldung.examples.common;

import org.springframework.beans.factory.annotation.Autowired;

public class BookServiceImpl implements BookService {

@Autowired(required = false)
private AuthorService authorService;

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.baeldung.examples.common;

import org.springframework.stereotype.Component;

@Component
public class PersonDaoImpl implements PersonDao {

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.baeldung.examples.guice;

import org.springframework.lang.Nullable;

import com.google.inject.Inject;

public class FooProcessor {

@Inject
@Nullable
private Foo foo;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected void configure() {
// });
bind(Foo.class).toProvider(new Provider<Foo>() {
public Foo get() {
return null;
return new Foo();
}
});
bind(PersonDao.class).to(PersonDaoImpl.class);
Expand Down
27 changes: 27 additions & 0 deletions spring-core/src/main/java/com/baeldung/di/spring/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.baeldung.di.spring;

import org.springframework.stereotype.Component;

@Component
public class Account {

private String accountNumber;
private String type;

public String getAccountNumber() {
return accountNumber;
}

public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.di.spring;

public interface AccountService {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.di.spring;

import org.springframework.stereotype.Component;

@Component
public class AccountServiceImpl implements AccountService {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.di.spring;

public interface AudioBookService {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.di.spring;

public class AudioBookServiceImpl implements AudioBookService {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.di.spring;

public interface AuthorService {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.di.spring;

public class AuthorServiceImpl implements AuthorService {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.di.spring;

public interface BookService {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.baeldung.di.spring;

import org.springframework.beans.factory.annotation.Autowired;

public class BookServiceImpl implements BookService {

@Autowired(required = false)
private AuthorService authorService;

}
4 changes: 4 additions & 0 deletions spring-core/src/main/java/com/baeldung/di/spring/Foo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.baeldung.di.spring;

public class Foo {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.baeldung.di.spring;

public class FooProcessor {

private Foo foo;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.baeldung.di.spring;

public interface PersonDao {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.baeldung.di.spring;

import org.springframework.stereotype.Component;

@Component
public class PersonDaoImpl implements PersonDao {

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.baeldung.examples.spring;
package com.baeldung.di.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baeldung.examples.common.AudioBookService;
import com.baeldung.examples.common.AudioBookServiceImpl;

@Configuration
public class SpringBeansConfig {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.baeldung.examples.spring;
package com.baeldung.di.spring;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.baeldung.examples.common.BookService;
import com.baeldung.examples.common.BookServiceImpl;

@Configuration
@Import({ SpringBeansConfig.class })
@ComponentScan("com.baeldung.examples")
@ComponentScan("com.baeldung.di.spring")
public class SpringMainConfig {

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.baeldung.examples.spring;
package com.baeldung.di.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baeldung.examples.common.PersonDao;

@Component
public class SpringPersonService {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.baeldung.examples.spring;
package com.baeldung.di.spring;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baeldung.examples.common.AccountService;

@Component
public class UserService {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.baeldung.examples;
package com.baeldung.di.spring;

import static org.junit.Assert.assertNotNull;

Expand All @@ -9,12 +9,6 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import com.baeldung.examples.common.AudioBookService;
import com.baeldung.examples.common.BookService;
import com.baeldung.examples.spring.SpringMainConfig;
import com.baeldung.examples.spring.SpringPersonService;
import com.baeldung.examples.spring.UserService;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { SpringMainConfig.class })
public class SpringUnitTest {
Expand Down

0 comments on commit 01fa4cf

Please sign in to comment.