BAEL-2399: Migrate Spring DI example to spring-core module

This commit is contained in:
eric-martin 2019-02-05 22:14:51 -06:00
parent 874acddfcf
commit 01fa4cfaf8
26 changed files with 129 additions and 78 deletions

View File

@ -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>

View File

@ -1,8 +1,5 @@
package com.baeldung.examples.common;
import org.springframework.stereotype.Component;
@Component
public class Account {
private String accountNumber;

View File

@ -1,8 +1,5 @@
package com.baeldung.examples.common;
import org.springframework.stereotype.Component;
@Component
public class AccountServiceImpl implements AccountService {
}

View File

@ -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;
}

View File

@ -1,8 +1,5 @@
package com.baeldung.examples.common;
import org.springframework.stereotype.Component;
@Component
public class PersonDaoImpl implements PersonDao {
}

View File

@ -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;
}

View File

@ -27,7 +27,7 @@ public class GuiceModule extends AbstractModule {
// });
bind(Foo.class).toProvider(new Provider<Foo>() {
public Foo get() {
return null;
return new Foo();
}
});
bind(PersonDao.class).to(PersonDaoImpl.class);

View File

@ -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;
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public interface AccountService {
}

View File

@ -0,0 +1,8 @@
package com.baeldung.di.spring;
import org.springframework.stereotype.Component;
@Component
public class AccountServiceImpl implements AccountService {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public interface AudioBookService {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public class AudioBookServiceImpl implements AudioBookService {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public interface AuthorService {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public class AuthorServiceImpl implements AuthorService {
}

View File

@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public interface BookService {
}

View File

@ -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;
}

View File

@ -0,0 +1,4 @@
package com.baeldung.di.spring;
public class Foo {
}

View File

@ -0,0 +1,6 @@
package com.baeldung.di.spring;
public class FooProcessor {
private Foo foo;
}

View File

@ -0,0 +1,5 @@
package com.baeldung.di.spring;
public interface PersonDao {
}

View File

@ -0,0 +1,8 @@
package com.baeldung.di.spring;
import org.springframework.stereotype.Component;
@Component
public class PersonDaoImpl implements PersonDao {
}

View File

@ -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 {

View File

@ -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

View File

@ -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 {

View File

@ -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 {

View File

@ -1,4 +1,4 @@
package com.baeldung.examples;
package com.baeldung.di.spring;
import static org.junit.Assert.assertNotNull;
@ -9,12 +9,6 @@ import org.springframework.context.ApplicationContext;
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 {