BAEL-812: List of Rules Engines in Java (#2319)
* spring beans DI examples * fix-1: shortening examples * List of Rules Engines in Java * BAEL-812: Openl-Tablets example added * BAEL-812: artifacts names changed
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.autowired;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@SpringBootApplication
|
||||
public class TypesOfBeanInjectionSpring {
|
||||
private final UserService userService;
|
||||
|
||||
@Autowired // the @Autowired can even be omitted, in case there's only one explicit constructor
|
||||
public TypesOfBeanInjectionSpring(UserService userService) {
|
||||
this.userService = userService;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TypesOfBeanInjectionSpring.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
CommandLineRunner runIt() {
|
||||
return args -> {
|
||||
userService.listUsers()
|
||||
.stream()
|
||||
.forEach(System.out::println);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class User {
|
||||
private String name;
|
||||
|
||||
public User(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
// getters and setters ...
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface UserService {
|
||||
List<User> listUsers();
|
||||
}
|
||||
|
||||
@Service
|
||||
class UserServiceImpl implements UserService {
|
||||
|
||||
@Override
|
||||
public List<User> listUsers() {
|
||||
ArrayList<User> users = new ArrayList<>(3);
|
||||
users.add(new User("Snoopy"));
|
||||
users.add(new User("Woodstock"));
|
||||
users.add(new User("Charlie Brown"));
|
||||
return users;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.autowired;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class TypesOfBeanInjectionSpringIntegrationTest {
|
||||
@Autowired
|
||||
UserService userService;
|
||||
|
||||
private static final String[] expected = new String[] { "Snoopy", "Woodstock", "Charlie Brown" };
|
||||
|
||||
@Test
|
||||
public void givenDI_whenInjectObject_thenUserNamesAreListed() {
|
||||
Assert.assertArrayEquals(expected, userService.listUsers()
|
||||
.stream()
|
||||
.map(User::getName)
|
||||
.toArray());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user