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,6 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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"> 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> <modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung.examples.guice</groupId> <groupId>com.baeldung.examples.guice</groupId>
@ -21,25 +20,10 @@
<artifactId>guice</artifactId> <artifactId>guice</artifactId>
<version>${guice.version}</version> <version>${guice.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${springtest.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<properties> <properties>
<guice.version>4.2.2</guice.version> <guice.version>4.1.0</guice.version>
<spring.version>5.1.3.RELEASE</spring.version>
<springtest.version>5.1.3.RELEASE</springtest.version>
</properties> </properties>
</project> </project>

View File

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

View File

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

View File

@ -1,10 +1,7 @@
package com.baeldung.examples.common; package com.baeldung.examples.common;
import org.springframework.beans.factory.annotation.Autowired;
public class BookServiceImpl implements BookService { public class BookServiceImpl implements BookService {
@Autowired(required = false)
private AuthorService authorService; private AuthorService authorService;
} }

View File

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

View File

@ -1,12 +1,9 @@
package com.baeldung.examples.guice; package com.baeldung.examples.guice;
import org.springframework.lang.Nullable;
import com.google.inject.Inject; import com.google.inject.Inject;
public class FooProcessor { public class FooProcessor {
@Inject @Inject
@Nullable
private Foo foo; private Foo foo;
} }

View File

@ -27,7 +27,7 @@ public class GuiceModule extends AbstractModule {
// }); // });
bind(Foo.class).toProvider(new Provider<Foo>() { bind(Foo.class).toProvider(new Provider<Foo>() {
public Foo get() { public Foo get() {
return null; return new Foo();
} }
}); });
bind(PersonDao.class).to(PersonDaoImpl.class); 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import com.baeldung.examples.common.AudioBookService;
import com.baeldung.examples.common.AudioBookServiceImpl;
@Configuration @Configuration
public class SpringBeansConfig { 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.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import com.baeldung.examples.common.BookService;
import com.baeldung.examples.common.BookServiceImpl;
@Configuration @Configuration
@Import({ SpringBeansConfig.class }) @Import({ SpringBeansConfig.class })
@ComponentScan("com.baeldung.examples") @ComponentScan("com.baeldung.di.spring")
public class SpringMainConfig { public class SpringMainConfig {
@Bean @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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.baeldung.examples.common.PersonDao;
@Component @Component
public class SpringPersonService { 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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.baeldung.examples.common.AccountService;
@Component @Component
public class UserService { public class UserService {

View File

@ -1,4 +1,4 @@
package com.baeldung.examples; package com.baeldung.di.spring;
import static org.junit.Assert.assertNotNull; 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.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; 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) @RunWith(SpringRunner.class)
@ContextConfiguration(classes = { SpringMainConfig.class }) @ContextConfiguration(classes = { SpringMainConfig.class })
public class SpringUnitTest { public class SpringUnitTest {