File tree Expand file tree Collapse file tree
main/java/com/baeldung/stream
test/java/com/baeldung/stream
java/com/baeldung/beaninjection
test/java/com/baeldung/beaninjection Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .stream ;
2+
3+ import java .util .List ;
4+ import java .util .stream .Stream ;
5+
6+ public class StreamApi {
7+
8+ public String getLastElementUsingReduce (List <String > valueList ) {
9+ Stream <String > stream = valueList .stream ();
10+ return stream .reduce ((first , second ) -> second ).orElse (null );
11+ }
12+
13+ public Integer getInfiniteStreamLastElementUsingReduce () {
14+ Stream <Integer > stream = Stream .iterate (0 , i -> i + 1 );
15+ return stream .limit (20 ).reduce ((first , second ) -> second ).orElse (null );
16+ }
17+
18+ public String getLastElementUsingSkip (List <String > valueList ) {
19+ long count = valueList .stream ().count ();
20+ Stream <String > stream = valueList .stream ();
21+ return stream .skip (count - 1 ).findFirst ().orElse (null );
22+ }
23+
24+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .stream ;
2+
3+ import static org .junit .Assert .assertEquals ;
4+
5+ import java .util .ArrayList ;
6+ import java .util .List ;
7+
8+ import org .junit .Test ;
9+
10+ public class StreamApiTest {
11+ private StreamApi streamApi = new StreamApi ();
12+
13+ @ Test
14+ public void givenList_whenGetLastElementUsingReduce_thenReturnLastElement () {
15+ List <String > valueList = new ArrayList <String >();
16+ valueList .add ("Joe" );
17+ valueList .add ("John" );
18+ valueList .add ("Sean" );
19+
20+ String last = streamApi .getLastElementUsingReduce (valueList );
21+
22+ assertEquals ("Sean" , last );
23+ }
24+
25+ @ Test
26+ public void givenInfiniteStream_whenGetInfiniteStreamLastElementUsingReduce_thenReturnLastElement () {
27+ Integer last = streamApi .getInfiniteStreamLastElementUsingReduce ();
28+ assertEquals (new Integer (19 ), last );
29+ }
30+
31+ @ Test
32+ public void givenListAndCount_whenGetLastElementUsingSkip_thenReturnLastElement () {
33+ List <String > valueList = new ArrayList <String >();
34+ valueList .add ("Joe" );
35+ valueList .add ("John" );
36+ valueList .add ("Sean" );
37+
38+ String last = streamApi .getLastElementUsingSkip (valueList );
39+
40+ assertEquals ("Sean" , last );
41+ }
42+
43+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjection ;
2+
3+ import org .springframework .beans .factory .annotation .Autowired ;
4+ import org .springframework .stereotype .Component ;
5+
6+ @ Component
7+ public class FileReader {
8+
9+ @ Autowired
10+ private Location location ;
11+
12+ public Location getLocation () {
13+ return location ;
14+ }
15+
16+ public void setLocation (Location location ) {
17+ this .location = location ;
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjection ;
2+
3+ public class FtpReader {
4+ private String ftpHost = null ;
5+ private Integer ftpPort = null ;
6+
7+ // Constructor with arguments
8+ public FtpReader (String host , Integer port ) {
9+ this .ftpHost = host ;
10+ this .ftpPort = port ;
11+ }
12+
13+ public String getFtpHost () {
14+ return ftpHost ;
15+ }
16+
17+ public void setFtpHost (String ftpHost ) {
18+ this .ftpHost = ftpHost ;
19+ }
20+
21+ public Integer getFtpPort () {
22+ return ftpPort ;
23+ }
24+
25+ public void setFtpPort (Integer ftpPort ) {
26+ this .ftpPort = ftpPort ;
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjection ;
2+
3+ import org .springframework .stereotype .Component ;
4+
5+ @ Component
6+ public class Location {
7+ private String filePath ;
8+
9+ public String getFilePath () {
10+ return filePath ;
11+ }
12+
13+ public void setFilePath (String filePath ) {
14+ this .filePath = filePath ;
15+ }
16+
17+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjection ;
2+
3+ import org .springframework .context .annotation .Bean ;
4+ import org .springframework .context .annotation .ComponentScan ;
5+ import org .springframework .context .annotation .Configuration ;
6+
7+ @ Configuration
8+ @ ComponentScan (basePackages = { "com.baeldung.beaninjection" })
9+ public class ReaderApplicationConfig {
10+
11+ @ Bean
12+ public FtpReader exampleDAO () {
13+ return new FtpReader ("localhost" , 21 );
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjection ;
2+
3+ import org .springframework .beans .factory .annotation .Autowired ;
4+ import org .springframework .stereotype .Component ;
5+
6+ @ Component
7+ public class ReaderManager {
8+ private FileReader fileReader ;
9+ private FtpReader ftpReader ;
10+
11+ @ Autowired
12+ public ReaderManager (FtpReader ftpReader ) {
13+ this .ftpReader = ftpReader ;
14+ }
15+
16+ @ Autowired
17+ public void setFileReader (FileReader fileReader ) {
18+ this .fileReader = fileReader ;
19+ }
20+
21+ public FileReader getFileReader () {
22+ return fileReader ;
23+ }
24+
25+ public void setFtpReader (FtpReader ftpReader ) {
26+ this .ftpReader = ftpReader ;
27+ }
28+
29+ public FtpReader getFtpReader () {
30+ return ftpReader ;
31+ }
32+
33+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjection ;
2+
3+ public class ReaderService {
4+ private FtpReader ftpReader ;
5+
6+ public void setFtpReader (FtpReader reader ) {
7+ this .ftpReader = reader ;
8+ }
9+
10+ public FtpReader getFtpReader () {
11+ return ftpReader ;
12+ }
13+
14+ }
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <beans xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
3+ xmlns=" http://www.springframework.org/schema/beans"
4+ xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >
5+
6+ <bean name =" ftpReader" class =" com.baeldung.beaninjection.FtpReader" >
7+ <constructor-arg value =" localhost" />
8+ <constructor-arg value =" 21" />
9+ </bean >
10+
11+ <bean name =" readerService" class =" com.baeldung.beaninjection.ReaderService" >
12+ <property name =" ftpReader" ref =" ftpReader" />
13+ </bean >
14+
15+ </beans >
Original file line number Diff line number Diff line change 1+ package com .baeldung .beaninjection ;
2+
3+ import static org .junit .Assert .assertNotNull ;
4+
5+ import org .junit .Test ;
6+ import org .junit .runner .RunWith ;
7+ import org .springframework .beans .factory .annotation .Autowired ;
8+ import org .springframework .context .ApplicationContext ;
9+ import org .springframework .test .context .ContextConfiguration ;
10+ import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
11+
12+ @ RunWith (SpringJUnit4ClassRunner .class )
13+ @ ContextConfiguration (classes = ReaderApplicationConfig .class )
14+ public class FileReaderTest {
15+
16+ @ Autowired
17+ private ApplicationContext context ;
18+
19+ @ Test
20+ public void testAutowiredAnnotation_WhenField_ThenInjected () {
21+ FileReader service = context .getBean (FileReader .class );
22+ assertNotNull (service .getLocation ());
23+ }
24+ }
You can’t perform that action at this time.
0 commit comments