Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.baeldung.beaninjectiontypes;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import com.baeldung.beaninjectiontypes.models.Family;

@Configuration
@ComponentScan("com.baeldung.beaninjectiontypes")
public class BeanConfig {

@Bean
public Family family() {
return new Family("Ayo", "Kemi");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.baeldung.beaninjectiontypes;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baeldung.beaninjectiontypes.models.Family;

@Component
public class ConstructorInjectionBean {

private Family family;

@Autowired
public ConstructorInjectionBean(Family family) {
this.family = family;
}

public int noOfChildren() {
return family.getChildren()
.size();
}

public void addChildToFamily(String childName) {
family.getChildren()
.add(childName);
}

public boolean isChildAlive(String childName) {
return family.getChildren()
.contains(childName);
}

public void killChild(String childName) {
family.getChildren()
.remove(childName);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.baeldung.beaninjectiontypes;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baeldung.beaninjectiontypes.models.Family;

@Component
public class SetterInjectionBean {

private Family family;

@Autowired
public void setFamily(Family family) {
this.family = family;
}

public int noOfChildren() {
return family.getChildren()
.size();
}

public void addChildToFamily(String childName) {
family.getChildren()
.add(childName);
}

public boolean isChildAlive(String childName) {
return family.getChildren()
.contains(childName);
}

public void killChild(String childName) {
family.getChildren()
.remove(childName);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.baeldung.beaninjectiontypes.models;

import java.util.ArrayList;
import java.util.List;

public class Family {

private String father;
private String mother;
private List<String> children;

public Family(String father, String mother) {
this.father = father;
this.mother = mother;
this.children = new ArrayList<>();
}

public String getFather() {
return father;
}

public void setFather(String father) {
this.father = father;
}

public String getMother() {
return mother;
}

public void setMother(String mother) {
this.mother = mother;
}

public List<String> getChildren() {
return children;
}

public void setChildren(List<String> children) {
this.children = children;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.baeldung.beaninjectiontypes;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.baeldung.beaninjectiontypes.models.Family;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { BeanConfig.class })
public class ConstructorInjectionBeanTest {

@Autowired
private ConstructorInjectionBean constructorFamilyBean;

@Test
public void testConstructorInjectionBean() {
assertNotNull(constructorFamilyBean);
}

@Test
public void testNoOfChildren() {
assertTrue(constructorFamilyBean.noOfChildren() == 0);
constructorFamilyBean.addChildToFamily("Bimbo");
constructorFamilyBean.addChildToFamily("Bayo");
assertTrue(constructorFamilyBean.noOfChildren() == 2);
}

@Test
public void testAddChildToFamily() {
assertFalse(constructorFamilyBean.isChildAlive("Damola"));
constructorFamilyBean.addChildToFamily("Damola");
assertTrue(constructorFamilyBean.isChildAlive("Damola"));
constructorFamilyBean.killChild("Damola");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.baeldung.beaninjectiontypes;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.baeldung.beaninjectiontypes.models.Family;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { BeanConfig.class })
public class SetterInjectionBeanTest {

@Autowired
private SetterInjectionBean setterFamilyBean;

@Test
public void testSetterInjectionBean() {
assertNotNull(setterFamilyBean);
}

@Test
public void testNoOfChildren() {
assertTrue(setterFamilyBean.noOfChildren() == 0);
setterFamilyBean.addChildToFamily("Bimbo");
setterFamilyBean.addChildToFamily("Bayo");
assertTrue(setterFamilyBean.noOfChildren() == 2);
}

@Test
public void testAddChildToFamily() {
assertFalse(setterFamilyBean.isChildAlive("Damola"));
setterFamilyBean.addChildToFamily("Damola");
assertTrue(setterFamilyBean.isChildAlive("Damola"));
setterFamilyBean.killChild("Damola");
}

}