Commit for Different Types of Bean Injection in Spring — Draft article

This commit is contained in:
korneliusz.wandzel 2018-01-29 01:03:39 +01:00
parent 0d85d1ad01
commit 7e9794256d
10 changed files with 186 additions and 0 deletions

View File

@ -0,0 +1,19 @@
package com.baeldung.beaninjectiontypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Boat {
private Engine engine;
@Autowired
public void setEngine(Engine engine) {
this.engine = engine;
}
public Engine getEngine() {
return engine;
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.beaninjectiontypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Car {
@Autowired
private Engine engine;
public void setEngine(Engine engine) {
this.engine = engine;
}
public Engine getEngine() {
return engine;
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.beaninjectiontypes;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.baeldung.beaninjectiontypes")
public class Config {
}

View File

@ -0,0 +1,8 @@
package com.baeldung.beaninjectiontypes;
import org.springframework.stereotype.Component;
@Component
public class Engine {
}

View File

@ -0,0 +1,19 @@
package com.baeldung.beaninjectiontypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class LawnMower {
@Autowired
private Engine engine;
public LawnMower(Engine engine) {
this.engine = engine;
}
public Engine getEngine() {
return engine;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.beaninjectiontypes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Rocket {
private Engine engine;
@Autowired
public Rocket(Engine engine) {
this.engine = engine;
}
public Engine getEngine() {
return engine;
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.beaninjectiontypes;
import static org.junit.Assert.assertNotNull;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class)
public class BoatTest {
@Autowired
Boat boat;
@Test
public void engineInjectionTest() {
assertNotNull(boat);
assertNotNull(boat.getEngine());
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.beaninjectiontypes;
import static org.junit.Assert.assertNotNull;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class)
public class CarTest {
@Autowired
Car car;
@Test
public void engineInjectionTest() {
assertNotNull(car);
assertNotNull(car.getEngine());
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.beaninjectiontypes;
import static org.junit.Assert.assertNotNull;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class)
public class LawnMowerTest {
@Autowired
LawnMower lawnMower;
@Test
public void engineInjectionTest() {
assertNotNull(lawnMower);
assertNotNull(lawnMower.getEngine());
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.beaninjectiontypes;
import static org.junit.Assert.assertNotNull;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class)
public class RocketTest {
@Autowired
Rocket rocket;
@Test
public void engineInjectionTest() {
assertNotNull(rocket);
assertNotNull(rocket.getEngine());
}
}