BAEL-2399: Migrate Spring DI example to spring-core module
This commit is contained in:
parent
874acddfcf
commit
01fa4cfaf8
|
@ -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>
|
|
@ -1,8 +1,5 @@
|
|||
package com.baeldung.examples.common;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class Account {
|
||||
|
||||
private String accountNumber;
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
package com.baeldung.examples.common;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AccountServiceImpl implements AccountService {
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
}
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
package com.baeldung.examples.common;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PersonDaoImpl implements PersonDao {
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
public interface AccountService {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AccountServiceImpl implements AccountService {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
public interface AudioBookService {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
public class AudioBookServiceImpl implements AudioBookService {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
public interface AuthorService {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
public class AuthorServiceImpl implements AuthorService {
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
public interface BookService {
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
public class Foo {
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
public class FooProcessor {
|
||||
|
||||
private Foo foo;
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
public interface PersonDao {
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.di.spring;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PersonDaoImpl implements PersonDao {
|
||||
|
||||
}
|
|
@ -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 {
|
||||
|
|
@ -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
|
|
@ -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 {
|
||||
|
|
@ -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 {
|
||||
|
|
@ -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 {
|
Loading…
Reference in New Issue